Golang的i18n庫:lingo

jopen 10年前發布 | 23K 次閱讀 lingo Google Go/Golang開發

非常基本的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.baiduhome.net/lib/view/home/1416207794211

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!