i18n

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT, MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package i18n is gettext in pure Go: GNU .mo and .po catalogs with plural forms and message contexts, locale resolution from the environment, and catalog lookup in a file system. A Catalog plugs into lys-ui's widget.SetMessageCatalog, so a toolkit's built-in strings and an app's own translate through one table.

cat, err := i18n.LoadSystem("myapp", i18n.Locales())
label := cat.Get("Open")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Locales

func Locales() []string

Locales returns the locale candidates gettext would try, most specific first, from LANGUAGE, LC_ALL, LC_MESSAGES and LANG: for "nb_NO.UTF-8@euro" that is nb_NO@euro, nb_NO, nb@euro, nb. The C and POSIX locales mean no translation.

Types

type Catalog

type Catalog struct {
	// contains filtered or unexported fields
}

Catalog is one language's translations. The zero value translates nothing: every lookup returns its English input.

func FromMap

func FromMap(m map[string]string) *Catalog

FromMap is a catalog from a plain English-to-translation map, for apps that keep a handful of strings in code. It has no plural forms or contexts.

func Load

func Load(fsys fs.FS, domain string, locales []string) (*Catalog, error)

Load finds domain's catalog in fsys for the first locale that has one, as <locale>/LC_MESSAGES/<domain>.mo or .po. An empty catalog (the identity) and no error mean no locale has one; a catalog that exists but does not parse is an error.

func LoadSystem

func LoadSystem(domain string, locales []string) (*Catalog, error)

LoadSystem is Load over the system's locale directories: each directory of xdg.Env.DataSearchPath's locale directory, first match wins.

func ParseMO

func ParseMO(data []byte) (*Catalog, error)

ParseMO parses a GNU .mo catalog, either byte order.

func ParsePO

func ParsePO(data []byte) (*Catalog, error)

ParsePO parses a .po catalog. Fuzzy entries are skipped, as msgfmt skips them; the header is kept for its Plural-Forms.

func (*Catalog) Get

func (c *Catalog) Get(id string) string

Get translates id, or returns id untranslated.

func (*Catalog) GetCtx

func (c *Catalog) GetCtx(ctx, id string) string

GetCtx translates id within ctx (msgctxt), or returns id.

func (*Catalog) GetN

func (c *Catalog) GetN(singular, plural string, n int) string

GetN translates the plural pair for n, choosing the form by the catalog's Plural-Forms rule, or returns singular when n is 1 and plural otherwise.

func (*Catalog) GetNCtx

func (c *Catalog) GetNCtx(ctx, singular, plural string, n int) string

GetNCtx is GetN within ctx.

Jump to

Keyboard shortcuts

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