Easy-i18n是Go程序包和命令,可帮助您将Go程序翻译成多种语言。
-支持带有=x 或 >x表达式的复数字符串。
-支持类似于fmt.Sprintf格式语法的字符串。
-支持任何格式的消息文件(例如JSON,TOML,YAML)。
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
easyi18n extract . ./locales/en.json
./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
{
"%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!": "你好世界!"
}
Translating new messages
If you have added new messages to your program:
easyi18n extract./locales/en.jsoneasyi18n update ./locales/en.json./locales/new-language.json./locales/new-language.jsoneasyi18n generate ./locales ./catalog.go --pkg=main
For examples:
Thanks:
License
Easy-i18n is available under the MIT license. See the LICENSE file for more info.