localize

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MPL-2.0 Imports: 14 Imported by: 3 modules, 2 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

SPDX-License-Identifier: MPL-2.0 Package localize loads immutable, data-only message catalogs. Products own source selection, preference persistence and explicit replacement on reload. It imports neither the Admin shell nor Core and performs no lookup-time I/O.

SPDX-License-Identifier: MPL-2.0

Index

Constants

View Source
const (
	MaxSources        = 32
	MaxFiles          = 128
	MaxEntries        = 4096
	MaxFileBytes      = 256 << 10
	MaxTotalBytes     = 4 << 20
	MaxMessages       = 8192
	MaxLocales        = 32
	MaxDomains        = 64
	MaxMessageBytes   = 8 << 10
	MaxOutputBytes    = 32 << 10
	MaxParameters     = 32
	MaxParameterBytes = 8 << 10
)

Fixed limits bound startup work, retained catalogs and formatted output.

Variables

View Source
var (
	ErrCatalog    = errors.New("invalid localization catalog")
	ErrLocale     = errors.New("unsupported localization locale")
	ErrMessage    = errors.New("unknown localization message")
	ErrParameters = errors.New("invalid localization parameters")
	ErrLimit      = errors.New("localization limit exceeded")
)

Functions

This section is empty.

Types

type Catalog

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

Catalog is immutable after Load and safe for concurrent readers. There is no process-global locale and no automatic file watcher. Load a replacement before publishing it through the product's own instance/reload lifecycle.

func Load

func Load(sources ...fs.FS) (*Catalog, error)

Load reads all .json files below each source root once. Use fs.Sub for embedded Module roots and a product-selected fs.FS for installed language packs. Sources add domains/messages, not ordered overrides: duplicate identities are errors. Symlinks, nonregular JSON files, excessive depth and unsupported directory readers are rejected. The owner must also trust the supplied FS root itself.

func (*Catalog) For

func (c *Catalog) For(tag string) (*Localizer, error)

For selects one explicitly loaded locale. Empty selects English. Unsupported input is an error rather than a silent change to the product's preference.

func (*Catalog) Locales

func (c *Catalog) Locales() []Locale

Locales returns a sorted defensive copy of loaded metadata. Products decide which loaded languages they expose and how they persist a user's selection.

type Forms

type Forms struct {
	Zero  string `json:"zero,omitempty"`
	One   string `json:"one,omitempty"`
	Two   string `json:"two,omitempty"`
	Few   string `json:"few,omitempty"`
	Many  string `json:"many,omitempty"`
	Other string `json:"other"`
}

Forms contains complete messages, not HTML or executable templates. Other is required; absent categories use Other. Parameters use {Name}; {{ and }} escape literal braces. Plural uses CLDR cardinal categories for integral quantities.

type Locale

type Locale struct {
	Tag       string `json:"tag"`
	Name      string `json:"name"`
	Direction string `json:"direction"`
}

Locale is presentation metadata, not a content, calendar or permission policy.

type Localizer

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

Localizer is an immutable per-selection view. It does not store preferences or alter the original content. Returned text remains untrusted display data and must use the output context's normal escaping, including html/template or DOM textContent. Never cast it to trusted HTML or execute it as a template/script.

func (*Localizer) Locale

func (l *Localizer) Locale() Locale

func (*Localizer) Messages

func (l *Localizer) Messages(domain string) (map[string]Message, error)

Messages returns a defensive, English-fallback-completed browser message table for one domain. It contains data-only patterns and the source locale of each complete message. Client plural selection must use Message.Locale, not necessarily the document locale; absent categories use Forms.Other. Serialize with normal encoding/json escaping and consume values through safe DOM text.

func (*Localizer) Plural

func (l *Localizer) Plural(domain, key string, count int64, params map[string]string) (string, error)

Plural selects a cardinal form for an integral count using maintained CLDR rules. It injects the decimal Count parameter without changing calendar, number/currency formatting or stored values. Params must not supply Count.

func (*Localizer) Text

func (l *Localizer) Text(domain, key string, params map[string]string) (string, error)

Text formats the complete Other message. Missing translations fall back as a whole message to English; unknown keys or missing parameters return errors. Valid extra parameters are accepted to allow a shared caller-owned parameter set. Values are strings, not executable templates or locale-formatting policy.

type Message

type Message struct {
	Locale string `json:"locale"`
	Forms  Forms  `json:"forms"`
}

Message is a defensive browser export. Locale is the message's source locale, which can differ from the selected locale after whole-message fallback.

Jump to

Keyboard shortcuts

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