gampleman/elm-translate-icu/1.0.0

Icu.Bundle

A runtime store of translations for the generated code to target.

A Bundle holds the Intl handle, the active Locale, the active locale's messages, and a set of fallback messages (the master language). Keys missing from the active locale fall back to the master, so a locale can ship partial translations. New locales are loaded as data at runtime — adding one requires no recompile.

type Bundle

An opaque runtime translation store.

type alias Messages =
Dict String Message

A map from translation key to its parsed message.

fromMessages : Intl -> Locale -> Messages -> Bundle

Build a bundle from already-parsed messages. The messages passed here serve as both the active locale and the fallback (they are the master language). Generated code calls this in its init.

decodeMessages : String -> Result Error ( Messages, List ( String, String ) )

Parse a JSON object of { "key": "ICU string", ... } into Messages.

Returns the list of (key, parseError) pairs for any values that are not valid ICU, alongside the messages that did parse. This is total: a malformed entry is reported but does not abort the whole load.

parseMessages : List ( String, String ) -> ( Messages, List ( String, String ) )

Parse an association list of (key, ICU string) into messages, collecting any per-key parse errors. Exposed for callers that already have the pairs (e.g. generated code baking in the master language).

withLocale : Locale -> Messages -> Bundle -> Bundle

Switch the active locale, replacing the active messages. The fallback (master) messages are preserved, so keys missing from the new locale still resolve.

The bundle's currently active locale.

translate : Bundle -> String -> Args -> String

Format the message for key with the given arguments, formatting for the active locale. If the key is absent from the active locale it falls back to the master language; if it is absent there too, the key itself is returned so the gap is visible.