Locales & Resolution

Two axes: the locale chain from most to least specific, and catalog precedence within each locale.

Locale

A locale is a language plus an optional region, in BCP-47 shape. Normalization happens at construction, so casing and separator never matter.

Locale("de")             // de
Locale("de", "CH")       // de-CH
Locale.parse("de_ch")    // de-CH  — underscore and casing normalized
Locale.parse("DE")       // de

Locale.parse("de-CH").base   // de     — the language without the region
Locale("de").base            // de     — a base locale returns itself
Locale.parse("de-CH").tag    // "de-CH"

Always parse untrusted tags. A language stored on a user record is free-form text; Locale.parse is what makes a stored "DE" or "de_CH" resolve like the de-CH it means instead of missing every catalog and falling to English.

The fallback chain

Resolution walks four positions, most specific first, de-duplicated:

locale -> locale.base -> fallback -> fallback.base

With locale = de-CH and fallback = en that is de-CH, de, en. So a Swiss catalog only needs to carry what actually differs from German — everything else falls through.

Two axes, and the order matters

The locale chain is the outer loop; catalog precedence is the inner one. The first hit wins.

for (locale in chain) {          // de-CH, de, en
    for (catalog in catalogs) {   // app, then framework
        // first hit wins
    }
}

So a more specific locale always beats a less specific one, and within the same locale the app beats the framework. An app's German string beats the framework's German string; the framework's German string beats the app's English one.

Locale-outer is the right default for message catalogs, where a key is a fragment and getting the language right matters more than which module supplied it. It is worth knowing this is a decision rather than an inevitability — funktor's email templates deliberately invert it, because a branded mail in the wrong language beats an unbranded one in the right language.

Switching language

I18n is immutable. Changing language produces a new instance carrying the same catalogs:

val german = i18n.withLocale(Locale("de"))

That is what makes the reactive integration straightforward — a stream emits a new snapshot and anything subscribed re-renders. Nothing mutates under a component that is mid-render.

Two surfaces

i18n.translate is the open surface: module-generated namespaces hang off it as extension properties, which is how a module contributes translations without the core knowing about it.

i18n.format is the closed surface for dates, numbers and currency. It is currently a seam that exposes the locale and little else — CLDR-backed formatting is not implemented yet. Route formatting through it anyway, so the implementation can land later without moving call sites.

Known limits

Script and variant subtags are not modelled. The second subtag is always treated as a region and upper-cased, so zh-Hant parses as zh-HANT and will not match a script-aware catalog. Do not rely on those tags until script support lands.