Documentation ¶
Overview ¶
Package gettext implements a basic GNU's gettext library.
Example:
import ( "github.com/chai2010/gettext-go/gettext" ) func main() { gettext.SetLocale("zh_CN") gettext.Textdomain("hello") // gettext.BindTextdomain("hello", "local", nil) // from local dir // gettext.BindTextdomain("hello", "local.zip", nil) // from local zip file // gettext.BindTextdomain("hello", "local.zip", zipData) // from embedded zip data gettext.BindTextdomain("hello", "local", nil) // translate source text fmt.Println(gettext.Gettext("Hello, world!")) // Output: 你好, 世界! // translate resource fmt.Println(string(gettext.Getdata("poems.txt"))) // Output: ... }
Translate directory struct("../examples/local.zip"):
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
See:
http://en.wikipedia.org/wiki/Gettext http://www.gnu.org/software/gettext/manual/html_node http://www.gnu.org/software/gettext/manual/html_node/Header-Entry.html http://www.gnu.org/software/gettext/manual/html_node/PO-Files.html http://www.gnu.org/software/gettext/manual/html_node/MO-Files.html http://www.poedit.net/
Please report bugs to <chaishushan{AT}gmail.com>. Thanks!
Index ¶
- Variables
- func BindTextdomain(domain, path string, zipData []byte) (domains, paths []string)
- func DGetdata(domain, name string) []byte
- func DGettext(domain, msgid string) string
- func DNGettext(domain, msgid, msgidPlural string, n int) string
- func DPGettext(domain, msgctxt, msgid string) string
- func DPNGettext(domain, msgctxt, msgid, msgidPlural string, n int) string
- func Getdata(name string) []byte
- func Gettext(msgid string) string
- func NGettext(msgid, msgidPlural string, n int) string
- func PGettext(msgctxt, msgid string) string
- func PNGettext(msgctxt, msgid, msgidPlural string, n int) string
- func SetLocale(locale string) string
- func Textdomain(domain string) string
Constants ¶
This section is empty.
Variables ¶
var (
DefaultLocale = getDefaultLocale() // use $(LC_MESSAGES) or $(LANG) or "default"
)
Functions ¶
func BindTextdomain ¶
BindTextdomain sets and queries program's domains.
If the domain and path are all not empty string, bind the new domain. If the domain already exists, return error.
If the domain is not empty string, but the path is the empty string, delete the domain. If the domain don't exists, return error.
If the domain and the path are all empty string, don't change anything.
Returns is the all bind domains.
Examples:
BindTextdomain("poedit", "local", nil) // bind "poedit" domain BindTextdomain("", "", nil) // return all domains BindTextdomain("poedit", "", nil) // delete "poedit" domain BindTextdomain("", "", nil) // return all domains
Use zip file:
BindTextdomain("poedit", "local.zip", nil) // bind "poedit" domain BindTextdomain("poedit", "local.zip", zipData) // bind "poedit" domain
func DGetdata ¶
DGetdata like Getdata(), but looking up the resource in the specified domain.
Examples:
func Foo() { msg := gettext.DGetdata("hello", "poems.txt") }
func DGettext ¶
DGettext like Gettext(), but looking up the message in the specified domain.
Examples:
func Foo() { msg := gettext.DGettext("poedit", "Hello") }
func DNGettext ¶
DNGettext like NGettext(), but looking up the message in the specified domain.
Examples:
func Foo() { msg := gettext.PNGettext("poedit", "gettext-go.example", "%d people", "%d peoples", 2) }
func DPGettext ¶
DPGettext like PGettext(), but looking up the message in the specified domain.
Examples:
func Foo() { msg := gettext.DPGettext("poedit", "gettext-go.example", "Hello") }
func DPNGettext ¶
DPNGettext like PNGettext(), but looking up the message in the specified domain.
Examples:
func Foo() { msg := gettext.DPNGettext("poedit", "gettext-go.example", "%d people", "%d peoples", 2) }
func Getdata ¶
Getdata attempt to translate a resource file into the user's native language, by looking up the translation in a message catalog.
Examples:
func Foo() { Textdomain("hello") BindTextdomain("hello", "local.zip", nilOrZipData) poems := gettext.Getdata("poems.txt") }
func Gettext ¶
Gettext attempt to translate a text string into the user's native language, by looking up the translation in a message catalog.
It use the caller's function name as the msgctxt.
Examples:
func Foo() { msg := gettext.Gettext("Hello") // msgctxt is "some/package/name.Foo" }
func NGettext ¶
NGettext attempt to translate a text string into the user's native language, by looking up the appropriate plural form of the translation in a message catalog.
It use the caller's function name as the msgctxt.
Examples:
func Foo() { msg := gettext.NGettext("%d people", "%d peoples", 2) }
func PGettext ¶
PGettext attempt to translate a text string into the user's native language, by looking up the translation in a message catalog.
Examples:
func Foo() { msg := gettext.PGettext("gettext-go.example", "Hello") // msgctxt is "gettext-go.example" }
func PNGettext ¶
PNGettext attempt to translate a text string into the user's native language, by looking up the appropriate plural form of the translation in a message catalog.
Examples:
func Foo() { msg := gettext.PNGettext("gettext-go.example", "%d people", "%d peoples", 2) }
func SetLocale ¶
SetLocale sets and queries the program's current locale.
If the locale is not empty string, set the new local.
If the locale is empty string, don't change anything.
Returns is the current locale.
Examples:
SetLocale("") // get locale: return DefaultLocale SetLocale("zh_CN") // set locale: return zh_CN SetLocale("") // get locale: return zh_CN
func Textdomain ¶
Textdomain sets and retrieves the current message domain.
If the domain is not empty string, set the new domains.
If the domain is empty string, don't change anything.
Returns is the all used domains.
Examples:
Textdomain("poedit") // set domain: poedit Textdomain("") // get domain: return poedit
Types ¶
This section is empty.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package mo provides support for reading and writing GNU MO file.
|
Package mo provides support for reading and writing GNU MO file. |
Package plural provides standard plural formulas.
|
Package plural provides standard plural formulas. |
Package po provides support for reading and writing GNU PO file.
|
Package po provides support for reading and writing GNU PO file. |