Documentation
¶
Overview ¶
Package i18n is a pure-Go (no cgo) port of Ruby's I18n core — the interpreter-independent translate/localize logic of the `i18n` gem's I18n::Backend::Simple. It does dotted-key lookup over an in-memory translation store, %{name} / %<count>d interpolation, :count pluralization, :default and fallback-locale chains, the "Translation missing: …" behaviour, :scope, and strftime-style localization of a host-supplied Date/Time — matching the gem byte-for-byte.
Loading translation *files* (YAML/Ruby) and constructing Date/Time values are seams the host (go-embedded-ruby / rbgo) fills: it parses a file to a nested Hash and calls StoreTranslations, and it hands Localize a Temporal. Everything the gem computes deterministically lives here, with no Ruby runtime.
Index ¶
- func AsString(v Value) string
- type DateValue
- func (d DateValue) Day() int
- func (d DateValue) HasTime() bool
- func (d DateValue) Hour() int
- func (d DateValue) Min() int
- func (d DateValue) Month() int
- func (d DateValue) Nsec() int
- func (d DateValue) Sec() int
- func (d DateValue) Wday() int
- func (d DateValue) Yday() int
- func (d DateValue) Year() int
- func (d DateValue) ZoneName() string
- func (d DateValue) ZoneOffset() int
- type DefaultEntry
- type I18n
- func (i *I18n) AvailableLocales() []string
- func (i *I18n) Backend() *Simple
- func (i *I18n) DefaultLocale() string
- func (i *I18n) Exists(key string, locale ...string) bool
- func (i *I18n) L(obj Temporal, format string, named bool, opts *Options) (string, error)
- func (i *I18n) Locale() string
- func (i *I18n) Localize(obj Temporal, format string, named bool, opts *Options) (string, error)
- func (i *I18n) SetDefaultLocale(l string)
- func (i *I18n) SetFallbacks(locale string, chain ...string)
- func (i *I18n) SetLocale(l string)
- func (i *I18n) SetMissingInterpolationHandler(h MissingInterpolationHandler)
- func (i *I18n) T(key string, opts *Options) (Value, error)
- func (i *I18n) Translate(key string, opts *Options) (Value, error)
- type InvalidPluralizationData
- type MissingInterpolationArgument
- type MissingInterpolationHandler
- type MissingTranslation
- type Options
- type PluralRule
- type ReservedInterpolationKey
- type Simple
- type Temporal
- type TimeValue
- func (t TimeValue) Day() int
- func (t TimeValue) HasTime() bool
- func (t TimeValue) Hour() int
- func (t TimeValue) Min() int
- func (t TimeValue) Month() int
- func (t TimeValue) Nsec() int
- func (t TimeValue) Sec() int
- func (t TimeValue) Wday() int
- func (t TimeValue) Yday() int
- func (t TimeValue) Year() int
- func (t TimeValue) ZoneName() string
- func (t TimeValue) ZoneOffset() int
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DateValue ¶
DateValue adapts a Go time.Time to Temporal as a bare Date (no clock): HasTime is false, so the "date" format tree is used and clock directives read zero.
func (DateValue) ZoneOffset ¶
type DefaultEntry ¶
DefaultEntry is one element of a :default chain: either a literal string to return as-is, or a key to look up (Key set). Use Lit / Key helpers.
func Key ¶
func Key(k string) DefaultEntry
Key builds a symbol default (looked up as a translation key if reached).
func Lit ¶
func Lit(s string) DefaultEntry
Lit builds a literal default (returned verbatim if reached).
type I18n ¶
type I18n struct {
// contains filtered or unexported fields
}
I18n bundles the mutable global state the gem keeps in I18n.config: the active backend, the current and default locale, and the fallback chain. The package exposes a default instance through the top-level functions (I18n.t etc.), and New lets a host hold an isolated one.
func New ¶
New returns an I18n with an empty Simple backend and the given default locale, which is also the initial current locale.
func (*I18n) AvailableLocales ¶
AvailableLocales returns the locales with stored translations.
func (*I18n) Backend ¶
Backend returns the underlying Simple store so a host can load translations into it.
func (*I18n) DefaultLocale ¶
DefaultLocale returns the default locale (I18n.default_locale).
func (*I18n) Exists ¶
Exists reports whether key resolves to a translation in locale (defaulting to the current locale), mirroring I18n.exists?. It does not consult :default or fallback locales — only the asked-for locale.
func (*I18n) Localize ¶
Localize formats a Temporal (a host-supplied Date/Time) for a locale, the way I18n.localize / I18n.l does:
- a named format (e.g. "long") is looked up under <type>.formats.<name>, where <type> is "time" when the value carries a clock else "date"; an unknown named format yields a *MissingTranslation (matching the gem, which raises there);
- a literal format string is used as-is;
- %a/%A/%b/%B/%p/%P (and their %^ upcased variants) are substituted from the locale's date.day_names / month_names / time.am|pm before the value is strftime'd, exactly as I18n::Backend::Base#translate_localization_format.
opts.Locale overrides the current locale; opts.Values supply nothing here but are accepted for symmetry.
func (*I18n) SetDefaultLocale ¶
SetDefaultLocale sets the default locale (I18n.default_locale=).
func (*I18n) SetFallbacks ¶
SetFallbacks sets the fallback chain for a locale (the list of locales tried, in order, when a key is missing in it). The locale itself need not be listed; it is always tried first.
func (*I18n) SetMissingInterpolationHandler ¶
func (i *I18n) SetMissingInterpolationHandler(h MissingInterpolationHandler)
SetMissingInterpolationHandler installs the handler used when a %{...} placeholder has no value. The default keeps the placeholder literal.
func (*I18n) Translate ¶
Translate looks up key and returns its translation, applying scope, count pluralization, interpolation, :default chains and fallback locales — the behaviour of I18n.translate / I18n.t.
A missing key yields the gem's "Translation missing: <locale>.<key>" string, unless opts.Raise is set, in which case a *MissingTranslation is returned.
type InvalidPluralizationData ¶
type InvalidPluralizationData struct {
Entry map[string]Value // the plural sub-hash
Count int
Key string // the plural key that was expected but absent
}
InvalidPluralizationData mirrors I18n::InvalidPluralizationData: a :count was given but the entry lacks the plural key the rule selected (e.g. no :other).
func (*InvalidPluralizationData) Error ¶
func (e *InvalidPluralizationData) Error() string
type MissingInterpolationArgument ¶
MissingInterpolationArgument mirrors I18n::MissingInterpolationArgument: a placeholder in the string has no matching value. The Simple backend's default handler raises this; the default config installed here keeps the placeholder literal instead (matching the gem's out-of-the-box behaviour), so this is only produced when a raising handler is configured.
func (*MissingInterpolationArgument) Error ¶
func (e *MissingInterpolationArgument) Error() string
type MissingInterpolationHandler ¶
MissingInterpolationHandler decides what to substitute for a placeholder whose value is absent. Returning an error aborts interpolation. The default keeps the placeholder text literal, matching the i18n gem's out-of-the-box behaviour (its default handler is overridable but, as shipped, leaves the text in place because the un-configured handler returns the string unchanged for the gsub).
type MissingTranslation ¶
type MissingTranslation struct {
Locale string // the locale the lookup ran in
Key string // the requested key (may be dotted)
Scope []string // any :scope prefix that was applied
}
MissingTranslation is the error MRI's I18n raises (as I18n::MissingTranslationData / its MissingTranslation mixin) when a key does not resolve. Its Error() reproduces the gem's "Translation missing: <keys>" message verbatim, and Translate falls back to that same text (rather than raising) unless WithRaise / T! is used.
func (*MissingTranslation) Error ¶
func (e *MissingTranslation) Error() string
func (*MissingTranslation) Message ¶
func (e *MissingTranslation) Message() string
Message returns the human string the gem substitutes for a missing key, e.g. "Translation missing: en.foo.bar". MRI capitalises the leading "Translation".
type Options ¶
type Options struct {
// Locale overrides the current locale for this call (:locale).
Locale string
// Scope is a key prefix prepended to the lookup (:scope); each entry may be
// dotted and is split on ".".
Scope []string
// Count drives pluralization and is also exposed to interpolation as
// "count" (:count). Use a non-nil *int.
Count *int
// Default is the :default chain: each entry is tried in order. A string that
// names no key is returned literally; a *Symbol entry is looked up as a key.
Default []DefaultEntry
// Values are the interpolation variables (the remaining keyword args).
Values map[string]Value
// Raise makes a missing translation return a *MissingTranslation error
// instead of the "Translation missing: …" string (:raise / I18n.t!).
Raise bool
}
Options configures a single Translate / Localize call, mirroring the gem's keyword options (:scope, :default, :count, :locale, interpolation values…).
type PluralRule ¶
PluralRule maps a count to one of the CLDR plural category names ("zero", "one", "two", "few", "many", "other"). The Simple backend ships only the basic English-style rule (zero/one/other); richer rules — the CLDR categories the i18n-spec / rails-i18n pluralization data uses — are registered per locale and consulted by Translate when a :count is given.
type ReservedInterpolationKey ¶
ReservedInterpolationKey mirrors I18n::ReservedInterpolationKey: the string uses a %{...} placeholder whose name collides with a reserved option key (:scope, :default, …).
func (*ReservedInterpolationKey) Error ¶
func (e *ReservedInterpolationKey) Error() string
type Simple ¶
type Simple struct {
// contains filtered or unexported fields
}
Simple is a port of I18n::Backend::Simple: the in-memory translation store (a per-locale nested symbol-keyed Hash) plus the lookup, pluralization and interpolation logic the default I18n backend runs over it. Loading translation *files* into the store is the host's job (the seam) — StoreTranslations takes the already-parsed nested data and deep-merges it, exactly as the gem's store_translations does after its YAML/Ruby loader returns.
func (*Simple) AvailableLocales ¶
AvailableLocales returns the locales that have stored translations, sorted for determinism (I18n.available_locales).
func (*Simple) RegisterPluralRule ¶
func (s *Simple) RegisterPluralRule(locale string, rule PluralRule)
RegisterPluralRule sets the plural-category rule for a locale, overriding the built-in table. It lets a host install the rails-i18n pluralization data's rule for any locale.
func (*Simple) StoreTranslations ¶
StoreTranslations deep-merges data (a nested symbol-keyed tree for one locale) into the store. data may use map[string]any/[]any (a generic loader's output) or the canonical Value model; it is normalized on the way in. This is the file-loading seam: rbgo parses a YAML/Ruby file to this shape and calls here.
type Temporal ¶
type Temporal interface {
Year() int
Month() int // 1..12
Day() int // 1..31
Hour() int // 0..23
Min() int // 0..59
Sec() int // 0..59
Nsec() int // 0..999999999
// Wday is the 0..6 day-of-week, Sunday == 0 (Ruby's Time#wday).
Wday() int
// Yday is the 1..366 day-of-year (Ruby's Time#yday).
Yday() int
// ZoneOffset is the offset east of UTC in seconds (Ruby's Time#utc_offset).
ZoneOffset() int
// ZoneName is the abbreviated zone name, "" when unknown (Ruby's Time#zone).
ZoneName() string
// HasTime reports whether this value carries a clock component (a Time /
// DateTime) versus a bare Date; it selects the "time" vs "date" format tree
// and whether %p is meaningful, mirroring object.respond_to?(:sec).
HasTime() bool
}
Temporal is the date/time value Localize formats. rbgo's Date / Time / DateTime objects implement it (the seam): the library never constructs a clock value, it only reads the broken-down fields a host hands it. A plain Go time.Time satisfies the contract through TimeValue.
type TimeValue ¶
TimeValue adapts a Go time.Time to Temporal so callers can localize standard library times directly. It always reports HasTime() == true.
func (TimeValue) ZoneOffset ¶
type Value ¶
type Value any
Value is a translation-tree node. The host (rbgo) loads YAML/Ruby translation files into this shape — the file parsing is the seam; this library does the lookup, interpolation, pluralization and localization over it. A node is one of: nil, bool, int, int64, float64, string, []Value, or map[string]Value (a nested sub-tree, the Ruby symbol-keyed Hash). Keys are stored as plain strings (the symbol name without the leading colon), matching how the Simple backend symbolizes loaded data.
func RaiseMissingInterpolation ¶
RaiseMissingInterpolation is the handler that reproduces the gem's documented I18n::MissingInterpolationArgument behaviour. Install it via SetMissingInterpolationHandler to make an absent placeholder an error instead of being left literal.
