Documentation ¶
Overview ¶
Package gotext implements GNU gettext utilities.
For quick/simple translations you can use the package level functions directly.
import ( "fmt" "code.rocket9labs.com/tslocum/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 EscapeSpecialCharacters(s string) 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 GetLanguage() string
- func GetLanguages() []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 IsTranslated(str string, langs ...string) bool
- func IsTranslatedC(str, ctx string, langs ...string) bool
- func IsTranslatedD(dom, str string, langs ...string) bool
- func IsTranslatedDC(dom, str, ctx string, langs ...string) bool
- func IsTranslatedN(str string, n int, langs ...string) bool
- func IsTranslatedNC(str string, n int, ctx string, langs ...string) bool
- func IsTranslatedND(dom, str string, n int, langs ...string) bool
- func IsTranslatedNDC(dom, str string, n int, ctx string, langs ...string) bool
- 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 SetLocales(locales []*Locale)
- func SetStorage(locale *Locale)deprecated
- func SimplifiedLocale(lang string) string
- func Sprintf(format string, params map[string]interface{}) string
- type Domain
- func (do *Domain) DropStaleTranslations()
- func (do *Domain) Get(str string, vars ...interface{}) string
- func (do *Domain) GetC(str, ctx string, vars ...interface{}) string
- func (do *Domain) GetN(str, plural string, n int, vars ...interface{}) string
- func (do *Domain) GetNC(str, plural string, n int, ctx string, vars ...interface{}) string
- func (do *Domain) GetRefs(str string) []string
- func (do *Domain) GetTranslations() map[string]*Translation
- func (do *Domain) IsTranslated(str string) bool
- func (do *Domain) IsTranslatedC(str, ctx string) bool
- func (do *Domain) IsTranslatedN(str string, n int) bool
- func (do *Domain) IsTranslatedNC(str string, n int, ctx string) bool
- func (do *Domain) MarshalText() ([]byte, error)
- func (do *Domain) Set(id, str string)
- func (do *Domain) SetC(id, ctx, str string)
- func (do *Domain) SetN(id, plural string, n int, str string)
- func (do *Domain) SetNC(id, plural, ctx string, n int, str string)
- func (do *Domain) SetRefs(str string, refs []string)
- type HeaderMap
- type IsTranslatedDomainIntrospector
- type IsTranslatedIntrospector
- 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) GetActualLanguage(dom string) 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) GetLanguage() 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) GetTranslations() map[string]*Translation
- func (l *Locale) IsTranslated(str string) bool
- func (l *Locale) IsTranslatedC(str, ctx string) bool
- func (l *Locale) IsTranslatedD(dom, str string) bool
- func (l *Locale) IsTranslatedDC(dom, str, ctx string) bool
- func (l *Locale) IsTranslatedN(str string, n int) bool
- func (l *Locale) IsTranslatedNC(str string, n int, ctx string) bool
- func (l *Locale) IsTranslatedND(dom, str string, n int) bool
- func (l *Locale) IsTranslatedNDC(dom string, str string, n int, ctx string) bool
- func (l *Locale) SetDomain(dom string)
- 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) GetDomain() *Domain
- 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) IsTranslated(str string) bool
- func (mo *Mo) IsTranslatedC(str, ctx string) bool
- func (mo *Mo) IsTranslatedN(str string, n int) bool
- func (mo *Mo) IsTranslatedNC(str string, n int, ctx string) bool
- func (mo *Mo) Parse(buf []byte)
- func (mo *Mo) ParseFile(f string)
- type Po
- func (po *Po) DropStaleTranslations()
- func (po *Po) Get(str string, vars ...interface{}) string
- func (po *Po) GetC(str, ctx string, vars ...interface{}) string
- func (po *Po) GetDomain() *Domain
- 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) GetRefs(str string) []string
- func (po *Po) IsTranslated(str string) bool
- func (po *Po) IsTranslatedC(str, ctx string) bool
- func (po *Po) IsTranslatedN(str string, n int) bool
- func (po *Po) IsTranslatedNC(str string, n int, ctx string) bool
- func (po *Po) MarshalText() ([]byte, error)
- func (po *Po) Parse(buf []byte)
- func (po *Po) ParseFile(f string)
- func (po *Po) Set(id, str string)
- func (po *Po) SetC(id, ctx, str string)
- func (po *Po) SetN(id, plural string, n int, str string)
- func (po *Po) SetNC(id, plural, ctx string, n int, str string)
- func (po *Po) SetRefs(str string, refs []string)
- type SourceReference
- type Translation
- func (t *Translation) Get() string
- func (t *Translation) GetN(n int) string
- func (t *Translation) IsStale() bool
- func (t *Translation) IsTranslated() bool
- func (t *Translation) IsTranslatedN(n int) bool
- func (t *Translation) Set(str string)
- func (t *Translation) SetN(n int, str string)
- func (t *Translation) SetRefs(refs []string)
- 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 FallbackLocale = "en_US"
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 EscapeSpecialCharacters ¶
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 ¶
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 ¶
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 GetLanguage ¶
func GetLanguage() string
GetLanguage returns the language gotext will translate into. If multiple languages have been supplied, the first one will be returned. If no language has been supplied, the fallback will be returned.
func GetLanguages ¶
func GetLanguages() []string
GetLanguages returns all languages that have been supplied.
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 ¶
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 ¶
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 IsTranslated ¶
IsTranslated reports whether a string is translated in given languages. When the langs argument is omitted, the output of GetLanguages is used.
func IsTranslatedC ¶
IsTranslatedC reports whether a context string is translated in given languages. When the langs argument is omitted, the output of GetLanguages is used.
func IsTranslatedD ¶
IsTranslatedD reports whether a domain string is translated in given languages. When the langs argument is omitted, the output of GetLanguages is used.
func IsTranslatedDC ¶
IsTranslatedDC reports whether a domain context string is translated in given languages. When the langs argument is omitted, the output of GetLanguages is used.
func IsTranslatedN ¶
IsTranslatedN reports whether a plural string is translated in given languages. When the langs argument is omitted, the output of GetLanguages is used.
func IsTranslatedNC ¶
IsTranslatedNC reports whether a plural context string is translated in given languages. When the langs argument is omitted, the output of GetLanguages is used.
func IsTranslatedND ¶
IsTranslatedND reports whether a plural domain string is translated in any of given languages. When the langs argument is omitted, the output of GetLanguages is used.
func IsTranslatedNDC ¶
IsTranslatedNDC reports whether a plural domain context string is translated in any of given languages. When the langs argument is omitted, the output of GetLanguages is used.
func NPrintf ¶
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 (or colon separated language codes) 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 locale directories and files to be used at package level. It reloads the corresponding Translation file.
func SetLocales ¶
func SetLocales(locales []*Locale)
SetLocales allows for overriding the global Locale objects with ones built manually with NewLocale(). This makes it possible to attach custom Domain objects from in-memory po/mo. The library, language and domain of the first Locale will set the default global configuration.
func SetStorage
deprecated
func SetStorage(locale *Locale)
SetStorage allows overriding the global Locale object with one built manually with NewLocale().
Deprecated: Storage has been renamed to Locale for consistency, use SetLocales instead.
func SimplifiedLocale ¶
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...
Types ¶
type Domain ¶
type Domain struct { Headers HeaderMap // Language header Language string // Plural-Forms header PluralForms string // contains filtered or unexported fields }
Domain has all the common functions for dealing with a gettext domain it's initialized with a GettextFile (which represents either a Po or Mo file)
func (*Domain) DropStaleTranslations ¶
func (do *Domain) DropStaleTranslations()
Drops any translations stored that have not been Set*() since 'po' was initialised
func (*Domain) GetC ¶
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 (*Domain) 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 (*Domain) GetNC ¶
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 (*Domain) GetTranslations ¶
func (do *Domain) GetTranslations() map[string]*Translation
GetTranslations returns a copy of every translation in the domain. It does not support contexts.
func (*Domain) IsTranslated ¶
IsTranslated reports whether a string is translated
func (*Domain) IsTranslatedC ¶
IsTranslatedC reports whether a context string is translated
func (*Domain) IsTranslatedN ¶
IsTranslatedN reports whether a plural string is translated
func (*Domain) IsTranslatedNC ¶
IsTranslatedNC reports whether a plural context string is translated
func (*Domain) MarshalText ¶
MarshalText implements encoding.TextMarshaler interface Assists round-trip of POT/PO content
type IsTranslatedDomainIntrospector ¶
type IsTranslatedDomainIntrospector interface { IsTranslated(str string) bool IsTranslatedN(str string, n int) bool IsTranslatedD(dom, str string) bool IsTranslatedND(dom, str string, n int) bool IsTranslatedC(str, ctx string) bool IsTranslatedNC(str string, n int, ctx string) bool IsTranslatedDC(dom, str, ctx string) bool IsTranslatedNDC(dom, str string, n int, ctx string) bool }
IsTranslatedDomainIntrospector is able to determine whether a given string is translated. Example of this introspector is Locale, which holds multiple domains. Simpler objects that are domain-specific, like Po or Mo, implement IsTranslatedIntrospector.
type IsTranslatedIntrospector ¶
type IsTranslatedIntrospector interface { IsTranslated(str string) bool IsTranslatedN(str string, n int) bool IsTranslatedC(str, ctx string) bool IsTranslatedNC(str string, n int, ctx string) bool }
IsTranslatedIntrospector is able to determine whether a given string is translated. Examples of this introspector are Po and Mo, which are specific to their domain. Locale holds multiple domains and also implements IsTranslatedDomainIntrospector.
type Locale ¶
type Locale struct { // List of available Domains for this locale. Domains map[string]Translator // 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 ( "bytes" "fmt" "code.rocket9labs.com/tslocum/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 GetLocales ¶
func GetLocales() []*Locale
func GetStorage
deprecated
func GetStorage() *Locale
GetStorage is the locale storage getter for the package configuration.
Deprecated: Storage has been renamed to Locale for consistency, use GetLocales instead.
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 NewLocaleFS ¶
NewLocaleFS returns a Locale working with a fs.FS
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 ¶
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) GetActualLanguage ¶
GetActualLanguage inspects the filesystem and decides whether to strip a CC part of the ll_CC locale string.
func (*Locale) GetC ¶
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 ¶
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) GetLanguage ¶
GetLanguage is the lang getter for Locale configuration
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 ¶
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 ¶
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) GetTranslations ¶
func (l *Locale) GetTranslations() map[string]*Translation
GetTranslations returns a copy of all translations in all domains of this locale. It does not support contexts.
func (*Locale) IsTranslated ¶
IsTranslated reports whether a string is translated
func (*Locale) IsTranslatedC ¶
IsTranslatedC reports whether a context string is translated
func (*Locale) IsTranslatedD ¶
IsTranslatedD reports whether a domain string is translated
func (*Locale) IsTranslatedDC ¶
IsTranslatedDC reports whether a domain context string is translated
func (*Locale) IsTranslatedN ¶
IsTranslatedN reports whether a plural string is translated
func (*Locale) IsTranslatedNC ¶
IsTranslatedNC reports whether a plural context string is translated
func (*Locale) IsTranslatedND ¶
IsTranslatedND reports whether a plural domain string is translated
func (*Locale) IsTranslatedNDC ¶
IsTranslatedNDC reports whether a plural domain context string is translated
type LocaleEncoding ¶
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 ¶
type Mo struct { //these three public members are for backwards compatibility. they are just set to the value in the domain Headers HeaderMap Language string PluralForms string // 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" "code.rocket9labs.com/tslocum/gotext" ) func main() { // Create mo object mo := gotext.NewMo() // Parse .mo file mo.ParseFile("/path/to/po/file/translations.mo") // Get Translation fmt.Println(mo.Get("Translate this")) }
func (*Mo) IsTranslated ¶
func (*Mo) IsTranslatedC ¶
type Po ¶
type Po struct { //these three public members are for backwards compatibility. they are just set to the value in the domain Headers HeaderMap Language string PluralForms string // 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" "code.rocket9labs.com/tslocum/gotext" ) func main() { // Create po object po := gotext.NewPo() // Parse .po file po.ParseFile("/path/to/po/file/translations.po") // Get Translation fmt.Println(po.Get("Translate this")) }
func (*Po) IsTranslated ¶
func (*Po) IsTranslatedC ¶
func (*Po) MarshalText ¶
type SourceReference ¶
type SourceReference struct {
// contains filtered or unexported fields
}
type Translation ¶
type Translation struct { ID string PluralID string Trs map[int]string Refs []string // contains filtered or unexported fields }
Translation is the struct for the Translations parsed via Po or Mo files and all coming parsers
func NewTranslation ¶
func NewTranslation() *Translation
NewTranslation returns the Translation object and initialized it.
func NewTranslationWithRefs ¶
func NewTranslationWithRefs(refs []string) *Translation
func (*Translation) Get ¶
func (t *Translation) Get() string
Get returns the string of the translation
func (*Translation) GetN ¶
func (t *Translation) GetN(n int) string
GetN returns the string of the plural translation
func (*Translation) IsStale ¶
func (t *Translation) IsStale() bool
func (*Translation) IsTranslated ¶
func (t *Translation) IsTranslated() bool
IsTranslated reports whether a string is translated
func (*Translation) IsTranslatedN ¶
func (t *Translation) IsTranslatedN(n int) bool
IsTranslatedN reports whether a plural string is translated
func (*Translation) Set ¶
func (t *Translation) Set(str string)
func (*Translation) SetN ¶
func (t *Translation) SetN(n int, str string)
func (*Translation) SetRefs ¶
func (t *Translation) SetRefs(refs []string)
type Translator ¶
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 GetDomain() *Domain }
Translator interface is used by Locale and Po objects.Translator It contains all methods needed to parse translation sources and obtain corresponding translations.
type TranslatorEncoding ¶
type TranslatorEncoding struct { // Headers storage Headers HeaderMap // 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 ¶
func (te *TranslatorEncoding) GetTranslator() Translator
GetTranslator is used to recover a Translator object after unmarshalling the TranslatorEncoding object. Internally uses a Po object as it should be switchable with Mo objects without problem. External Translator implementations should be able to serialize into a TranslatorEncoding object in order to deserialize into a Po-compatible object.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cli
|
|
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 |