Internationalization support for Go language (resource file translation)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In the previous internationalization support for the Go language (based on gettext-go), we talked about how to translate strings in the source code.

Project address in: Http://code.google.com/p/gettext-go. The document is in godoc.org or gowalker.org.

Based on feedback from comments (@ sheep half cents), previous versions of the lack of support for resource files.

Recently, some improvements have been made to gettext-go, which mainly involve the following points:

    • Support for translation of resource files
    • Support for translation files in zip format

Translation of resource files

The translation of a resource file is simpler than a string (using a gettext.Getdata function):

import (    "code.google.com/p/gettext-go/gettext")func main() {    gettext.SetLocale("zh_CN")    gettext.Textdomain("hello")    // translate resource    fmt.Println(string(gettext.Getdata("poems.txt")))    // Output: ...}

The content of the output is Li Bai's poetry < month under the sole discretion;

    • Simplified Chinese version: < month under the sole discretion >
    • Traditional Chinese version: < month under the sole discretion >
    • English translation version: < month under the sole discretion >

Translation file support in zip format

The translation file directory can be packaged in a zip format. If the bound translation file is not a directory, but a file, it is treated as a zip file.

import (    "code.google.com/p/gettext-go/gettext")func main() {    gettext.SetLocale("zh_CN")    gettext.Textdomain("hello")    gettext.BindTextdomain("hello", "local.zip", nil)    ...}

ZIP file here: Http://gettext-go.googlecode.com/hg/examples/local.zip

If you want to embed a zip file into your program, you can first convert the zip file to formatted data by using a tool []byte , and then bind to the translation domain:

import (    "code.google.com/p/gettext-go/gettext")// 根据 local.zip 生成var local_zip_data = []byte{    0x00, 0x01, 0x02, 0x03, ...}func main() {    gettext.SetLocale("zh_CN")    gettext.Textdomain("hello")    gettext.BindTextdomain("hello", "embeded_local", local_zip_data)    ...}

If gettext.BindTextdomain the third argument is not nil , the data passed into the parameter is processed as a zip.

Structure of the translation directory

The translation of the Gettext-go package involves the following concepts:

    • Local: The language used locally. The default value can be specified by the $(LC_MESSAGES) or $(LANG) environment variable.
    • domain : The first parameter of a translation namespace, similar to the concept of a color style in a text editor gettext.BindTextdomain .
    • domain_path: domain corresponding directory path, may also be a zip file path or zip data name, gettext.BindTextdomain the second parameter.
    • domain_data: The second parameter of domain corresponding zip data (can be empty) gettext.BindTextdomain .

The internal directory organizational structure is consistent, whether it is a zip or a local directory:

  Root: "Path" or "File.zip/zipbasename" +-default # Local: $ (lc_messages) or $ (LANG) or "default  " | +-lc_messages # Just for ' GetText.  Gettext ' |   |  +-hello.mo # $ (Root)/$ (local)/lc_messages/$ (domain). Mo |   |  \-hello.po # $ (Root)/$ (local)/lc_messages/$ (domain). Mo |  | | \-lc_resource # Just for ' GetText.      Getdata ' |         +-hello # domain Map a dir in resource translate |         +-favicon.ico # $ (Root)/$ (local)/lc_resource/$ (domain)/$ (filename) | \-poems.txt |   \-ZH_CN # Simple Chinese translate +-lc_messages |   +-hello.mo # Try "$ (domain). Mo" First |    \-hello.po # Try "$ (domain). po" Second |         \-lc_resource +-hello +-favicon.ico # try "$ (local)/$ (domain)/file" first \-poems.txt # try "default/$ (domain)/file" second  

The $(Root)/$(local)/LC_MESSAGES/ corresponding gettext.Gettext translation string, $(Root)/$(local)/LC_RESOURCE/$(domain)/? corresponding gettext.Getdata to the resource file to be translated.

When a string is translated, the binary translation file of the format is tried first, mo and if it fails, the po original translation file is attempted and the original string is returned if the failure persists.

When translating a resource file, if the resource file is missing, it will continue to attempt to load default the local resource file and return if it still fails nil .

Prospect

At present, the Gettext-go run time has been initially complete, can support the translation of strings and resource files. However, Gettext-go's assistive tools are still inadequate, especially for tools that automatically extract strings from the Go program xgettext . The next step is to consider implementing a tool for the go language xgettext .

There is also a special kind of string resource in the Go language: Document information for Godoc display (similar to blog articles, etc.). If the xgettext tool can support the extraction of information from the Go language document and the merging of the translated documents, then the translation of the document will be much more convenient.

If you are interested in the students can be perfect together.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.