i18n

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package i18n is the internal message lookup engine for the Credo framework. It provides Bundle-based message loading, Localizer for per-request lookup, and CLDR plural form selection (delegated to golang.org/x/text/feature/plural).

This package is internal — the public API is exposed via the root credo package:

  • app.UseI18n(...) — setup
  • ctx.Locale() — detected language
  • ctx.T(key, data) — translate (Other form)
  • ctx.TPlural(key, count, data) — translate with plural selection

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseTag

func ParseTag(s string) (language.Tag, error)

ParseTag parses a BCP 47 language tag string into a language.Tag. Exported for use by the root package to construct bundles.

Types

type Bundle

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

Bundle holds all loaded messages and field translations. It is safe for concurrent use after loading is complete.

func NewBundle

func NewBundle(defaultLang language.Tag) *Bundle

NewBundle creates a new Bundle with the given default language.

func NewBundleFromString

func NewBundleFromString(defaultLang string) (*Bundle, error)

NewBundleFromString creates a new Bundle with the default language parsed from a BCP 47 string. Returns an error if the string is not a valid tag.

func (*Bundle) AddMessages

func (b *Bundle) AddMessages(tag language.Tag, msgs ...*Message) error

AddMessages registers messages for the given language tag. If a message with the same ID already exists, it is overwritten.

func (*Bundle) DefaultLang

func (b *Bundle) DefaultLang() string

DefaultLang returns the default language as a string.

func (*Bundle) DefaultLanguage

func (b *Bundle) DefaultLanguage() language.Tag

DefaultLanguage returns the bundle's default language tag.

func (*Bundle) FieldNameForLang

func (b *Bundle) FieldNameForLang(lang, raw string) string

FieldNameForLang returns the translated field display name for the given language string and raw field name. Returns raw if no translation exists.

func (*Bundle) HasMessages

func (b *Bundle) HasMessages() bool

HasMessages returns true if the bundle has any loaded messages.

func (*Bundle) LanguageTags

func (b *Bundle) LanguageTags() []language.Tag

LanguageTags returns all language tags that have messages loaded.

func (*Bundle) LoadDir

func (b *Bundle) LoadDir(dir string) error

LoadDir loads locale files from a filesystem directory. Expected structure: {dir}/{lang}/messages.json [+ fields.json]

func (*Bundle) LoadDirFS

func (b *Bundle) LoadDirFS(fsys fs.FS, dir string) error

LoadDirFS loads locale files from an fs.FS. Expected structure: {dir}/{lang}/messages.json [+ fields.json]

func (*Bundle) MatchLangString

func (b *Bundle) MatchLangString(lang string) string

MatchLangString resolves a language string (BCP 47 tag or Accept-Language header) against the bundle's available tags and returns the matched tag as a string. Returns the default language if no match is found.

func (*Bundle) SetFields

func (b *Bundle) SetFields(tag language.Tag, fields map[string]string)

SetFields sets field name translations for the given language tag. Used for opt-in field name injection (Mode 2 in ADR-008).

func (*Bundle) TranslateForLang

func (b *Bundle) TranslateForLang(lang, key string, data map[string]any) (string, bool)

TranslateForLang looks up a message by key for the given language string. Returns the translated string and true if found, or ("", false) if not. The lang parameter can be a BCP 47 tag or Accept-Language header value. The message's Other plural form is always used; for count-based plural selection use Bundle.TranslatePluralForLang.

func (*Bundle) TranslatePluralForLang

func (b *Bundle) TranslatePluralForLang(lang, key string, count any, data map[string]any) (string, bool)

TranslatePluralForLang is like TranslateForLang but renders the CLDR cardinal plural form selected for count (any integer kind, an integral float, or a decimal string such as "1.5"). count is also exposed to the template as {{.count}}. When count cannot be interpreted as a number, the Other form is rendered.

type Localizer

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

Localizer provides per-request message lookup and rendering.

func NewLocalizer

func NewLocalizer(bundle *Bundle, langs ...string) *Localizer

NewLocalizer creates a Localizer that will look up messages in the bundle for the given language preferences (BCP 47 strings, e.g. "en", "tr", "en-US").

func (*Localizer) Localize

func (l *Localizer) Localize(id string, data map[string]any) (string, error)

Localize looks up a message by ID and renders it with the given data. Returns MessageNotFoundError if the message is not found in any language.

func (*Localizer) LocalizePlural

func (l *Localizer) LocalizePlural(id string, count any, data map[string]any) (string, error)

LocalizePlural looks up a message by ID and renders the correct plural form based on count. The count value is also available as {{.count}} in templates. count may be any integer kind, an integral float, or a decimal string; non-integral floats must be pre-formatted into a string (e.g. "1.5").

func (*Localizer) LocalizeWithTag

func (l *Localizer) LocalizeWithTag(id string, data map[string]any) (string, language.Tag, error)

LocalizeWithTag is like Localize but also returns the matched language tag.

type Message

type Message struct {
	// ID is the unique message identifier (e.g., "v.required", "http.not_found").
	ID string

	// CLDR plural forms.
	Zero  string
	One   string
	Two   string
	Few   string
	Many  string
	Other string // required fallback
}

Message holds the translations for a single message ID. It supports CLDR plural forms; for messages that don't need plurals, only the Other field is populated (string shorthand in JSON).

type MessageNotFoundError

type MessageNotFoundError struct {
	ID  string
	Tag language.Tag
}

MessageNotFoundError is returned when a message cannot be found in the bundle.

func (*MessageNotFoundError) Error

func (e *MessageNotFoundError) Error() string

Jump to

Keyboard shortcuts

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