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 ¶
- func ParseTag(s string) (language.Tag, error)
- type Bundle
- func (b *Bundle) AddMessages(tag language.Tag, msgs ...*Message) error
- func (b *Bundle) DefaultLang() string
- func (b *Bundle) DefaultLanguage() language.Tag
- func (b *Bundle) FieldNameForLang(lang, raw string) string
- func (b *Bundle) HasMessages() bool
- func (b *Bundle) LanguageTags() []language.Tag
- func (b *Bundle) LoadDir(dir string) error
- func (b *Bundle) LoadDirFS(fsys fs.FS, dir string) error
- func (b *Bundle) MatchLangString(lang string) string
- func (b *Bundle) SetFields(tag language.Tag, fields map[string]string)
- func (b *Bundle) TranslateForLang(lang, key string, data map[string]any) (string, bool)
- func (b *Bundle) TranslatePluralForLang(lang, key string, count any, data map[string]any) (string, bool)
- type Localizer
- type Message
- type MessageNotFoundError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 NewBundleFromString ¶
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 ¶
AddMessages registers messages for the given language tag. If a message with the same ID already exists, it is overwritten.
func (*Bundle) DefaultLang ¶
DefaultLang returns the default language as a string.
func (*Bundle) DefaultLanguage ¶
DefaultLanguage returns the bundle's default language tag.
func (*Bundle) FieldNameForLang ¶
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 ¶
HasMessages returns true if the bundle has any loaded messages.
func (*Bundle) LanguageTags ¶
LanguageTags returns all language tags that have messages loaded.
func (*Bundle) LoadDir ¶
LoadDir loads locale files from a filesystem directory. Expected structure: {dir}/{lang}/messages.json [+ fields.json]
func (*Bundle) LoadDirFS ¶
LoadDirFS loads locale files from an fs.FS. Expected structure: {dir}/{lang}/messages.json [+ fields.json]
func (*Bundle) MatchLangString ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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").
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 ¶
MessageNotFoundError is returned when a message cannot be found in the bundle.
func (*MessageNotFoundError) Error ¶
func (e *MessageNotFoundError) Error() string