gettext

package module
Version: v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2020 License: BSD-3-Clause Imports: 12 Imported by: 6

README

  • 赞助 BTC: 1Cbd6oGAUUyBi7X7MaR4np4nTmQZXVgkCW
  • 赞助 ETH: 0x623A3C3a72186A6336C79b18Ac1eD36e1c71A8a6
  • Go语言付费QQ群: 1055927514

gettext-go: GNU gettext for Go (Imported By Kubernetes)

Install

  1. go get github.com/chai2010/gettext-go
  2. go run hello.go

The godoc.org or go.dev has more information.

Examples

package main

import (
	"fmt"

	"github.com/chai2010/gettext-go"
)

func main() {
	gettext := gettext.New("hello", "./examples/locale").SetLanguage("zh_CN")
	fmt.Println(gettext.Gettext("Hello, world!"))

	// Output: 你好, 世界!
}
package main

import (
	"fmt"

	"github.com/chai2010/gettext-go"
)

func main() {
	gettext.SetLanguage("zh_CN")
	gettext.BindLocale(gettext.New("hello", "locale"))

	// gettext.BindLocale("hello", "locale")              // from locale dir
	// gettext.BindLocale("hello", "locale.zip")          // from locale zip file
	// gettext.BindLocale("hello", "locale.zip", zipData) // from embedded zip data

	// translate source text
	fmt.Println(gettext.Gettext("Hello, world!"))
	// Output: 你好, 世界!

	// if no msgctxt in PO file (only msgid and msgstr),
	// specify context as "" by
	fmt.Println(gettext.PGettext("", "Hello, world!"))
	// Output: 你好, 世界!

	// translate resource
	fmt.Println(string(gettext.Getdata("poems.txt"))))
	// Output: ...
}

Go file: hello.go; PO file: hello.po;


API Changes (v0.1.0 vs v1.0.0)

Renamed package path
v0.1.0 (old) v1.0.0 (new)
github.com/chai2010/gettext-go/gettext github.com/chai2010/gettext-go
github.com/chai2010/gettext-go/gettext/po github.com/chai2010/gettext-go/po
github.com/chai2010/gettext-go/gettext/mo github.com/chai2010/gettext-go/mo
github.com/chai2010/gettext-go/gettext/plural github.com/chai2010/gettext-go/plural
Renamed functions
v0.1.0 (old) v1.0.0 (new)
gettext-go/gettext.* gettext-go.*
gettext-go/gettext.DefaultLocal gettext-go.DefaultLanguage
gettext-go/gettext.BindTextdomain gettext-go.BindLocale
gettext-go/gettext.Textdomain gettext-go.SetDomain
gettext-go/gettext.SetLocale gettext-go.SetLanguage
gettext-go/gettext/po.Load gettext-go/po.LoadFile
gettext-go/gettext/po.LoadData gettext-go/po.Load
gettext-go/gettext/mo.Load gettext-go/mo.LoadFile
gettext-go/gettext/mo.LoadData gettext-go/mo.Load
Use empty string as the default context for gettext.Gettext
package main

// v0.1.0
// if the **context** missing, use `callerName(2)` as the context:

// v1.0.0
// if the **context** missing, use empty string as the context:

func main() {
	gettext.Gettext("hello")          
	// v0.1.0 => gettext.PGettext("main.main", "hello")
	// v1.0.0 => gettext.PGettext("", "hello")

	gettext.DGettext("domain", "hello")
	// v0.1.0 => gettext.DPGettext("domain", "main.main", "hello")
	// v1.0.0 => gettext.DPGettext("domain", "", "hello")

	gettext.NGettext("domain", "hello", "hello2", n)
	// v0.1.0 => gettext.PNGettext("domain", "main.main", "hello", "hello2", n)
	// v1.0.0 => gettext.PNGettext("domain", "", "hello", "hello2", n)

	gettext.DNGettext("domain", "hello", "hello2", n)
	// v0.1.0 => gettext.DPNGettext("domain", "main.main", "hello", "hello2", n)
	// v1.0.0 => gettext.DPNGettext("domain", "", "hello", "hello2", n)
}
BindLocale support FileSystem interface
// Use FileSystem:
//	BindLocale(New("poedit", "name", OS("path/to/dir"))) // bind "poedit" domain
//	BindLocale(New("poedit", "name", OS("path/to.zip"))) // bind "poedit" domain

New API in v1.0.0

Gettexter interface:

type Gettexter interface {
	FileSystem() FileSystem

	GetDomain() string
	SetDomain(domain string) Gettexter

	GetLanguage() string
	SetLanguage(lang string) Gettexter

	Gettext(msgid string) string
	PGettext(msgctxt, msgid string) string

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

	DGettext(domain, msgid string) string
	DPGettext(domain, msgctxt, msgid string) string
	DNGettext(domain, msgid, msgidPlural string, n int) string
	DPNGettext(domain, msgctxt, msgid, msgidPlural string, n int) string

	Getdata(name string) []byte
	DGetdata(domain, name string) []byte
}

func New(domain, path string, data ...interface{}) Gettexter

FileSystem interface:

type FileSystem interface {
	LocaleList() []string
	LoadMessagesFile(domain, lang, ext string) ([]byte, error)
	LoadResourceFile(domain, lang, name string) ([]byte, error)
	String() string
}

func NewFS(name string, x interface{}) FileSystem
func OS(root string) FileSystem
func ZipFS(r *zip.Reader, name string) FileSystem
func NilFS(name string) FileSystem

BUGS

Please report bugs to chaishushan@gmail.com.

Thanks!

Documentation

Overview

Package gettext implements a basic GNU's gettext library.

Example:

import (
	"github.com/chai2010/gettext-go"
)

func main() {
	gettext.SetLanguage("zh_CN")

	// gettext.BindLocale(gettext.New("hello", "locale"))              // from locale dir
	// gettext.BindLocale(gettext.New("hello", "locale.zip"))          // from locale zip file
	// gettext.BindLocale(gettext.New("hello", "locale.zip", zipData)) // from embedded zip data

	gettext.BindLocale(gettext.New("hello", "locale"))

	// translate source text
	fmt.Println(gettext.Gettext("Hello, world!"))
	// Output: 你好, 世界!

	// translate resource
	fmt.Println(string(gettext.Getdata("poems.txt")))
	// Output: ...
}

Translate directory struct("./examples/locale.zip"):

Root: "path" or "file.zip/zipBaseName"
 +-default                 # locale: $(LC_MESSAGES) or $(LANG) or "default"
 |  +-LC_MESSAGES            # just for `gettext.Gettext`
 |  |   +-hello.mo             # $(Root)/$(lang)/LC_MESSAGES/$(domain).mo
 |  |   +-hello.po             # $(Root)/$(lang)/LC_MESSAGES/$(domain).po
 |  |   \-hello.json           # $(Root)/$(lang)/LC_MESSAGES/$(domain).json
 |  |
 |  \-LC_RESOURCE            # just for `gettext.Getdata`
 |      +-hello                # domain map a dir in resource translate
 |         +-favicon.ico       # $(Root)/$(lang)/LC_RESOURCE/$(domain)/$(filename)
 |         \-poems.txt
 |
 \-zh_CN                   # simple chinese translate
    +-LC_MESSAGES
    |   +-hello.po             # try "$(domain).po" first
    |   +-hello.mo             # try "$(domain).mo" second
    |   \-hello.json           # try "$(domain).json" third
    |
    \-LC_RESOURCE
        +-hello
           +-favicon.ico       # $(lang)/$(domain)/favicon.ico
           \-poems.txt         # $(lang)/$(domain)/poems.txt

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!

Example
gettext := gettext.New("hello", "./examples/locale").SetLanguage("zh_CN")
fmt.Println(gettext.Gettext("Hello, world!"))
Output:

你好, 世界!
Example (Bind)
gettext.BindLocale(gettext.New("hello", "./examples/locale.zip"))
gettext.SetLanguage("zh_CN")

fmt.Println(gettext.Gettext("Hello, world!"))
Output:

你好, 世界!
Example (Json)
const jsonData = `{
		"zh_CN": {
			"LC_MESSAGES": {
				"hello.json": [{
					"msgctxt"     : "",
					"msgid"       : "Hello, world!",
					"msgid_plural": "",
					"msgstr"      : ["你好, 世界!"]
				}]
			}
		}
	}`

gettext := gettext.New("hello", "???", jsonData).SetLanguage("zh_CN")
fmt.Println(gettext.Gettext("Hello, world!"))
Output:

你好, 世界!
Example (MultiLang)
zh := gettext.New("hello", "./examples/locale").SetLanguage("zh_CN")
tw := gettext.New("hello", "./examples/locale").SetLanguage("zh_TW")

fmt.Println(zh.PGettext(
	"code.google.com/p/gettext-go/examples/hi.SayHi",
	"pkg hi: Hello, world!",
))

fmt.Println(tw.PGettext(
	"code.google.com/p/gettext-go/examples/hi.SayHi",
	"pkg hi: Hello, world!",
))
Output:

来自"Hi"包的问候: 你好, 世界!(ctx:code.google.com/p/gettext-go/examples/hi.SayHi)
來自"Hi"包的問候: 你好, 世界!(ctx:code.google.com/p/gettext-go/examples/hi.SayHi)
Example (Zip)
gettext := gettext.New("hello", "./examples/locale.zip").SetLanguage("zh_CN")
fmt.Println(gettext.Gettext("Hello, world!"))
Output:

你好, 世界!
Example (ZipData)
zipData, err := ioutil.ReadFile("./examples/locale.zip")
if err != nil {
	log.Fatal(err)
}

gettext := gettext.New("hello", "???", zipData).SetLanguage("zh_CN")
fmt.Println(gettext.Gettext("Hello, world!"))
Output:

你好, 世界!

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	DefaultLanguage string = getDefaultLanguage() // use $(LC_MESSAGES) or $(LANG) or "default"
)

Functions

func BindLocale

func BindLocale(g Gettexter)

BindLocale sets and queries program's domains.

Examples:

BindLocale(New("poedit", "locale")) // bind "poedit" domain

Use zip file:

BindLocale(New("poedit", "locale.zip"))          // bind "poedit" domain
BindLocale(New("poedit", "locale.zip", zipData)) // bind "poedit" domain

Use FileSystem:

BindLocale(New("poedit", "name", OS("path/to/dir"))) // bind "poedit" domain
BindLocale(New("poedit", "name", OS("path/to.zip"))) // 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")
	BindLocale("hello", "locale.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 ""
}

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 SetDomain

func SetDomain(domain string) string

SetDomain 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:

SetDomain("poedit") // set domain: poedit
SetDomain("")       // get domain: return poedit

func SetLanguage

func SetLanguage(lang string) string

SetLanguage sets and queries the program's current lang.

If the lang is not empty string, set the new locale.

If the lang is empty string, don't change anything.

Returns is the current locale.

Examples:

SetLanguage("")      // get locale: return DefaultLocale
SetLanguage("zh_CN") // set locale: return zh_CN
SetLanguage("")      // get locale: return zh_CN

Types

type FileSystem

type FileSystem interface {
	LocaleList() []string
	LoadMessagesFile(domain, lang, ext string) ([]byte, error)
	LoadResourceFile(domain, lang, name string) ([]byte, error)
	String() string
}

func NewFS

func NewFS(name string, x interface{}) FileSystem

func NilFS

func NilFS(name string) FileSystem

func OS

func OS(root string) FileSystem

func ZipFS

func ZipFS(r *zip.Reader, name string) FileSystem

type Gettexter

type Gettexter interface {
	FileSystem() FileSystem

	GetDomain() string
	SetDomain(domain string) Gettexter

	GetLanguage() string
	SetLanguage(lang string) Gettexter

	Gettext(msgid string) string
	PGettext(msgctxt, msgid string) string

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

	DGettext(domain, msgid string) string
	DPGettext(domain, msgctxt, msgid string) string
	DNGettext(domain, msgid, msgidPlural string, n int) string
	DPNGettext(domain, msgctxt, msgid, msgidPlural string, n int) string

	Getdata(name string) []byte
	DGetdata(domain, name string) []byte
}

func New

func New(domain, path string, data ...interface{}) Gettexter

New create Interface use default language.

Directories

Path Synopsis
cmd
xgettext-go
The xgettext-go program extracts translatable strings from Go packages.
The xgettext-go program extracts translatable strings from Go packages.
This is a gettext-go exmaple.
This is a gettext-go exmaple.
hi
Package hi is a example pkg.
Package hi is a example pkg.
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
y or Y : Canonical URL