Documentation
¶
Overview ¶
Package i18n provides dependency-free typed message catalog helpers.
Index ¶
- func Format(message string, vars map[string]string) string
- func FormatDate(locale string, value time.Time, style DateStyle) string
- func FormatNumber(locale string, value float64, options NumberFormatOptions) string
- func FormatPlural(locale string, count int, forms PluralForms, vars map[string]string) string
- func FormatTime(locale string, value time.Time, style TimeStyle) string
- type Bundle
- type BundleReport
- type Catalog
- func (catalog Catalog[K]) Format(key K, vars map[string]string) (string, bool)
- func (catalog Catalog[K]) Message(key K) (string, bool)
- func (catalog Catalog[K]) MissingKeys(keys []K) []K
- func (catalog Catalog[K]) Must(key K) string
- func (catalog Catalog[K]) MustFormat(key K, vars map[string]string) string
- func (catalog Catalog[K]) Template(required []MessageReference[K]) CatalogTemplate[K]
- type CatalogReport
- type CatalogTemplate
- type CatalogTemplateEntry
- type DateStyle
- type MessageReference
- type NumberFormatOptions
- type PluralCategory
- type PluralForms
- type TimeStyle
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatDate ¶ added in v0.12.0
FormatDate formats a date using the supported deterministic style.
func FormatNumber ¶ added in v0.12.0
func FormatNumber(locale string, value float64, options NumberFormatOptions) string
FormatNumber formats a number with deterministic locale separators. It is a bounded helper, not a CLDR replacement.
func FormatPlural ¶ added in v0.12.0
FormatPlural selects a plural form, injects {count}, and applies Format.
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]) Check ¶ added in v0.12.0
func (bundle Bundle[K]) Check(required []MessageReference[K]) BundleReport[K]
Check returns a deterministic completeness report for the required message references. Missing keys, unused keys, and missing default catalogs are reported without panicking so app tests can decide the failure policy.
func (Bundle[K]) Template ¶ added in v0.12.0
func (bundle Bundle[K]) Template(locale string, required []MessageReference[K]) CatalogTemplate[K]
Template returns a deterministic starter catalog for one locale. Existing values are used only when that exact locale is already present.
type BundleReport ¶ added in v0.12.0
type BundleReport[K comparable] struct { DefaultLocale string `json:"defaultLocale,omitempty"` Required []MessageReference[K] `json:"required,omitempty"` Catalogs []CatalogReport[K] `json:"catalogs,omitempty"` }
BundleReport summarizes catalog completeness across locales.
func (BundleReport[K]) Error ¶ added in v0.12.0
func (report BundleReport[K]) Error() string
Error renders the report as indented JSON for app-owned tests.
func (BundleReport[K]) OK ¶ added in v0.12.0
func (report BundleReport[K]) OK() bool
OK reports whether the bundle has no missing catalogs, missing keys, or unused keys for the checked required references.
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 ¶
Format replaces {name} placeholders in the keyed message. Missing variables are left intact so callers can detect incomplete data in tests.
func (Catalog[K]) MissingKeys ¶
func (catalog Catalog[K]) MissingKeys(keys []K) []K
MissingKeys reports typed keys absent from the catalog.
func (Catalog[K]) Must ¶
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 ¶
MustFormat is the panic-on-missing-key variant of Format.
func (Catalog[K]) Template ¶ added in v0.12.0
func (catalog Catalog[K]) Template(required []MessageReference[K]) CatalogTemplate[K]
Template returns a deterministic starter catalog for the required keys. Any existing catalog values are copied into the template entries.
type CatalogReport ¶ added in v0.12.0
type CatalogReport[K comparable] struct { Locale string `json:"locale,omitempty"` MissingCatalog bool `json:"missingCatalog,omitempty"` Missing []MessageReference[K] `json:"missing,omitempty"` Unused []K `json:"unused,omitempty"` }
CatalogReport is the deterministic completeness report for one locale.
type CatalogTemplate ¶ added in v0.12.0
type CatalogTemplate[K comparable] struct { Locale string `json:"locale"` Entries []CatalogTemplateEntry[K] `json:"entries"` }
CatalogTemplate is a deterministic locale-specific starter catalog.
type CatalogTemplateEntry ¶ added in v0.12.0
type CatalogTemplateEntry[K comparable] struct { Key K `json:"key"` Value string `json:"value"` Source string `json:"source,omitempty"` Line int `json:"line,omitempty"` Column int `json:"column,omitempty"` }
CatalogTemplateEntry is one deterministic entry in a catalog template.
type DateStyle ¶ added in v0.12.0
type DateStyle string
DateStyle selects a deterministic date format.
type MessageReference ¶ added in v0.12.0
type MessageReference[K comparable] struct { Key K `json:"key"` Source string `json:"source,omitempty"` Line int `json:"line,omitempty"` Column int `json:"column,omitempty"` }
MessageReference records one expected message key and, optionally, where it was declared or used. Apps can keep these references beside build helpers or generated extraction output and check catalogs in ordinary Go tests.
func Key ¶ added in v0.12.0
func Key[K comparable](key K) MessageReference[K]
Key records an expected message key without source metadata.
func Ref ¶ added in v0.12.0
func Ref[K comparable](key K, source string, line int, column int) MessageReference[K]
Ref records an expected message key with source metadata.
type NumberFormatOptions ¶ added in v0.12.0
NumberFormatOptions controls dependency-free number formatting.
type PluralCategory ¶ added in v0.12.0
type PluralCategory string
PluralCategory is the bounded core plural category set.
const ( PluralZero PluralCategory = "zero" PluralOne PluralCategory = "one" PluralOther PluralCategory = "other" )
func Cardinal ¶ added in v0.12.0
func Cardinal(locale string, count int) PluralCategory
Cardinal returns the supported plural category for count. The root runtime intentionally keeps this bounded: East Asian locale families fall back to other, and the default family uses one for exactly +/-1.
type PluralForms ¶ added in v0.12.0
PluralForms holds the supported plural variants. Other is the fallback.