i18n

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: Apache-2.0 Imports: 14 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 translates user-interface text.

Each language is one JSON catalog:

{
  "meta": {"locale": "es", "name": "Español", "english_name": "Spanish"},
  "messages": {
    "session.resumed": "Sesión {id} reanudada",
    "files.count.one": "{count} archivo",
    "files.count.other": "{count} archivos"
  }
}

Catalogs are embedded for the shipped languages and also loaded from extra directories (e.g. ~/.blitz/locales), so a language can be added, or a shipped translation corrected, without rebuilding. Lookups fall back along the locale chain (fr-CA → fr → en-US), so a partial catalog still works.

Placeholders are {name}; values are passed as name/value pairs:

i18n.T("session.resumed", "id", id)

Plurals use ".one"/".other" suffixed keys selected by N.

Index

Constants

View Source
const DefaultLocale = "en-US"

DefaultLocale is the source language; every key must exist in it.

View Source
const PseudoLocale = "en-XA"

PseudoLocale renders English text with accents and brackets, to spot text that was never moved into a catalog.

Variables

View Source
var ErrUnknownLocale = errors.New("unknown locale")

ErrUnknownLocale is returned when input can't be understood as a locale.

Functions

func CheckCatalog

func CheckCatalog(data []byte) error

CheckCatalog reports why data isn't a catalog a Bundle would load.

func N

func N(key string, count int, args ...any) string

N renders a plural with the active localizer.

func Placeholders

func Placeholders(msg string) []string

Placeholders returns the {names} used in msg.

func ReplyInstruction

func ReplyInstruction(l *Localizer) string

ReplyInstruction tells the model to answer in the user's language; empty for English.

func SetCurrent

func SetCurrent(l *Localizer)

SetCurrent sets the localizer used by T and N.

func T

func T(key string, args ...any) string

T renders key with the active localizer.

Types

type Bundle

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

Bundle holds every loaded catalog.

func Default

func Default() *Bundle

Default returns the bundle of embedded catalogs.

func NewBundle

func NewBundle(dirs ...string) (*Bundle, []error)

NewBundle loads the embedded catalogs, then catalogs in dirs (later files add keys and override earlier translations). Problems with external files are returned but don't stop loading.

func Setup added in v0.3.1

func Setup(dir, locale string, warn func(string)) *Bundle

Setup loads the catalogs (the built-in ones, and the files in dir) and makes locale ("" for DefaultLocale) the current language, telling warn about catalogs that don't load and a locale that isn't a language code.

func (*Bundle) Available

func (b *Bundle) Available() []Meta

Available lists catalogs sorted by locale.

func (*Bundle) Catalog

func (b *Bundle) Catalog(tag string) (*Catalog, bool)

Catalog returns the catalog for exactly tag, if loaded.

func (*Bundle) Localizer

func (b *Bundle) Localizer(tag language.Tag) *Localizer

Localizer builds a localizer for tag, falling back through its parents and finally the default locale.

func (*Bundle) Missing

func (b *Bundle) Missing(tag string) []string

Missing lists default-locale keys the catalog doesn't translate.

func (*Bundle) Problems

func (b *Bundle) Problems(tag string) []string

Problems reports translation issues in catalog tag relative to the default: keys the default doesn't have, and placeholders that don't match.

func (*Bundle) Resolve

func (b *Bundle) Resolve(input string) (language.Tag, error)

Resolve turns user input into a locale tag. It accepts BCP 47 tags in any case with "-" or "_" ("es", "es-ES", "ES_es"), tolerates an invalid region by keeping the language ("ES-sp" → "es"), and matches catalog names in English or the language itself ("spanish", "español").

type Catalog

type Catalog struct {
	Meta     Meta              `json:"meta"`
	Messages map[string]string `json:"messages"`
	// contains filtered or unexported fields
}

Catalog is one language's messages.

type Localizer

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

Localizer renders messages for one locale.

func Current

func Current() *Localizer

Current returns the active localizer (English until SetCurrent).

func (*Localizer) HasCatalog

func (l *Localizer) HasCatalog() bool

HasCatalog reports whether any non-default catalog serves this locale.

func (*Localizer) IsEnglish

func (l *Localizer) IsEnglish() bool

IsEnglish reports whether the locale's language is English.

func (*Localizer) LanguageName

func (l *Localizer) LanguageName() string

LanguageName is the locale's English display name ("Spanish").

func (*Localizer) Lookup

func (l *Localizer) Lookup(key string) (string, bool)

Lookup returns the raw message for key and whether it was found.

func (*Localizer) N

func (l *Localizer) N(key string, count int, args ...any) string

N renders the plural form of key for count; count is also available as {count}.

func (*Localizer) NativeName

func (l *Localizer) NativeName() string

NativeName is the locale's name in its own language ("español").

func (*Localizer) T

func (l *Localizer) T(key string, args ...any) string

T renders key with name/value argument pairs. Missing keys render as the key itself so the problem is visible rather than silent.

func (*Localizer) Tag

func (l *Localizer) Tag() language.Tag

Tag returns the requested locale.

type Meta

type Meta struct {
	Locale      string `json:"locale"`
	Name        string `json:"name"`         // in the language itself
	EnglishName string `json:"english_name"` // for users who can't read it
}

Meta describes a catalog.

Jump to

Keyboard shortcuts

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