Documentation ¶
Index ¶
- type Data
- type Service
- func (m *Service) ParseAcceptLanguage(lang string) language.Tag
- func (m *Service) ParseLang(lang string) language.Tag
- func (m *Service) Tags() []language.Tag
- func (m *Service) Translate(key string, lang language.Tag, data ...Data) string
- func (m *Service) TranslateMaybe(key string, lang language.Tag, data ...Data) (string, error)
- func (m *Service) TranslatePlural(cldrKey string, count interface{}, lang language.Tag, data ...Data) string
- func (m *Service) TranslatePluralMaybe(cldrKey string, count interface{}, lang language.Tag, data ...Data) (string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is your convenience object to call Translate/TranslatePlural and match languages according to your loaded translation bundle and its supported languages/locales.
func New ¶
New returns a new Service struct holding bundle and matcher with the settings of the given config
Note that Service is typically created and owned by the api.Server (use it via s.I18n)
func (*Service) ParseAcceptLanguage ¶
ParseAcceptLanguage takes the value of the Accept-Language header and returns the best matched language using the matcher.
func (*Service) ParseLang ¶
ParseLang parses the string as language tag and returns the best matched language using the matcher.
func (*Service) Tags ¶
Tags returns the parsed and priority ordered []language.Tag (your config.DefaultLanguage will be on position 0)
func (*Service) Translate ¶
Translate your key into a localized string.
Translate makes a lookup for the key in the current bundle with the specified language. If a language translation is not available the default language will be used. Additional data for templated strings can be passed as key value pairs with by passing an optional data map.
Translate will not fail if a template value is missing "<no value>" will be inserted instead. Translate will also not fail if the key is not present. "{{key}}" will be returned instead.
func (*Service) TranslateMaybe ¶
TranslateMaybe has the same sematics as Translate with the following exceptions: It exposes encountered errors (does not automatically log this error) and encountered errors may result in an empty "" string!
This method may be useful for conditional translation rendering (if key is available, use that, else...).
func (*Service) TranslatePlural ¶
func (m *Service) TranslatePlural(cldrKey string, count interface{}, lang language.Tag, data ...Data) string
TranslatePlural translates a pluralized cldrKey into a localized string.
TranslatePlural makes a lookup for the cldrKey (a base key holding CLDR keys like "one" and "other") in the current bundle with the specified language. This function should be used to conditionally show the pluralized form, controlled by the count param and according to the CLDR rules.
Note that English and German only support .one and .other CLDR plural rules. See https://cldr.unicode.org/index/cldr-spec/plural-rules and https://www.unicode.org/cldr/cldr-aux/charts/28/supplemental/language_plural_rules.html
If a language translation is not available the default language will be used. Additional data for templated strings can be passed as key value pairs with by passing an optional data map. The count param is automatically injected into this data map as stringified {{.Count}} and may be overwritten.
TranslatePlural will not fail if a template value is missing "<no value>" will be inserted instead. TranslatePlural will also not fail if the cldrKey is not present. "{{cldrKey}} (count={{count}})" will be returned instead.
func (*Service) TranslatePluralMaybe ¶
func (m *Service) TranslatePluralMaybe(cldrKey string, count interface{}, lang language.Tag, data ...Data) (string, error)
TranslatePluralMaybe uses the same sematics as TranslatePlural with the following exceptions: It exposes encountered errors (does not automatically log this error) and encountered errors may result in an empty "" string!
This method may be useful for conditional plural translation rendering (if key is available, use that, else...).