i18n

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: AGPL-3.0 Imports: 11 Imported by: 1 module, 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 reads lich's translation catalogs: locales/<tag>/<namespace>.json, one tree per namespace, looked up by a dotted key ("settings.title"). A message holds {name} placeholders or is an object of CLDR plural forms picked by an int "count". lich's window reads its own catalogs with this package, and an extension reads its own with Bundle.

Index

Constants

View Source
const LocalesDir = "locales"

LocalesDir is the folder of an extension's catalogs, beside its manifest.

View Source
const (
	ManifestNameKey = "extension.manifest.name"
)

The keys lich reads an extension's manifest strings from, in the namespace "extension" (locales/<tag>/extension.json). A key a catalog lacks reads the manifest's own text.

View Source
const Source = "en"

Source is the language catalogs are written in first, and the one a message missing from a translation falls back to.

Variables

View Source
var ErrNoCount = errors.New("is a plural and needs an int count")

ErrNoCount is a plural asked for without an int count.

View Source
var Placeholder = regexp.MustCompile(`\{(\w+)\}`)

Placeholder matches a {name} in a message.

Functions

func Fill

func Fill(text string, args Args) (string, error)

Fill puts args' values in text's placeholders. A placeholder without a value is left as it is and named in the error.

func Match

func Match(tag string, among []string) string

Match is the tag of among that tag reads: an exact match, ignoring case, then a language match (any Portuguese reads pt-BR), then "".

func PluralForm

func PluralForm(tag string, n int) string

PluralForm is the CLDR plural category of an integer count in the language tag, any case. Only the forms lich's catalogs use are told apart: Spanish's "many" (1000000 and up) has no message of its own and reads "other" either way, and a language lich has no rule for reads "other" always.

func SurfaceTitleKey

func SurfaceTitleKey(id string) string

SurfaceTitleKey is the key of surface id's title.

func TagFromEnv

func TagFromEnv(getenv func(string) string) string

TagFromEnv is the POSIX locale the environment names, in the order glibc reads it for messages (LC_ALL, LC_MESSAGES, LANG), as a BCP 47 tag: pt_BR.UTF-8@euro reads pt-BR. C and POSIX name no language.

Types

type Args

type Args map[string]any

Args are a message's placeholder values by name; "count" picks a plural's form and must be an int.

type Bundle

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

Bundle is every catalog an extension ships, by tag as its folder names it.

func Open

func Open(fsys fs.FS) (*Bundle, error)

Open reads locales/<tag>/<namespace>.json under fsys. A missing locales folder is an empty bundle, which answers every key with itself.

func OpenDir

func OpenDir(dir string) (*Bundle, error)

OpenDir is Open on the folder dir, an extension's own: the folder its executable runs in.

func (*Bundle) In

func (b *Bundle) In(tag, key string, args ...Args) string

In is the message under key in the language tag, its placeholders filled from args: English when the tag's catalog lacks it, and the key itself when no catalog has it. A plural without an int count reads its "other" form, and a placeholder without a value stays as written.

func (*Bundle) Lookup

func (b *Bundle) Lookup(tag, key string) (Message, string, bool)

Lookup is the message under key in the catalog tag reads, then in English, with the tag whose catalog had it.

func (*Bundle) Tags

func (b *Bundle) Tags() []string

Tags are the languages the bundle has a catalog for, sorted.

type Catalog

type Catalog map[string]Message

Catalog is one locale's messages by dotted key, the namespace first.

func Load

func Load(fsys fs.FS, dir string) (Catalog, error)

Load reads every <namespace>.json in the directory dir of fsys into one catalog. Any other file is left alone.

type Language

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

Language is the tag lich shows its interface in, as an extension learns it: the environment at start, then the language field of every surface.opened and every language message, which the extension passes to Set. It is safe to use from any goroutine.

func FromEnv

func FromEnv() *Language

FromEnv starts from the variable lich starts an extension with, English when it is unset.

func (*Language) Set

func (l *Language) Set(tag string)

Set records tag; an empty one changes nothing.

func (*Language) Tag

func (l *Language) Tag() string

Tag is the language last set, English before any.

type Message

type Message struct {
	Text   string
	Plural map[string]string
}

Message is a leaf of a catalog: a text, or a plural's CLDR forms.

func (Message) Template

func (m Message) Template(tag string, args Args) (string, error)

Template is m's text in the language tag: a plural's form for args' count, or "other" when the catalog lacks the form the rule picks.

Jump to

Keyboard shortcuts

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