Documentation ¶
Overview ¶
Package goyai provides a simple interface to support i18n for Go applications.
Index ¶
Constants ¶
const (
// Version of goyai package
Version = "0.2.0"
)
Variables ¶
var ( // ErrInvalidFileFormat indicates that the specified language file format is not supported. ErrInvalidFileFormat = errors.New("language file format is invalid or not supported") )
Functions ¶
This section is empty.
Types ¶
type Goi18n ¶
type Goi18n struct {
// contains filtered or unexported fields
}
Goi18n is the default I18n implementation from goyai.
func (*Goi18n) AvailableLocales ¶
func (i *Goi18n) AvailableLocales() []LocaleInfo
AvailableLocales implements I18n.AvailableLocales.
type I18n ¶
type I18n interface { // Localize returns a localized message. Params are optional, a param can be either a string, number, boolean or a LocalizeConfig instance. // Note: params can be mixed of strings, numbers and booleans but not LocalizeConfig instances. If there is one or more // LocalizeConfig instances are supplied, only the first one is used; all other params are ignored (including // strings/numbers/booleans and other LocalizeConfig instances). Localize(locale, msgId string, params ...interface{}) string // Localise is alias of Localize. // // Available since v0.2.0 Localise(locale, msgId string, params ...interface{}) string // AvailableLocales returns all defined locale configurations. AvailableLocales() []LocaleInfo }
I18n is the main interface of goyai package, offering APIs to render a message.
func BuildI18n ¶
func BuildI18n(opts I18nOptions) (I18n, error)
BuildI18n builds an I18n instance from message file(s) and returns it.
type I18nFileFormat ¶
type I18nFileFormat int
I18nFileFormat defines list of supported i18n configuration file formats.
const ( // Auto hints that the file format should be automatically detected. Auto I18nFileFormat = iota // Json hints that the file is JSON-encoded. Json // Yaml hints that the file is YAML-encoded. Yaml )
type I18nOptions ¶
type I18nOptions struct { // ConfigFileOrDir points to the configuration file or the directory where configuration files are located. ConfigFileOrDir string // DefaultLocale is the default locale to be used when non specified. DefaultLocale string // I18nFileFormat hints the format of configuration files. I18nFileFormat I18nFileFormat }
I18nOptions specifies options to build new I18n instances.
type LocaleInfo ¶
LocaleInfo captures info of a locale package.
type LocalizeConfig ¶
type LocalizeConfig struct { // TemplateData is used to transform the message's template. TemplateData map[string]interface{} // PluralCount determines which plural form of the message is used. PluralCount must be an integer or nil. // See Message for more information. // // Rule for picking plural form: // - if PluralCount is negative number, nil or not cast-able to integer, the "other" form is chosen. // - if PluralCount = 0, the "zero" form is chosen. // - if PluralCount = 1, one of "one"/"few"/"other" forms is chosen, priority is from left to right (e.g. "one" form has the highest priority, if absent, the next one is checked) // - if PluralCount = 2, one of "two"/"many"/"other" forms is chosen, priority is from left to right (e.g. "two" form has the highest priority, if absent, the next one is checked) // - if PluralCount > 2, one of "many"/"other" forms is chosen, priority is from left to right (e.g. "many" form has the highest priority, if absent, the next one is checked) PluralCount interface{} // DefaultMessage holds the default message where there is no localized one. DefaultMessage string }
LocalizeConfig configures how a message should be localised, used by function I18n.Localize.
type Message ¶
type Message struct { // Id is the message's unique identity. Id string // Description provides additional information about the message. Description string // Zero is the message's content for the CLDR plural form "zero". Zero string // One is the message's content for the CLDR plural form "one". One string // Two is the message's content for the CLDR plural form "two". Two string // Few is the message's content for the CLDR plural form "few". Few string // Many is the message's content for the CLDR plural form "many". Many string // Other is the message's content for the CLDR plural form "other". Other string }
Message represents a message that can be localized.
func ParseMessage ¶
ParseMessage parses data and returns a new Message instance.
data must be either a string, or a map[string]string. If data is a string, the Message is constructed with data is the value of the plural form "other".