golang 国际化 / 多语言 支持库 easy-I18N
Easy-i18n
Easy-i18n是Go程序包和命令,可帮助您将Go程序翻译成多种语言。
-支持带有=x 或 >x表达式的复数字符串。
-支持类似于fmt.Sprintf格式语法的字符串。
-支持任何格式的消息文件(例如JSON,TOML,YAML)。
Package i18n
i18n软件包提供了根据一组区域设置首选项查找消息的支持。
package main
import (
"fmt"
"os"
"github.com/mylukin/easy-i18n/i18n"
"golang.org/x/text/language"
)
func main() {
i18n.SetLang(language.SimplifiedChinese)
i18n.Printf("hello world!")
fmt.Println()
name := "Lukin"
i18n.Printf("hello %s!", name)
fmt.Println()
i18n.Printf("%s has %d cat.", name, 1)
fmt.Println()
i18n.Printf("%s has %d cat.", name, 2, i18n.Plural(
"%[2]d=1", "%s has %d cat.",
"%[2]d>1", "%s has %d cats.",
))
fmt.Println()
i18n.Fprintf(os.Stderr, "%s have %d apple.", name, 2, i18n.Plural(
"%[2]d=1", "%s have an apple.",
"%[2]d=2", "%s have two apples.",
"%[2]d>2", "%s have %d apples.",
))
fmt.Println()
}
Command easyi18n
The easyi18n command manages message files used by the i18n package.
go get -u github.com/mylukin/easy-i18n/easyi18n
easyi18n -h
update, u merge translations and generate catalog
extract, e extracts strings to be translated from code
generate, g generates code to insert translated messages
Extracting messages
Use easyi18n extract . ./locales/en.json
to extract all i18n.Sprintf function literals in Go source files to a message file for translation.
./locales/en.json
{
"%s has %d cat.": "%s has %d cat.",
"%s has %d cats.": "%s has %d cats.",
"%s have %d apples.": "%s have %d apples.",
"%s have an apple.": "%s have an apple.",
"%s have two apples.": "%s have two apples.",
"hello %s!": "hello %s!",
"hello world!": "hello world!"
}
Translating a new language
Create an empty message file for the language that you want to add (e.g.
zh-Hans.json
).Run
easyi18n update ./locales/en.json ./locales/zh-Hans.json
to populatezh-Hans.json
with the mesages to be translated../locales/zh-Hans.json
{ "%s has %d cat.": "%s有%d只猫。", "%s has %d cats.": "%s有%d只猫。", "%s have %d apples.": "%s有%d个苹果。", "%s have an apple.": "%s有一个苹果。", "%s have two apples.": "%s有两个苹果。", "hello %s!": "你好%s!", "hello world!": "你好世界!" }
After
zh-Hans.json
has been translated, runeasyi18n generate ./locales ./catalog.go --pkg=main
.Make sure that –pkg=main your package name, automatically load catalog.go file.
Translating new messages
If you have added new messages to your program:
- Run
easyi18n extract
to update./locales/en.json
with the new messages. - Run
easyi18n update ./locales/en.json
to generate updated./locales/new-language.json
files. - Translate all the messages in the
./locales/new-language.json
files. - Run
easyi18n generate ./locales ./catalog.go --pkg=main
to merge the translated messages into the go files.
For examples:
- Look at an example application.
Thanks:
- https://xuanwo.io/2019/12/11/golang-i18n/
- https://github.com/qingstor/qsctl/tree/mas...
- https://github.com/golang/text
- https://github.com/nicksnyder/go-i18n
License
Easy-i18n is available under the MIT license. See the LICENSE file for more info.
代码仓库地址:https://github.com/mylukin/easy-i18n,记得star
YouTube 高清视频下载,支持搜索,海量视频,完全免费,全程无需科学:tools.liu.app/youtube