Golang的i18n库:lingo
jopen
10年前
非常基本的Golang的i18n库。
特性:
- 存储消息在JSON文件中
- 支持嵌套的声明。
- 检测基于请求头的语言。
- 使用方法很简单。
用法:
-
Create a dir to store translations, and write them in JSON files named [locale].json. For example:
en_US.json sr_RS.json de.json ...
You can write nested JSON too.
{ "lingo.example.1" : "Example value 1", "lingo.example" : { "2" : "Nested example", "3" : "Nested example too.", "4" : { "inception" : "Double nested?" } } }
-
Initialize a Lingo like this:
lingo := New("en_US", "path/to/translations/dir")
-
Get bundle for specific locale via either
string
:t := lingo.TranslationsForLocale("sr_RS")
This way Lingo will return the bundle for specific locale, or default if given is not found. Alternatively (or primarily), you can get it with
*http.Request
:t := lingo.TranslationsForRequest(req)
This way Lingo finds best suited locale via
Accept-Language
header, or if there is no match, returns default. -
Once you get T instance just fire away!
r1 := t1.Value("lingo.example.1") // Example value 1 r2 := t1.Value("lingo.example.2") // Nested example r2 := t1.Value("lingo.example.4.inception") // Double nested?