Documentation ¶
Overview ¶
Package gotext implements GNU gettext utilities.
For quick/simple translations you can use the package level functions directly.
import "github.com/leonelquinteros/gotext" func main() { // Configure package gotext.Configure("/path/to/locales/root/dir", "en_UK", "domain-name") // Translate text from default domain println(gotext.Get("My text on 'domain-name' domain")) // Translate text from a different domain without reconfigure println(gotext.GetD("domain2", "Another text on a different domain")) }
Index ¶
- 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 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 SetDomain(dom string)
- func SetLanguage(lang string)
- func SetLibrary(lib string)
- type Locale
- func (l *Locale) AddDomain(dom string)
- 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) 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
- 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) Parse(str string)
- func (po *Po) ParseFile(f string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
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 ¶
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 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 translation for the given string in the "default" domain. If n == 0, usually the singular form of the string is returned as defined in the PO file. 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 translation for the given string in the given context in the "default" domain. If n == 0, usually the singular form of the string is returned as defined in the PO file. 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 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 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 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.
Types ¶
type Locale ¶
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 "github.com/leonelquinteros/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' l.AddDomain("default") // Translate text from default domain println(l.Get("Translate this")) // Load different domain ('/path/to/i18n/dir/en_US/LC_MESSAGES/extras.po') l.AddDomain("extras") // Translate text from domain 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 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) 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 ¶
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) GetN ¶
GetN retrieves the (N)th plural form translation for the given string in the "default" domain. If n == 0, usually the singular form of the string is returned as defined in the PO file. 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 translation for the given string in the given context in the "default" domain. If n == 0, usually the singular form of the string is returned as defined in the PO file. 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 translation in the given domain for the given string. If n == 0, usually the singular form of the string is returned as defined in the PO file. 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 translation in the given domain for the given string in the given context. If n == 0, usually the singular form of the string is returned as defined in the PO file. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
type Po ¶
Po parses the content of any PO file and provides all the translation functions needed. It's the base object used by all packafe methods. And it's safe for concurrent use by multiple goroutines by using the sync package for write locking.
Example:
import "github.com/leonelquinteros/gotext" func main() { // Create po object po := new(gotext.Po) // Parse .po file po.ParseFile("/path/to/po/file/translations.po") // Get translation 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 ¶
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 translation for the given string. If n == 0, usually the singular form of the string is returned as defined in the PO file. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (*Po) GetNC ¶
GetNC retrieves the (N)th plural form translation for the given string in the given context. If n == 0, usually the singular form of the string is returned as defined in the PO file. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.