i18n

package
v0.0.0-...-66e4c3a Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data map[string]string

Data should be used to pass your template data

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

func New(config config.I18n) (*Service, error)

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

func (m *Service) ParseAcceptLanguage(lang string) language.Tag

ParseAcceptLanguage takes the value of the Accept-Language header and returns the best matched language using the matcher.

func (*Service) ParseLang

func (m *Service) ParseLang(lang string) language.Tag

ParseLang parses the string as language tag and returns the best matched language using the matcher.

func (*Service) Tags

func (m *Service) Tags() []language.Tag

Tags returns the parsed and priority ordered []language.Tag (your config.DefaultLanguage will be on position 0)

func (*Service) Translate

func (m *Service) Translate(key string, lang language.Tag, data ...Data) string

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

func (m *Service) TranslateMaybe(key string, lang language.Tag, data ...Data) (string, error)

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

Jump to

Keyboard shortcuts

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