Golang的i18n库:lingo

jopen 10年前

非常基本的Golang的i18n库。

特性:

  1. 存储消息在JSON文件中
  2. 支持嵌套的声明。
  3. 检测基于请求头的语言。
  4. 使用方法很简单。

用法:

  1. 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?"          }        }      }
  2. Initialize a Lingo like this:

        lingo := New("en_US", "path/to/translations/dir")
  3. 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.

  4. 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?

项目主页:http://www.open-open.com/lib/view/home/1416207794211