localised

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: ISC Imports: 5 Imported by: 0

README

go-localised

go reference pipeline status coverage report

A simple utility for storing and accessing a piece of data with one or more translations.

Usage

package main

import (
  "fmt"

  "gitlab.com/cptpackrat/go-localised"
)

var title = localised.
  New("en", "Release Notes").
  Add("es", "Notas de lanzamiento")

var notes = localised.
  New("en", []string{
    "Fixed a bug.",
    "Fixed another bug.",
    "Added a feature.",
  }).
  Add("es", []string{
    "Se corrigió un error.",
    "Se corrigió otro error.",
    "Se agregó una función.",
  })

func main() {
  fmt.Printf("%s:\n", title.MustGet())
  for _, note := range notes.MustGet() {
    fmt.Printf(" - %s\n", note)
  }
}
$ go build -o app
$ LANG=en ./app
Release Notes:
 - Fixed a bug.
 - Fixed another bug.
 - Added a feature.
$ LANG=es ./app
Notas de lanzamiento:
 - Se corrigió un error.
 - Se corrigió otro error.
 - Se agregó una función.

Supports marshalling to/from JSON. For the following collection of strings:

localised.
  New("en", "Hello").
  Add("es", "Hola").
  Add("fr", "Bonjour")

The JSON representation would be:

{
  "def": "en",
  "map": {
    "en": "Hello",
    "es": "Hola",
    "fr": "Bonjour",
  }
}

Documentation

Overview

Package localised provides a simple utility for storing and accessing a piece of data with one or more translations.

Index

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 New

func New[T any](loc string, val T) *Localised[T]

Returns a new Localised with the given locale as the default translation.

func (*Localised[T]) Add

func (l *Localised[T]) Add(loc string, val T) *Localised[T]

Adds a translation for the given locale.

func (*Localised[T]) Get

func (l *Localised[T]) Get() (T, error)

Returns the best match for the current system locale.

func (*Localised[T]) GetFor

func (l *Localised[T]) GetFor(locs ...string) (T, error)

Returns the best match for the given locale.

func (*Localised[T]) GetForTag

func (l *Localised[T]) GetForTag(tags ...language.Tag) (T, error)

Returns the best match for the given language tag.

func (*Localised[T]) GetLocale

func (l *Localised[T]) GetLocale() (string, error)

Returns the name of the best match for the current system locale.

func (*Localised[T]) GetLocaleFor

func (l *Localised[T]) GetLocaleFor(locs ...string) (string, error)

Returns the name of the best match for the given locale.

func (*Localised[T]) GetLocaleForTag

func (l *Localised[T]) GetLocaleForTag(tags ...language.Tag) (string, error)

Returns the name of the best match for the given language tag.

func (*Localised[T]) MarshalJSON

func (l *Localised[T]) MarshalJSON() ([]byte, error)

func (*Localised[T]) MustGet

func (l *Localised[T]) MustGet() T

Alternate version of Get that panics on error.

func (*Localised[T]) MustGetFor

func (l *Localised[T]) MustGetFor(locs ...string) T

Alternate version of GetFor that panics on error.

func (*Localised[T]) MustGetForTag

func (l *Localised[T]) MustGetForTag(tags ...language.Tag) T

Alternate version of GetForTag that panics on error.

func (*Localised[T]) MustGetLocale

func (l *Localised[T]) MustGetLocale() string

Alternate version of GetLocale that panics on error.

func (*Localised[T]) MustGetLocaleFor

func (l *Localised[T]) MustGetLocaleFor(locs ...string) string

Alternate version of GetLocaleFor that panics on error.

func (*Localised[T]) MustGetLocaleForTag

func (l *Localised[T]) MustGetLocaleForTag(tags ...language.Tag) string

Alternate version of GetLocaleForTag that panics on error.

func (*Localised[T]) Prepare

func (l *Localised[T]) Prepare() error

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

func (l *Localised[T]) Set(loc string, val T) *Localised[T]

Initialise an existing Localised with the given locale as the default translation.

func (*Localised[T]) UnmarshalJSON

func (l *Localised[T]) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

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