i18n

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package i18n holds the languages Exhale speaks and the translations of what chat and the service say (ADR 0023).

Texts are written in English where they are used; English is the key of every translation. A language is registered in languages.json (read by the web too) and translated in locales/<code>.json, a flat JSON object from the English text to its translation. An entry may be left empty (""): the text falls back to English. A text counted with a number may give one translation per plural category instead of a string, for example {"one": "…", "few": "…", "many": "…", "other": "…"}.

docs/guides/translating.md explains how to add a language.

Index

Constants

View Source
const (
	Zero  = "zero"
	One   = "one"
	Two   = "two"
	Few   = "few"
	Many  = "many"
	Other = "other"
)

Plural categories, named as CLDR names them.

View Source
const Source = "en"

Source is the language the texts are written in.

Variables

This section is empty.

Functions

func Catalog

func Catalog(lang string) map[string]Entry

Catalog returns the translations of a language, keyed by the English text (nil for English). The map is shared: do not change it.

func HasPluralRule

func HasPluralRule(lang string) bool

HasPluralRule reports whether the table above knows the language.

func Match

func Match(codes ...string) string

Match returns the first registered language among codes such as "pt-BR" or "es" (what a browser or a chat platform reports), or English.

func Name

func Name(code string) string

Name returns the own name of a registered language, or the code.

func Plural

func Plural(lang string, n int, one, other string) string

Plural picks the text for count n: the English one or other form, translated by the plural category of n in the language. When the other form has a translation per category, that wins.

func PluralCategories

func PluralCategories(lang string) []string

PluralCategories lists the categories a language uses, "other" last.

func PluralCategory

func PluralCategory(lang string, n int) string

PluralCategory returns the plural category of n in a language. A language without a rule counts like English.

func Supported

func Supported(code string) bool

Supported reports whether a language is registered.

func Translate

func Translate(lang, text string) string

Translate returns the translation of an English text, or the text itself. A text with plural forms gives its "other" form.

Types

type Entry

type Entry struct {
	Text  string
	Forms map[string]string
}

Entry is the translation of one text: a string, or one string per plural category.

func (Entry) Empty

func (e Entry) Empty() bool

Empty reports whether the entry has no translation yet.

func (Entry) MarshalJSON

func (e Entry) MarshalJSON() ([]byte, error)

MarshalJSON writes what UnmarshalJSON reads.

func (*Entry) UnmarshalJSON

func (e *Entry) UnmarshalJSON(b []byte) error

UnmarshalJSON reads a string or an object of plural forms.

type Language

type Language struct {
	// Code is the ISO 639-1 code: "es", "pt".
	Code string `json:"code"`
	// Name is the language's own name: "Español", "Português".
	Name string `json:"name"`
	// Complete languages must translate every text (a test fails
	// otherwise); an incomplete one ships with English in the gaps.
	Complete bool `json:"complete,omitempty"`
}

Language is a registered language.

func Languages

func Languages() []Language

Languages returns the registered languages, English first.

Jump to

Keyboard shortcuts

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