# go-i18n  [](https://goreportcard.com/report/github.com/nicksnyder/go-i18n) [](https://codecov.io/gh/nicksnyder/go-i18n) [](https://sourcegraph.com/github.com/nicksnyder/go-i18n?badge)
go-i18n is a Go [package](#package-i18n) and a [command](#command-goi18n) that helps you translate Go programs into multiple languages.
- Supports [pluralized strings](http://cldr.unicode.org/index/cldr-spec/plural-rules) for all 200+ languages in the [Unicode Common Locale Data Repository (CLDR)](http://www.unicode.org/cldr/charts/28/supplemental/language_plural_rules.html).
- Code and tests are [automatically generated](https://github.com/nicksnyder/go-i18n/tree/master/i18n/language/codegen) from [CLDR data](http://cldr.unicode.org/index/downloads).
- Supports strings with named variables using [text/template](http://golang.org/pkg/text/template/) syntax.
- Supports message files of any format (e.g. JSON, TOML, YAML).
## Package i18n [](http://godoc.org/github.com/nicksnyder/go-i18n/v2/i18n)
The i18n package provides support for looking up messages according to a set of locale preferences.
```go
import "github.com/nicksnyder/go-i18n/v2/i18n"
```
Create a Bundle to use for the lifetime of your application.
```go
bundle := i18n.NewBundle(language.English)
```
Load translations into your bundle during initialization.
```go
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
bundle.LoadMessageFile("es.toml")
```
Create a Localizer to use for a set of language preferences.
```go
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.
```go
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 goi18n [](http://godoc.org/github.com/nicksnyder/go-i18n/v2/goi18n)
The goi18n command manages message files used by the i18n package.
```
go get -u github.com/nicksnyder/go-i18n/v2/goi18n
goi18n -help
```
### Extracting messages
Use `goi18n extract` to extract all i18n.Message struct literals in Go source files to a message file for translation.
```toml
# 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
1. Create an empty message file for the language that you want to add (e.g. `translate.es.toml`).
2. Run `goi18n merge active.en.toml translate.es.toml` to populate `translate.es.toml` with the messages to be translated.
```toml
# translate.es.toml
[HelloPerson]
hash = "sha1-5b49bfdad81fedaeefb224b0ffc2acc58b09cff5"
other = "Hello {{.Name}}"
```
3. After `translate.es.toml` has been translated, rename it to `active.es.toml`.
```toml
# active.es.toml
[HelloPerson]
hash = "sha1-5b49bfdad81fedaeefb224b0ffc2acc58b09cff5"
other = "Hola {{.Name}}"
```
4. Load `active.es.toml` into your bundle.
```go
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
bundle.LoadMessageFile("active.es.toml")
```
### Translating new messages
If you have added new messages to your program:
1. Run `goi18n extract` to update `active.en.toml` with the new messages.
2. Run `goi18n merge active.*.toml` to generate updated `translate.*.toml` files.
3. Translate all the messages in the `translate.*.toml` files.
4. 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](http://godoc.org/github.com/nicksnyder/go-i18n/v2).
- Look at the [code examples](https://github.com/nicksnyder/go-i18n/blob/master/v2/i18n/example_test.go) and [tests](https://github.com/nicksnyder/go-i18n/blob/master/v2/i18n/localizer_test.go).
- Look at an example [application](https://github.com/nicksnyder/go-i18n/tree/master/v2/example).
## License
go-i18n is available under the MIT license. See the [LICENSE](LICENSE) file for more info.
没有合适的资源?快使用搜索试试~ 我知道了~
go-i18n:将您的Go程序翻译成多种语言

共56个文件
go:37个
yml:6个
md:4个


温馨提示
go-i18n go-i18n是一个Go和一个,可帮助您将Go程序翻译成多种语言。 在支持所有200多种语言的。 代码和测试是从。 使用语法支持带有命名变量的字符串。 支持任何格式的消息文件(例如JSON,TOML,YAML)。 套餐i18n i18n软件包支持根据一组区域设置首选项查找消息。 import "github.com/nicksnyder/go-i18n/v2/i18n" 创建一个捆绑包,以在应用程序的整个生命周期内使用。 bundle := i18n . NewBundle ( language . English ) 在初始化期间将翻译加载到包中。 bun
资源详情
资源评论
资源推荐
收起资源包目录



































































共 56 条
- 1



















雪地女王
- 粉丝: 106
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 最佳参考答案Autocad常用快捷键.doc
- 【】photoshop实用教程第09章(000002).ppt
- 大数据时代下的混合云应用.pdf
- 第四部分计算机应用基础考试说明.doc
- PLC在卷扬机自动控制系统中的应用.doc
- 电子信息技术在自动化系统的作用.docx
- 计算机网络工程安全问题与优化措施研究.docx
- 试论互联网+形势下纳税服务的优化.docx
- 《通信原理》-樊昌信-曹丽娜-编著第六版-第2章.ppt
- 通用航空飞行服务站系统设计及监视数据融合算法研究.docx
- 商场荧屏导购展板系统软件需求说明书-可行性研究-操作说明书.doc
- asp个人博客Blog系统实现大学本科方案设计书.doc
- 华为SDN概述-虚拟化.docx
- 物联网与大数据的新思考.docx
- 嵌入式WiFi技术研究报告与通信设计方案.doc
- 关于电气工程及自动化在生活中的应用探讨.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

评论1