gettext

package
Version: v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2020 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package gettext implements a basic GNU's gettext library.

Example:

import (
	"code.google.com/p/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

Constants

This section is empty.

Variables

View Source
var (
	DefaultLocale = getDefaultLocale() // use $(LC_MESSAGES) or $(LANG) or "default"
)

Functions

func BindTextdomain

func BindTextdomain(domain, path string, zipData []byte) (domains, paths []string)

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

func DGetdata(domain, name string) []byte

DGetdata like Getdata(), but looking up the resource in the specified domain.

Examples:

func Foo() {
	msg := gettext.DGetdata("hello", "poems.txt")
}

func DGettext

func DGettext(domain, msgid string) string

DGettext like Gettext(), but looking up the message in the specified domain.

Examples:

func Foo() {
	msg := gettext.DGettext("poedit", "Hello")
}

func DNGettext

func DNGettext(domain, msgid, msgidPlural string, n int) string

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

func DPGettext(domain, msgctxt, msgid string) string

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

func DPNGettext(domain, msgctxt, msgid, msgidPlural string, n int) string

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

func Getdata(name string) []byte

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

func Gettext(msgid string) string

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

func NGettext(msgid, msgidPlural string, n int) string

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

func PGettext(msgctxt, msgid string) string

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

func PNGettext(msgctxt, msgid, msgidPlural string, n int) string

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

func SetLocale(locale string) string

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

func Textdomain(domain string) string

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.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL