go-i18n - 一个包和一个命令可以帮助你将Go程序翻译成多国语言

网友投稿 922 2022-11-03

go-i18n - 一个包和一个命令可以帮助你将Go程序翻译成多国语言

go-i18n - 一个包和一个命令可以帮助你将Go程序翻译成多国语言

go-i18n is a Go package and a command that helps you translate Go programs into multiple languages.

Supports pluralized strings for all 200+ languages in the Unicode Common Locale Data Repository (CLDR). Code and tests are automatically generated from CLDR data. Supports strings with named variables using text/template syntax.Supports message files of any format (e.g. JSON, TOML, YAML).

The 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.

The goi18n command manages message files used by the i18n package.

go get -u github.com/nicksnyder/go-i18n/v2/goi18ngoi18n -help

Extracting messages

Use goi18n extract to extract all i18n.Message struct literals in Go source files to a message file for translation.

# 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

Create an empty message file for the language that you want to add (e.g. translate.es.toml). Run goi18n merge active.en.toml translate.es.toml to populate translate.es.toml with the mesages to be translated.# translate.es.toml[HelloPerson]hash = "sha1-5b49bfdad81fedaeefb224b0ffc2acc58b09cff5"other = "Hello {{.Name}}" After translate.es.toml has been translated, rename it to active.es.toml.# active.es.toml[HelloPerson]hash = "sha1-5b49bfdad81fedaeefb224b0ffc2acc58b09cff5"other = "Hola {{.Name}}" Load active.es.toml into your bundle.bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)bundle.LoadMessageFile("active.es.toml")

Translating new messages

If you have added new messages to your program:

Run goi18n extract to update active.en.toml with the new messages.Run goi18n merge active.*.toml to generate updated translate.*.toml files.Translate all the messages in the translate.*.toml files.Run goi18n merge active.*.toml translate.*.toml to merge the translated messages into the active message files.

For more information and examples:

Read the documentation.Look at the code examples and tests.Look at an example application.

License

go-i18n is available under the MIT license. See the LICENSE file for more info.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:设计模式(12) -- 建造者模式
下一篇:ZooKeeper框架教程Curator分布式锁实现及源码分析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~