开源软件名称(OpenSource Name):kataras/i18n开源软件地址(OpenSource Url):https://github.com/kataras/i18n开源编程语言(OpenSource Language):Go 100.0%开源软件介绍(OpenSource Introduction):i18n (Go)Efficient and easy to use localization and internationalization support for Go. InstallationThe only requirement is the Go Programming Language. $ go get github.com/kataras/i18n@latest Examples Getting startedCreate a folder named │ main.go
└───locales
├───el-GR
│ example.yml
├───en-US
│ example.yml
└───zh-CN
example.yml Now, put the key-values content for each locale, e.g. ./locales/en-US/example.yml hi: "Hi %s"
#
# Templates are supported
# hi: "Hi {{ .Name }}
#
# Template functions are supported
# hi: "Hi {{sayHi .Name}} # ./locales/el-GR/example.yaml
hi: "Γειά σου %s" # ./locales/zh-CN/example.yaml
hi: 您好 %s Some other possible filename formats...
The Default Let's take a look at the simplest usage of this package. package main
import (
"fmt"
"github.com/kataras/i18n"
)
type user struct {
Name string
Age int
}
func main() {
// i18n.SetDefaultLanguage("en-US")
// Fmt style.
enText := i18n.Tr("en", "hi", "John Doe") // or "en-US"
elText := i18n.Tr("el", "hi", "John Doe")
zhText := i18n.Tr("zh", "hi", "John Doe")
fmt.Println(enText)
fmt.Println(elText)
fmt.Println(zhText)
// Templates style.
templateData := user{
Name: "John Doe",
Age: 66,
}
enText = i18n.Tr("en-US", "intro", templateData) // or "en"
elText = i18n.Tr("el-GR", "intro", templateData)
zhText = i18n.Tr("zh-CN", "intro", templateData)
fmt.Println(enText)
fmt.Println(elText)
fmt.Println(zhText)
} Load specific languages over a new I18n instance. The default language is the first registered, in that case is the "en-US". I18n, err := i18n.New(i18n.Glob("./locales/*/*"), "en-US", "el-GR", "zh-CN")
// load embedded files through a go-bindata package
I18n, err := i18n.New(i18n.Assets(AssetNames, Asset), "en-US", "el-GR", "zh-CN") Template variables & functionsUsing template variables & functions as values in your locale value entry via We are going to use a 3rd-party package for plural and singular words. Note that this is only for english dictionary, but you can use the Before we get started, install the necessary packages: $ go get -u github.com/kataras/i18n
$ go get -u github.com/gertd/go-pluralize Let's create two simple translation files for our example. The Dog: "dog"
HiDogs: Hi {{plural (tr "Dog") .count }} Dog: "σκυλί"
HiDogs: Γειά {{plural (tr "Dog") .count }}
Now, create a package main
import (
"fmt"
"text/template"
"github.com/kataras/i18n"
"github.com/gertd/go-pluralize"
)
var pluralizeClient = pluralize.NewClient()
func getFuncs(current *i18n.Locale) template.FuncMap {
return template.FuncMap{
"plural": func(word string, count int) string {
return pluralizeClient.Pluralize(word, count, true)
},
}
}
func main() {
I18n, err := i18n.New(i18n.Glob("./locales/*/*", i18n.LoaderConfig{
// Set custom functions per locale!
Funcs: getFuncs,
}), "en-US", "el-GR", "zh-CN")
if err != nil {
panic(err)
}
textEnglish := I18n.Tr("en", "HiDogs", map[string]interface{}{
"count": 2,
}) // prints "Hi 2 dogs".
fmt.Println(textEnglish)
textEnglishSingular := I18n.Tr("en", "HiDogs", map[string]interface{}{
"count": 1,
}) // prints "Hi 1 dog".
fmt.Println(textEnglishSingular)
textGreek := I18n.Tr("el", "HiDogs", map[string]interface{}{
"count": 1,
}) // prints "Γειά 1 σκυλί".
fmt.Println(textGreek)
} Use Hi 2 dogs
Hi 1 dog
Γειά 1 σκυλί HTTPHTTP, automatically searches for url parameter, cookie, custom function and headers for the current user language. mux := http.NewServeMux()
I18n.URLParameter = "lang" // i.e https://domain.com?lang=el
I18n.Cookie = "lang"
I18n.ExtractFunc = func(r *http.Request) string { /* custom logic */ }
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
translated := I18n.GetMessage(r, "hi", "John Doe")
fmt.Fprintf(w, "Text: %s", translated)
}) Prefer mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
locale := I18n.GetLocale(r)
translated := locale.GetMessage("hi", "John Doe")
// [...some locale.GetMessage calls]
}) Optionally, identify the current language by subdomain or path prefix, e.g. en.domain.com and domain.com/en or domain.com/en-US and e.t.c. I18n.Subdomain = true
http.ListenAndServe(":8080", I18n.Router(mux)) If the I18n.ContextKey = "lang"
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
currentLang := r.Context().Value("lang").(string)
fmt.Fprintf(w, "Language: %s", currentLang)
}) Set the translate function as a key on a templates, _ := template.ParseGlob("./templates/*.html")
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// per-request.
translateFunc := I18n.GetLocale(r).GetMessage
templates.ExecuteTemplate(w, "index.html", map[string]interface{}{
"tr": translateFunc,
})
// {{ call .tr "hi" "John Doe" }}
}) Global function with the language as its first input argument. translateLangFunc := I18n.Tr
templates.Funcs(template.FuncMap{
"tr": translateLangFunc,
})
// {{ tr "en" "hi" "John Doe" }} For a more detailed technical documentation you can head over to our godocs. And for executable code you can always visit the _examples repository's subdirectory. Licensekataras/i18n is free and open-source software licensed under the MIT License. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论