Documentation ¶
Overview ¶
Package gotext implements GNU gettext utilities.
For quick/simple translations you can use the package level functions directly.
import ( "fmt" "github.com/DeineAgenturUG/gotext" ) func main() { // Configure package gotext.Configure("/path/to/locales/root/dir", "en_UK", "domain-name") // Translate text from default domain fmt.Println(gotext.Get("My text on 'domain-name' domain")) // Translate text from a different domain without reconfigure fmt.Println(gotext.GetD("domain2", "Another text on a different domain")) }
Index ¶
- Constants
- Variables
- func Configure(lib, lang, dom string)
- func Get(str string, vars ...interface{}) string
- func GetC(str, ctx string, vars ...interface{}) string
- func GetD(dom, str string, vars ...interface{}) string
- func GetDC(dom, str, ctx string, vars ...interface{}) string
- func GetDomain() string
- func GetInstance(loadDomains, loadLanguages []string, ...)
- func GetLanguage() string
- func GetLibrary() string
- func GetN(str, plural string, n int, vars ...interface{}) string
- func GetNC(str, plural string, n int, ctx string, vars ...interface{}) string
- func GetND(dom, str, plural string, n int, vars ...interface{}) string
- func GetNDC(dom, str, plural string, n int, ctx string, vars ...interface{}) string
- func NPrintf(format string, params map[string]interface{})
- func Printf(str string, vars ...interface{}) string
- func SetDomain(dom string)
- func SetLanguage(lang string)
- func SetLibrary(lib string)
- func SimplifiedLocale(lang string) string
- func Sprintf(format string, params map[string]interface{}) string
- func UniqStrings(input []string) []string
- type Locale
- func (l *Locale) AddDomain(dom string)
- func (l *Locale) AddTranslator(dom string, tr Translator)
- func (l *Locale) Get(str string, vars ...interface{}) string
- func (l *Locale) GetC(str, ctx string, vars ...interface{}) string
- func (l *Locale) GetD(dom, str string, vars ...interface{}) string
- func (l *Locale) GetDC(dom, str, ctx string, vars ...interface{}) string
- func (l *Locale) GetDomain() string
- func (l *Locale) GetN(str, plural string, n int, vars ...interface{}) string
- func (l *Locale) GetNC(str, plural string, n int, ctx string, vars ...interface{}) string
- func (l *Locale) GetND(dom, str, plural string, n int, vars ...interface{}) string
- func (l *Locale) GetNDC(dom, str, plural string, n int, ctx string, vars ...interface{}) string
- func (l *Locale) MarshalBinary() ([]byte, error)
- func (l *Locale) SetDomain(dom string)
- func (l *Locale) UnmarshalBinary(data []byte) error
- type LocaleEncoding
- type Mo
- func (mo *Mo) Get(str string, vars ...interface{}) string
- func (mo *Mo) GetC(str, ctx string, vars ...interface{}) string
- func (mo *Mo) GetN(str, plural string, n int, vars ...interface{}) string
- func (mo *Mo) GetNC(str, plural string, n int, ctx string, vars ...interface{}) string
- func (mo *Mo) MarshalBinary() ([]byte, error)
- func (mo *Mo) Parse(buf []byte)
- func (mo *Mo) ParseFile(f string)
- func (mo *Mo) UnmarshalBinary(data []byte) error
- type Po
- func (po *Po) Get(str string, vars ...interface{}) string
- func (po *Po) GetC(str, ctx string, vars ...interface{}) string
- func (po *Po) GetN(str, plural string, n int, vars ...interface{}) string
- func (po *Po) GetNC(str, plural string, n int, ctx string, vars ...interface{}) string
- func (po *Po) MarshalBinary() ([]byte, error)
- func (po *Po) Parse(buf []byte)
- func (po *Po) ParseFile(f string)
- func (po *Po) UnmarshalBinary(data []byte) error
- type Translation
- type Translator
- type TranslatorEncoding
Constants ¶
const ( // MoMagicLittleEndian encoding MoMagicLittleEndian = 0x950412de // MoMagicBigEndian encoding MoMagicBigEndian = 0xde120495 // EotSeparator msgctxt and msgid separator EotSeparator = "\x04" // NulSeparator msgid and msgstr separator NulSeparator = "\x00" )
Variables ¶
var (
// DefaultDomain as mostly used
DefaultDomain = "default"
)
Functions ¶
func Configure ¶
func Configure(lib, lang, dom string)
Configure sets all configuration variables to be used at package level and reloads the corresponding Translation file. It receives the library path, language code and domain name. This function is recommended to be used when changing more than one setting, as using each setter will introduce a I/O overhead because the Translation file will be loaded after each set.
func Get ¶
Get uses the default domain globally set to return the corresponding Translation of a given string. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func GetC ¶ added in v1.0.0
GetC uses the default domain globally set to return the corresponding Translation of the given string in the given context. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func GetD ¶
GetD returns the corresponding Translation in the given domain for a given string. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func GetDC ¶ added in v1.0.0
GetDC returns the corresponding Translation in the given domain for the given string in the given context. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func GetDomain ¶
func GetDomain() string
GetDomain is the domain getter for the package configuration
func GetInstance ¶ added in v1.4.0
func GetInstance(loadDomains, loadLanguages []string, defaultDomain, defaultLanguage, library string)
GetInstance Create Instance default configuration
func GetLanguage ¶
func GetLanguage() string
GetLanguage is the language getter for the package configuration
func GetLibrary ¶
func GetLibrary() string
GetLibrary is the library getter for the package configuration
func GetN ¶
GetN retrieves the (N)th plural form of Translation for the given string in the default domain. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func GetNC ¶ added in v1.0.0
GetNC retrieves the (N)th plural form of Translation for the given string in the given context in the default domain. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func GetND ¶
GetND retrieves the (N)th plural form of Translation in the given domain for a given string. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func GetNDC ¶ added in v1.0.0
GetNDC retrieves the (N)th plural form of Translation in the given domain for a given string. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func NPrintf ¶ added in v1.4.0
NPrintf support named format NPrintf("%(name)s is Type %(type)s", map[string]interface{}{"name": "Gotext", "type": "struct"})
func SetDomain ¶
func SetDomain(dom string)
SetDomain sets the name for the domain to be used at package level. It reloads the corresponding Translation file.
func SetLanguage ¶
func SetLanguage(lang string)
SetLanguage sets the language code to be used at package level. It reloads the corresponding Translation file.
func SetLibrary ¶
func SetLibrary(lib string)
SetLibrary sets the root path for the loale directories and files to be used at package level. It reloads the corresponding Translation file.
func SimplifiedLocale ¶ added in v1.4.0
SimplifiedLocale simplified locale like " en_US"/"de_DE "/en_US.UTF-8/zh_CN/zh_TW/el_GR@euro/... to en_US, de_DE, zh_CN, el_GR...
func Sprintf ¶ added in v1.4.0
Sprintf support named format
Sprintf("%(name)s is Type %(type)s", map[string]interface{}{"name": "Gotext", "type": "struct"})
func UniqStrings ¶ added in v1.4.0
UniqStrings returns a unique subset of the string slice provided.
Types ¶
type Locale ¶
type Locale struct { // List of available Domains for this locale. Domains sync.Map // Sync Mutex sync.RWMutex // contains filtered or unexported fields }
Locale wraps the entire i18n collection for a single language (locale) It's used by the package functions, but it can also be used independently to handle multiple languages at the same time by working with this object.
Example:
import ( "encoding/gob" "bytes" "fmt" "github.com/DeineAgenturUG/gotext" ) func main() { // Create Locale with library path and language code l := gotext.NewLocale("/path/to/i18n/dir", "en_US") // Load domain '/path/to/i18n/dir/en_US/LC_MESSAGES/default.{po,mo}' l.AddDomain("default") // Translate text from default domain fmt.Println(l.Get("Translate this")) // Load different domain ('/path/to/i18n/dir/en_US/LC_MESSAGES/extras.{po,mo}') l.AddDomain("extras") // Translate text from domain fmt.Println(l.GetD("extras", "Translate this")) }
func NewLocale ¶
NewLocale creates and initializes a new Locale object for a given language. It receives a path for the i18n .po/.mo files directory (p) and a language code to use (l).
func (*Locale) AddDomain ¶
AddDomain creates a new domain for a given locale object and initializes the Po object. If the domain exists, it gets reloaded.
func (*Locale) AddTranslator ¶ added in v1.4.0
func (l *Locale) AddTranslator(dom string, tr Translator)
AddTranslator takes a domain name and a Translator object to make it available in the Locale object.
func (*Locale) Get ¶
Get uses a domain "default" to return the corresponding Translation of a given string. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Locale) GetC ¶ added in v1.0.0
GetC uses a domain "default" to return the corresponding Translation of the given string in the given context. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Locale) GetD ¶
GetD returns the corresponding Translation in the given domain for the given string. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Locale) GetDC ¶ added in v1.0.0
GetDC returns the corresponding Translation in the given domain for the given string in the given context. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Locale) GetN ¶
GetN retrieves the (N)th plural form of Translation for the given string in the "default" domain. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Locale) GetNC ¶ added in v1.0.0
GetNC retrieves the (N)th plural form of Translation for the given string in the given context in the "default" domain. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Locale) GetND ¶
GetND retrieves the (N)th plural form of Translation in the given domain for the given string. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Locale) GetNDC ¶ added in v1.0.0
GetNDC retrieves the (N)th plural form of Translation in the given domain for the given string in the given context. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Locale) MarshalBinary ¶ added in v1.4.0
MarshalBinary implements encoding BinaryMarshaler interface
func (*Locale) UnmarshalBinary ¶ added in v1.4.0
UnmarshalBinary implements encoding BinaryUnmarshaler interface
type LocaleEncoding ¶ added in v1.4.0
type LocaleEncoding struct { Path string Lang string Domains map[string][]byte DefaultDomain string }
LocaleEncoding is used as intermediary storage to encode Locale objects to Gob.
type Mo ¶ added in v1.4.0
type Mo struct { // Headers storage Headers textproto.MIMEHeader // Language header Language string // Plural-Forms header PluralForms string // Sync Mutex sync.RWMutex // contains filtered or unexported fields }
Mo parses the content of any MO file and provides all the Translation functions needed. It's the base object used by all package methods. And it's safe for concurrent use by multiple goroutines by using the sync package for locking.
Example:
import ( "fmt" "github.com/DeineAgenturUG/gotext" ) func main() { // Create po object po := gotext.NewMoTranslator() // Parse .po file po.ParseFile("/path/to/po/file/translations.mo") // Get Translation fmt.Println(po.Get("Translate this")) }
func (*Mo) Get ¶ added in v1.4.0
Get retrieves the corresponding Translation for the given string. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Mo) GetC ¶ added in v1.4.0
GetC retrieves the corresponding Translation for a given string in the given context. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Mo) GetN ¶ added in v1.4.0
GetN retrieves the (N)th plural form of Translation for the given string. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Mo) GetNC ¶ added in v1.4.0
GetNC retrieves the (N)th plural form of Translation for the given string in the given context. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Mo) MarshalBinary ¶ added in v1.4.0
MarshalBinary implements encoding.BinaryMarshaler interface
func (*Mo) Parse ¶ added in v1.4.0
Parse loads the translations specified in the provided string (str)
func (*Mo) ParseFile ¶ added in v1.4.0
ParseFile tries to read the file by its provided path (f) and parse its content as a .po file.
func (*Mo) UnmarshalBinary ¶ added in v1.4.0
UnmarshalBinary implements encoding.BinaryUnmarshaler interface
type Po ¶
type Po struct { // Headers storage Headers textproto.MIMEHeader // Language header Language string // Plural-Forms header PluralForms string // Sync Mutex sync.RWMutex // contains filtered or unexported fields }
Po parses the content of any PO file and provides all the Translation functions needed. It's the base object used by all package methods. And it's safe for concurrent use by multiple goroutines by using the sync package for locking.
Example:
import ( "fmt" "github.com/DeineAgenturUG/gotext" ) func main() { // Create po object po := gotext.NewPoTranslator() // Parse .po file po.ParseFile("/path/to/po/file/translations.po") // Get Translation fmt.Println(po.Get("Translate this")) }
func (*Po) Get ¶
Get retrieves the corresponding Translation for the given string. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Po) GetC ¶ added in v1.0.0
GetC retrieves the corresponding Translation for a given string in the given context. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Po) GetN ¶
GetN retrieves the (N)th plural form of Translation for the given string. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Po) GetNC ¶ added in v1.0.0
GetNC retrieves the (N)th plural form of Translation for the given string in the given context. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Po) MarshalBinary ¶ added in v1.4.0
MarshalBinary implements encoding.BinaryMarshaler interface
func (*Po) ParseFile ¶
ParseFile tries to read the file by its provided path (f) and parse its content as a .po file.
func (*Po) UnmarshalBinary ¶ added in v1.4.0
UnmarshalBinary implements encoding.BinaryUnmarshaler interface
type Translation ¶ added in v1.4.0
Translation is the struct for the Translations parsed via Po or Mo files and all coming parsers
func NewTranslation ¶ added in v1.4.0
func NewTranslation() *Translation
NewTranslation returns the Translation object and initialized it.
func (*Translation) Get ¶ added in v1.4.0
func (t *Translation) Get() string
Get returns the string of the translation
func (*Translation) GetN ¶ added in v1.4.0
func (t *Translation) GetN(n int) string
GetN returns the string of the plural translation
type Translator ¶ added in v1.4.0
type Translator interface { ParseFile(f string) Parse(buf []byte) Get(str string, vars ...interface{}) string GetN(str, plural string, n int, vars ...interface{}) string GetC(str, ctx string, vars ...interface{}) string GetNC(str, plural string, n int, ctx string, vars ...interface{}) string MarshalBinary() ([]byte, error) UnmarshalBinary([]byte) error }
Translator interface is used by Locale and Po objects.Translator It contains all methods needed to parse translation sources and obtain corresponding translations. Also implements gob.GobEncoder/gob.DobDecoder interfaces to allow serialization of Locale objects.
func NewMoTranslator ¶ added in v1.4.0
func NewMoTranslator() Translator
NewMoTranslator creates a new Mo object with the Translator interface
func NewPoTranslator ¶ added in v1.4.0
func NewPoTranslator() Translator
NewPoTranslator creates a new Po object with the Translator interface
type TranslatorEncoding ¶ added in v1.4.0
type TranslatorEncoding struct { // Headers storage Headers textproto.MIMEHeader // Language header Language string // Plural-Forms header PluralForms string // Parsed Plural-Forms header values Nplurals int Plural string // Storage Translations map[string]*Translation Contexts map[string]map[string]*Translation }
TranslatorEncoding is used as intermediary storage to encode Translator objects to Gob.
func (*TranslatorEncoding) GetTranslator ¶ added in v1.4.0
func (te *TranslatorEncoding) GetTranslator() Translator
GetTranslator is used to recover a Translator object after unmarshaling the TranslatorEncoding object. Internally uses a Po object as it should be switcheable with Mo objects without problem. External Translator implementations should be able to serialize into a TranslatorEncoding object in order to unserialize into a Po-compatible object.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cli
|
|
* Copyright (c) 2018 DeineAgentur UG https://www.deineagentur.com.
|
* Copyright (c) 2018 DeineAgentur UG https://www.deineagentur.com. |
loader
|
|
Package plurals is the pluralform compiler to get the correct translation id of the plural string
|
Package plurals is the pluralform compiler to get the correct translation id of the plural string |