i18n

package module
v0.0.0-...-c0075a2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2023 License: MIT Imports: 10 Imported by: 0

README

internationalization

Output the corresponding results according to different languages go-i18n

Usage

package main

import (
	"fmt"

	tran "github.com/segmentfault/pacman/contrib/i18n"
	"github.com/segmentfault/pacman/i18n"
)

func main() {
	translator, err := tran.NewTranslator("/BundleDir")
	if err != nil {
	    panic(err)
	}
	fmt.Println(translator.Tr(i18n.LanguageChinese, "base.success"))
	fmt.Println(translator.Tr(i18n.LanguageEnglish, "base.success"))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	GlobalTrans = &Translator{
		localizes: make(map[i18n.Language]*goI18n.Localizer),
		jsonData:  make(map[i18n.Language]any),
	}
	Bundle = goI18n.NewBundle(language.English)
)

GlobalTrans global translation

Functions

func AddTranslator

func AddTranslator(translation []byte, language string) (err error)

AddTranslator add translator to global translations translation is content language is language file name like en_US.yaml

func NewTranslator

func NewTranslator(bundleDir string) (i18n.Translator, error)

NewTranslator new translator from Bundle/resource directory TODO: singleton and multi-thread safe initialization

Types

type Translator

type Translator struct {
	// contains filtered or unexported fields
}

Translator save the language type with localizer mapping

func (*Translator) Dump

func (tr *Translator) Dump(la i18n.Language) ([]byte, error)

Dump the translator into json format

func (*Translator) Tr

func (tr *Translator) Tr(la i18n.Language, key string) string

Tr to Translate from specified language and string TODO: improve multi-threading performance

func (*Translator) TrWithData

func (tr *Translator) TrWithData(la i18n.Language, key string, templateData any) string

TrWithData to Translate from specified language and string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL