在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:nicksnyder/go-i18n开源软件地址:https://github.com/nicksnyder/go-i18n开源编程语言:Go 99.9%开源软件介绍:go-i18ngo-i18n is a Go package and a command that helps you translate Go programs into multiple languages.
Package i18nThe i18n package provides support for looking up messages according to a set of locale preferences. import "github.com/nicksnyder/go-i18n/v2/i18n" Create a Bundle to use for the lifetime of your application. bundle := i18n.NewBundle(language.English) Load translations into your bundle during initialization. bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
bundle.LoadMessageFile("es.toml") Create a Localizer to use for a set of language preferences. func(w http.ResponseWriter, r *http.Request) {
lang := r.FormValue("lang")
accept := r.Header.Get("Accept-Language")
localizer := i18n.NewLocalizer(bundle, lang, accept)
} Use the Localizer to lookup messages. localizer.Localize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "PersonCats",
One: "{{.Name}} has {{.Count}} cat.",
Other: "{{.Name}} has {{.Count}} cats.",
},
TemplateData: map[string]interface{}{
"Name": "Nick",
"Count": 2,
},
PluralCount: 2,
}) // Nick has 2 cats. Command goi18nThe goi18n command manages message files used by the i18n package.
Extracting messagesUse # active.en.toml
[PersonCats]
description = "The number of cats a person has"
one = "{{.Name}} has {{.Count}} cat."
other = "{{.Name}} has {{.Count}} cats." Translating a new language
Translating new messagesIf you have added new messages to your program:
For more information and examples:
Licensego-i18n is available under the MIT license. See the LICENSE file for more info. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论