Categorygithub.com/chai2010/gettext-go
modulepackage
1.0.3
Repository: https://github.com/chai2010/gettext-go.git
Documentation: pkg.go.dev

# README


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/gettextgithub.com/chai2010/gettext-go
github.com/chai2010/gettext-go/gettext/pogithub.com/chai2010/gettext-go/po
github.com/chai2010/gettext-go/gettext/mogithub.com/chai2010/gettext-go/mo
github.com/chai2010/gettext-go/gettext/pluralgithub.com/chai2010/gettext-go/plural

Renamed functions

v0.1.0 (old)v1.0.0 (new)
gettext-go/gettext.*gettext-go.*
gettext-go/gettext.DefaultLocalgettext-go.DefaultLanguage
gettext-go/gettext.BindTextdomaingettext-go.BindLocale
gettext-go/gettext.Textdomaingettext-go.SetDomain
gettext-go/gettext.SetLocalegettext-go.SetLanguage
gettext-go/gettext/po.Loadgettext-go/po.LoadFile
gettext-go/gettext/po.LoadDatagettext-go/po.Load
gettext-go/gettext/mo.Loadgettext-go/mo.LoadFile
gettext-go/gettext/mo.LoadDatagettext-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 [email protected].

Thanks!

# Packages

No description provided by the author
This is a gettext-go exmaple.
Package mo provides support for reading and writing GNU MO file.
Package plural provides standard plural formulas.
Package po provides support for reading and writing GNU PO file.

# Functions

BindLocale sets and queries program's domains.
DGetdata like Getdata(), but looking up the resource in the specified domain.
DGettext like Gettext(), but looking up the message in the specified domain.
DNGettext like NGettext(), but looking up the message in the specified domain.
DPGettext like PGettext(), but looking up the message in the specified domain.
DPNGettext like PNGettext(), but looking up the message in the specified domain.
Getdata attempt to translate a resource file into the user's native language, by looking up the translation in a message catalog.
Gettext attempt to translate a text string into the user's native language, by looking up the translation in a message catalog.
New create Interface use default language.
No description provided by the author
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.
No description provided by the author
No description provided by the author
PGettext attempt to translate a text string into the user's native language, by looking up the translation in a message catalog.
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.
SetDomain sets and retrieves the current message domain.
SetLanguage sets and queries the program's current lang.
No description provided by the author

# Variables

use $(LC_MESSAGES) or $(LANG) or "default".

# Interfaces

No description provided by the author
No description provided by the author