gampleman/elm-translate-icu/1.0.0

Icu.Codegen

Generate a type-safe Elm module from a master-language set of ICU messages.

The generator is a pure function from (key, ICU string) pairs to Elm source text, so it can run anywhere Elm runs (a Node build step, a test, an editor plugin). It emits one function per key. Each function takes the Bundle first (I18n-first argument order) followed by a record of the message's typed arguments; keys with no arguments take only the bundle.

greeting : Bundle -> { name : String } -> String

Non-master locales are never baked in — they load as data at runtime through Icu.Bundle, so adding a locale or a translation needs no regeneration. Only changing the master language's keys or their argument shapes requires re-running the generator.

Options
-> List ( String, String )
-> Result (List ( String, String )) String

Generate the Elm module source for the given master-language pairs.

Returns Err with (key, error) pairs if any message fails to parse; otherwise Ok with the module source as a String. Run the output through elm-format for canonical layout.

type alias Options =
{ moduleName : String }

Generator options.

  • moduleName — the fully-qualified name of the module to generate, e.g. "Translations" or "App.I18n".
type ArgType
= TString
| TInt
| TFloat
| TTime

The inferred type of a message argument, determining the record field type and which Icu.Format binder the generated code uses.

inferArgTypes : Message -> Dict String ArgType

Infer the type of every argument referenced in a message. When an argument is used in more than one way, the widest compatible type wins (Float over Int); a String/number conflict resolves to String.

fieldName : String -> String

Turn a key into a valid record field name (same rules as function names).