i18n

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MPL-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package i18n provides dependency-free typed message catalog helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(message string, vars map[string]string) string

Format replaces literal {name} placeholders with values.

Types

type Bundle

type Bundle[K comparable] struct {
	DefaultLocale string
	Catalogs      map[string]Catalog[K]
}

Bundle stores catalogs by locale code.

func NewBundle

func NewBundle[K comparable](defaultLocale string, catalogs map[string]Catalog[K]) Bundle[K]

NewBundle creates a catalog bundle with defensive copies.

func (Bundle[K]) Catalog

func (bundle Bundle[K]) Catalog(locale string) (Catalog[K], bool)

Catalog returns the requested locale catalog or the default catalog.

type Catalog

type Catalog[K comparable] struct {
	Locale   string
	Messages map[K]string
}

Catalog maps typed keys to localized messages for one locale.

func NewCatalog

func NewCatalog[K comparable](locale string, messages map[K]string) Catalog[K]

NewCatalog creates a catalog with a defensive message copy.

func (Catalog[K]) Format

func (catalog Catalog[K]) Format(key K, vars map[string]string) (string, bool)

Format replaces {name} placeholders in the keyed message. Missing variables are left intact so callers can detect incomplete data in tests.

func (Catalog[K]) Message

func (catalog Catalog[K]) Message(key K) (string, bool)

Message returns the localized message for key.

func (Catalog[K]) MissingKeys

func (catalog Catalog[K]) MissingKeys(keys []K) []K

MissingKeys reports typed keys absent from the catalog.

func (Catalog[K]) Must

func (catalog Catalog[K]) Must(key K) string

Must returns the localized message for key or panics when the key is missing. It is intended for build-time data functions and tests where missing keys should fail fast.

func (Catalog[K]) MustFormat

func (catalog Catalog[K]) MustFormat(key K, vars map[string]string) string

MustFormat is the panic-on-missing-key variant of Format.

Jump to

Keyboard shortcuts

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