Documentation ¶
Overview ¶
Package localised provides a simple utility for storing and accessing a piece of data with one or more translations.
Index ¶
- func GetLocale() []string
- type Localised
- func (l *Localised[T]) Add(loc string, val T) *Localised[T]
- func (l *Localised[T]) Get() (T, error)
- func (l *Localised[T]) GetFor(locs ...string) (T, error)
- func (l *Localised[T]) GetForTag(tags ...language.Tag) (T, error)
- func (l *Localised[T]) GetLocale() (string, error)
- func (l *Localised[T]) GetLocaleFor(locs ...string) (string, error)
- func (l *Localised[T]) GetLocaleForTag(tags ...language.Tag) (string, error)
- func (l *Localised[T]) MarshalJSON() ([]byte, error)
- func (l *Localised[T]) MustGet() T
- func (l *Localised[T]) MustGetFor(locs ...string) T
- func (l *Localised[T]) MustGetForTag(tags ...language.Tag) T
- func (l *Localised[T]) MustGetLocale() string
- func (l *Localised[T]) MustGetLocaleFor(locs ...string) string
- func (l *Localised[T]) MustGetLocaleForTag(tags ...language.Tag) string
- func (l *Localised[T]) Prepare() error
- func (l *Localised[T]) Set(loc string, val T) *Localised[T]
- func (l *Localised[T]) UnmarshalJSON(data []byte) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetLocale ¶
func GetLocale() []string
Returns the current system locale; may return multiple values on platforms that support specifying preferences.
On Unix-Like systems it reads from the environment variables LANGUAGE, LC_ALL, LC_MESSAGES, and LANG in that order, returning the first non-empty value. Returns "en" if all are empty, or if LC_ALL is set to "C" or "POSIX".
Types ¶
type Localised ¶
type Localised[T any] struct { // contains filtered or unexported fields }
Represents a piece of data with one or more translations; intended for self-contained storage in a larger document.
Language selection is based on golang.org/x/text/language, which attempts to find the most appropriate match for the available translations. If no match is found, the default locale's translation is used.
The origin of the default locale depends on the origin of the object. When marshalling to/from JSON, the default is explicitly marked; when creating a Localised from scratch, the default is the first translation added to the object.
Implements a custom json.Unmarshaler to validate that the resulting object contains at least one translation.
For the following collection of strings:
l := New("en", "Hello") l.Add("es", "Hola") l.Add("fr", "Bonjour")
The JSON representation would be:
{ "def": "en", "map": { "en": "Hello", "es": "Hola", "fr": "Bonjour", } }
For instances of Localised that may be accessed from multiple threads, it is recommended to call Prepare manually while the object is still owned by a single thread. For objects created by json.Unmarshal this is done automatically.
func (*Localised[T]) GetLocaleFor ¶
Returns the name of the best match for the given locale.
func (*Localised[T]) GetLocaleForTag ¶
Returns the name of the best match for the given language tag.
func (*Localised[T]) MarshalJSON ¶
func (*Localised[T]) MustGet ¶
func (l *Localised[T]) MustGet() T
Alternate version of Get that panics on error.
func (*Localised[T]) MustGetFor ¶
Alternate version of GetFor that panics on error.
func (*Localised[T]) MustGetForTag ¶
Alternate version of GetForTag that panics on error.
func (*Localised[T]) MustGetLocale ¶
Alternate version of GetLocale that panics on error.
func (*Localised[T]) MustGetLocaleFor ¶
Alternate version of GetLocaleFor that panics on error.
func (*Localised[T]) MustGetLocaleForTag ¶
Alternate version of GetLocaleForTag that panics on error.
func (*Localised[T]) Prepare ¶
Prepares a populated object for use.
Called automatically by json.Unmarshal, or on the first access to the object if it was populated manually. This method usually does not need to be called manually, but may be advisable for manually populated objects that will be accessed from multiple threads.
func (*Localised[T]) Set ¶
Initialise an existing Localised with the given locale as the default translation.