gotext

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2020 License: MIT Imports: 14 Imported by: 0

README

GitHub release MIT license GoDoc Gotext build Build Status Go Report Card

Gotext

GNU gettext utilities for Go.

Features

  • Implements GNU gettext support in native Go.
  • Complete support for PO files including:
  • Support for MO files.
  • Thread-safe: This package is safe for concurrent use across multiple goroutines.
  • It works with UTF-8 encoding as it's the default for Go language.
  • Unit tests available.
  • Language codes are automatically simplified from the form en_UK to en if the first isn't available.
  • Ready to use inside Go templates.
  • Objects are serializable to []byte to store them in cache.
  • Support for Go Modules.

License

MIT license

Documentation

Refer to the Godoc package documentation at (https://godoc.org/git.deineagentur.com/DeineAgenturUG/gotext)

Installation

go get git.deineagentur.com/DeineAgenturUG/gotext
  • There are no requirements or dependencies to use this package.
  • No need to install GNU gettext utilities (unless specific needs of CLI tools).
  • No need for environment variables. Some naming conventions are applied but not needed.

Version vendoring

Stable releases use semantic versioning tagging on this repository.

You can rely on this to use your preferred vendoring tool or to manually retrieve the corresponding release tag from the GitHub repository.

Add git.deineagentur.com/DeineAgenturUG/gotext inside the require section in your go.mod file.

i.e.

require (
    git.deineagentur.com/DeineAgenturUG/gotext v1.5.0
)
Vendoring with dep

To use last stable version (v1.5.0 at the moment of writing)

dep ensure -add git.deineagentur.com/DeineAgenturUG/gotext@v1.5.0

Import as

import "git.deineagentur.com/DeineAgenturUG/gotext"
Vendoring with gopkg.in

http://gopkg.in/leonelquinteros/gotext.v1

To get the latest v1 package stable release, execute:

go get gopkg.in/leonelquinteros/gotext.v1

Import as

import "gopkg.in/leonelquinteros/gotext.v1"

Refer to it as gotext.

Locales directories structure

The package will assume a directories structure starting with a base path that will be provided to the package configuration or to object constructors depending on the use, but either will use the same convention to lookup inside the base path.

Inside the base directory where will be the language directories named using the language and country 2-letter codes (en_US, es_AR, ...). All package functions can lookup after the simplified version for each language in case the full code isn't present but the more general language code exists. So if the language set is en_UK, but there is no directory named after that code and there is a directory named en, all package functions will be able to resolve this generalization and provide translations for the more general library.

The language codes are assumed to be ISO 639-1 codes (2-letter codes). That said, most functions will work with any coding standard as long the directory name matches the language code set on the configuration.

Then, there can be a LC_MESSAGES containing all PO files or the PO files themselves. A library directory structure can look like:

/path/to/locales
/path/to/locales/en_US
/path/to/locales/en_US/LC_MESSAGES
/path/to/locales/en_US/LC_MESSAGES/default.po
/path/to/locales/en_US/LC_MESSAGES/extras.po
/path/to/locales/en_UK
/path/to/locales/en_UK/LC_MESSAGES
/path/to/locales/en_UK/LC_MESSAGES/default.po
/path/to/locales/en_UK/LC_MESSAGES/extras.po
/path/to/locales/en_AU
/path/to/locales/en_AU/LC_MESSAGES
/path/to/locales/en_AU/LC_MESSAGES/default.po
/path/to/locales/en_AU/LC_MESSAGES/extras.po
/path/to/locales/es
/path/to/locales/es/default.po
/path/to/locales/es/extras.po
/path/to/locales/es_ES
/path/to/locales/es_ES/default.po
/path/to/locales/es_ES/extras.po
/path/to/locales/fr
/path/to/locales/fr/default.po
/path/to/locales/fr/extras.po

And so on...

Usage examples

Using package for single language/domain settings

For quick/simple translations you can use the package level functions directly.

import (
    "fmt"
    "git.deineagentur.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"))
}

Using dynamic variables on translations

All translation strings support dynamic variables to be inserted without translate. Use the fmt.Printf syntax (from Go's "fmt" package) to specify how to print the non-translated variable inside the translation string.

import (
    "fmt"
    "git.deineagentur.com/DeineAgenturUG/gotext"
)

func main() {
    // Configure package
    gotext.Configure("/path/to/locales/root/dir", "en_UK", "domain-name")

    // Set variables
    name := "John"

    // Translate text with variables
    fmt.Println(gotext.Get("Hi, my name is %s", name))
}

Using Locale object

When having multiple languages/domains/libraries at the same time, you can create Locale objects for each variation so you can handle each settings on their own.

import (
    "fmt"
    "git.deineagentur.com/DeineAgenturUG/gotext"
)

func main() {
    // Create Locale with library path and language code
    l := gotext.NewLocale("/path/to/locales/root/dir", "es_UY")

    // Load domain '/path/to/locales/root/dir/es_UY/default.po'
    l.AddDomain("default")

    // Translate text from default domain
    fmt.Println(l.Get("Translate this"))

    // Load different domain
    l.AddDomain("translations")

    // Translate text from domain
    fmt.Println(l.GetD("translations", "Translate this"))
}

This is also helpful for using inside templates (from the "text/template" package), where you can pass the Locale object to the template. If you set the Locale object as "Loc" in the template, then the template code would look like:

{{ .Loc.Get "Translate this" }}

Using the Po object to handle .po files and PO-formatted strings

For when you need to work with PO files and strings, you can directly use the Po object to parse it and access the translations in there in the same way.

import (
    "fmt"
    "git.deineagentur.com/DeineAgenturUG/gotext"
)

func main() {
    // Set PO content
    str := `
msgid "Translate this"
msgstr "Translated text"

msgid "Another string"
msgstr ""

msgid "One with var: %s"
msgstr "This one sets the var: %s"
`

    // Create Po object
    po := new(gotext.Po)
    po.Parse(str)

    fmt.Println(po.Get("Translate this"))
}

Use plural forms of translations

PO format supports defining one or more plural forms for the same translation. Relying on the PO file headers, a Plural-Forms formula can be set on the translation file as defined in (https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/Plural-forms.html)

Plural formulas are parsed and evaluated using Kinako

import (
    "fmt"
    "git.deineagentur.com/DeineAgenturUG/gotext"
)

func main() {
    // Set PO content
    str := `
msgid ""
msgstr ""

# Header below
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

msgid "Translate this"
msgstr "Translated text"

msgid "Another string"
msgstr ""

msgid "One with var: %s"
msgid_plural "Several with vars: %s"
msgstr[0] "This one is the singular: %s"
msgstr[1] "This one is the plural: %s"
`

    // Create Po object
    po := new(gotext.Po)
    po.Parse(str)

    fmt.Println(po.GetN("One with var: %s", "Several with vars: %s", 54, v))
    // "This one is the plural: Variable"
}

Contribute

  • Please, contribute.
  • Use the package on your projects.
  • Report issues on Github.
  • Send pull requests for bugfixes and improvements.
  • Send proposals on Github issues.

Documentation

Overview

* Package gotext implements GNU gettext utilities. * * For quick/simple translations you can use the package level functions directly. * * import ( * "fmt" * "git.deineagentur.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

View Source
const (
	// MoMagicLittleEndian encoding
	MoMagicLittleEndian = 0x950412de
	// MoMagicBigEndian encoding
	MoMagicBigEndian = 0xde120495

	// EotSeparator msgctxt and msgid separator
	EotSeparator = "\x04"
	// NulSeparator msgid and msgstr separator
	NulSeparator = "\x00"
)

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

func Get(str string, vars ...interface{}) string

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

func GetC(str, ctx string, vars ...interface{}) string

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

func GetD(dom, str string, vars ...interface{}) string

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

func GetDC(dom, str, ctx string, vars ...interface{}) string

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

func GetN(str, plural string, n int, vars ...interface{}) string

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

func GetNC(str, plural string, n int, ctx string, vars ...interface{}) string

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

func GetND(dom, str, plural string, n int, vars ...interface{}) string

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

func GetNDC(dom, str, plural string, n int, ctx string, vars ...interface{}) string

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.5.0

func NPrintf(format string, params map[string]interface{})

NPrintf support named format NPrintf("%(name)s is Type %(type)s", map[string]interface{}{"name": "Gotext", "type": "struct"})

func Printf added in v1.5.0

func Printf(str string, vars ...interface{}) string

Printf applies text formatting only when needed to parse variables.

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.5.0

func SimplifiedLocale(lang string) string

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.5.0

func Sprintf(format string, params map[string]interface{}) string

Sprintf support named format

Sprintf("%(name)s is Type %(type)s", map[string]interface{}{"name": "Gotext", "type": "struct"})

Types

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
}

func NewLocale

func NewLocale(p, l string) *Locale

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

func (l *Locale) AddDomain(dom string)

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.5.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

func (l *Locale) Get(str string, vars ...interface{}) string

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

func (l *Locale) GetC(str, ctx string, vars ...interface{}) string

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) GetCE added in v1.5.0

func (l *Locale) GetCE(str, ctx string, vars ...interface{}) (string, bool)

GetCE 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. The second return value is true iff the string was found.

func (*Locale) GetD

func (l *Locale) GetD(dom, str string, vars ...interface{}) string

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

func (l *Locale) GetDC(dom, str, ctx string, vars ...interface{}) string

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) GetDCE added in v1.5.0

func (l *Locale) GetDCE(dom, str, ctx string, vars ...interface{}) (string, bool)

GetDCE 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) GetDE added in v1.5.0

func (l *Locale) GetDE(dom, str string, vars ...interface{}) (string, bool)

GetDE 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. The second return value is true iff the string was found.

func (*Locale) GetDomain added in v1.5.0

func (l *Locale) GetDomain() string

GetDomain is the domain getter for Locale configuration

func (*Locale) GetE added in v1.5.0

func (l *Locale) GetE(str string, vars ...interface{}) (string, bool)

GetE 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. The second return value is true iff the string was found.

func (*Locale) GetN

func (l *Locale) GetN(str, plural string, n int, vars ...interface{}) string

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

func (l *Locale) GetNC(str, plural string, n int, ctx string, vars ...interface{}) string

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) GetNCE added in v1.5.0

func (l *Locale) GetNCE(str, plural string, n int, ctx string, vars ...interface{}) (string, bool)

GetNCE 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. The second return value is true iff the string was found.

func (*Locale) GetND

func (l *Locale) GetND(dom, str, plural string, n int, vars ...interface{}) string

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

func (l *Locale) GetNDC(dom, str, plural string, n int, ctx string, vars ...interface{}) string

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) GetNDCE added in v1.5.0

func (l *Locale) GetNDCE(dom, str, plural string, n int, ctx string, vars ...interface{}) (string, bool)

GetNDCE 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. The second return value is true iff the string was found.

func (*Locale) GetNDE added in v1.5.0

func (l *Locale) GetNDE(dom, str, plural string, n int, vars ...interface{}) (string, bool)

GetNDE 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. The second return value is true iff the string was found.

func (*Locale) GetNE added in v1.5.0

func (l *Locale) GetNE(str, plural string, n int, vars ...interface{}) (string, bool)

GetNE 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. The second return value is true iff the string was found.

func (*Locale) MarshalBinary added in v1.5.0

func (l *Locale) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding BinaryMarshaler interface

func (*Locale) SetDomain added in v1.5.0

func (l *Locale) SetDomain(dom string)

SetDomain sets the name for the domain to be used.

func (*Locale) UnmarshalBinary added in v1.5.0

func (l *Locale) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding BinaryUnmarshaler interface

type LocaleEncoding added in v1.5.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.5.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"
	"git.deineagentur.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.5.0

func (mo *Mo) Get(str string, vars ...interface{}) string

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.5.0

func (mo *Mo) GetC(str, ctx string, vars ...interface{}) string

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) GetCE added in v1.5.0

func (mo *Mo) GetCE(str, ctx string, vars ...interface{}) (string, bool)

GetCE 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. The second return value is true iff the string was found.

func (*Mo) GetE added in v1.5.0

func (mo *Mo) GetE(str string, vars ...interface{}) (string, bool)

GetE 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. The second return value is true iff the string was found.

func (*Mo) GetN added in v1.5.0

func (mo *Mo) GetN(str, plural string, n int, vars ...interface{}) string

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.5.0

func (mo *Mo) GetNC(str, plural string, n int, ctx string, vars ...interface{}) string

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) GetNCE added in v1.5.0

func (mo *Mo) GetNCE(str, plural string, n int, ctx string, vars ...interface{}) (string, bool)

GetNCE 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. The second return value is true iff the string was found.

func (*Mo) GetNE added in v1.5.0

func (mo *Mo) GetNE(str, plural string, n int, vars ...interface{}) (string, bool)

GetNE 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. The second return value is true iff the string was found.

func (*Mo) MarshalBinary added in v1.5.0

func (mo *Mo) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler interface

func (*Mo) Parse added in v1.5.0

func (mo *Mo) Parse(buf []byte)

Parse loads the translations specified in the provided string (str)

func (*Mo) ParseFile added in v1.5.0

func (mo *Mo) ParseFile(f string)

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.5.0

func (mo *Mo) UnmarshalBinary(data []byte) error

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"
	"git.deineagentur.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

func (po *Po) Get(str string, vars ...interface{}) string

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

func (po *Po) GetC(str, ctx string, vars ...interface{}) string

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) GetCE added in v1.5.0

func (po *Po) GetCE(str, ctx string, vars ...interface{}) (string, bool)

GetCE 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. The second return value is true iff the string was found.

func (*Po) GetE added in v1.5.0

func (po *Po) GetE(str string, vars ...interface{}) (string, bool)

Get Eretrieves the corresponding Translation for the given string. Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax. The second return value is true iff the string was found.

func (*Po) GetN

func (po *Po) GetN(str, plural string, n int, vars ...interface{}) string

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

func (po *Po) GetNC(str, plural string, n int, ctx string, vars ...interface{}) string

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) GetNCE added in v1.5.0

func (po *Po) GetNCE(str, plural string, n int, ctx string, vars ...interface{}) (string, bool)

GetNCE 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. The second return value is true iff the string was found.

func (*Po) GetNE added in v1.5.0

func (po *Po) GetNE(str, plural string, n int, vars ...interface{}) (string, bool)

GetN Eretrieves 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. The second return value is true iff the string was found.

func (*Po) MarshalBinary added in v1.5.0

func (po *Po) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler interface

func (*Po) Parse

func (po *Po) Parse(buf []byte)

Parse loads the translations specified in the provided string (str)

func (*Po) ParseFile

func (po *Po) ParseFile(f string)

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.5.0

func (po *Po) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler interface

type Translation added in v1.5.0

type Translation struct {
	ID       string
	PluralID string
	Trs      map[int]string
}

Translation is the struct for the Translations parsed via Po or Mo files and all coming parsers

func NewTranslation added in v1.5.0

func NewTranslation() *Translation

NewTranslation returns the Translation object and initialized it.

func (*Translation) Get added in v1.5.0

func (t *Translation) Get() string

Get returns the string of the translation

func (*Translation) GetE added in v1.5.0

func (t *Translation) GetE() (string, bool)

Get returns the string of the translation. The second return value is true iff the string was found.

func (*Translation) GetN added in v1.5.0

func (t *Translation) GetN(n int) string

GetN returns the string of the plural translation

func (*Translation) GetNE added in v1.5.0

func (t *Translation) GetNE(n int) (string, bool)

GetN returns the string of the plural translation. The second return value is true iff the string was found.

type Translator added in v1.5.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

	GetE(str string, vars ...interface{}) (string, bool)
	GetNE(str, plural string, n int, vars ...interface{}) (string, bool)
	GetCE(str, ctx string, vars ...interface{}) (string, bool)
	GetNCE(str, plural string, n int, ctx string, vars ...interface{}) (string, bool)

	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.5.0

func NewMoTranslator() Translator

NewMoTranslator creates a new Mo object with the Translator interface

func NewPoTranslator added in v1.5.0

func NewPoTranslator() Translator

NewPoTranslator creates a new Po object with the Translator interface

type TranslatorEncoding added in v1.5.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.5.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.

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL