ccapndave / elm-translator / Translator

This is package to provide type safe internationalisation, where translations can be loaded at runtime. Default translations, substitutions and pluralization are supported.

Substitutions are implemented by surrounding the literal in braces:

{
  "MyNameIs": "Je m'appelle {name}"
}

Pluralization is implemented by having the singular case on the left of the pipe symbol, and all other cases on the right. The number can be substituted using {count}.

{
  "MyAge": "I am only one year old|I'm {count} years old"
}


type Translator

The translator contains the translation stack. This probably belongs in the model.

decoder : Json.Decode.Decoder Translator

A Json decoder for a translator

encode : Translator -> Json.Encode.Value

A Json encoder for a translator


type Literal

This represents a literal that can be translated.

makeLiteral : String -> Literal

Given the id of the literal in the translations, make a Literal that can be used for doing a translation.

makeLiteralWithOptions : String -> Maybe String -> Dict String String -> Maybe Basics.Int -> Literal

Given the id of the literal in the translations, make a Literal that can be used for doing a translation. This also allows you to specify a default translation, substitutions and a count for pluralisation.

defaultTranslator : Translator

An empty translator. The only translations this will be able to do are the defaults specified in the literals (hence why it is called defaultTranslator).

addTranslations : Translations -> Translator -> Translator

Add a translation dictionary to a translator.

updateTranslations : Translations -> Translator -> Translator

Update the translation dictionary at the head of the stack. If there are none then set this as the only translation dictionary.

trans : Literal -> Translator -> String

Given a Literal, translate to a String. This can never fail, and in the event of being unable to match in either the loaded or default literals this will fall back to "...". This supports substitutions and pluralization.

text : Translator -> Literal -> Html msg

A translated version of Html.text for use directly in an Elm view

placeholder : Translator -> Literal -> Html.Attribute msg

A translated version of Html.Attributes.placeholder for use directly in an Elm view