ChristophP/elm-i18next/3.0.1

NOTE: the latest version is 4.2.1

I18Next

This library provides a solution to load and display translations in your app. It allows you to load json translation files, display the text and interpolate placeholders. There is also support for fallback languages if needed.

Types and Data

A type that represents your loaded translations

type Delims
= Curly
| Underscore
| Custom ( String, String )

A union type for representing delimiters for placeholders. Most commonly those will be {{...}}, or __...__. You can also provide a set of custom delimiters(start and end) to account for different types of placeholders.

type alias Replacements =
List ( String, String )

An alias for replacements for use with placeholders. Each tuple should contain the name of the placeholder as the first value and the value for the placeholder as the second entry. See tr and trf for usage examples.

Use this to initialize Translations in your model. This may be needed when loading translations but you need to initialize your model before your translations are fetched.

Using Translations

t : Translations -> String -> String

Translate a value at a given string.

{- If your translations are { "greet": { "hello": "Hello" } }
use dots to access nested keys.
-}
import I18Next exposing (t)
t translations "greet.hello" -- "Hello"
tr : Translations -> Delims -> String -> Replacements -> String

Translate a value at a key, while replacing placeholders, and trying different fallback languages. Check the Delims type for reference how to specify placeholder delimiters. Use this when you need to replace placeholders.

-- If your translations are { "greet": "Hello {{name}}" }
import I18Next exposing (tr, Delims(..))
tr translations Curly "greet" [("name", "Peter")]
tf : List Translations -> String -> String

Translate a value and try different fallback languages by providing a list of Translations. If the key you provide does not exist in the first of the list of languages, the function will try each language in the list.

{- Will use german if the key exist there, or fall back to english
if not. If the key is not in any of the provided languages the function
will return the key. -}
import I18Next exposing (tf)
tf [germanTranslations, englishTranslations] "labels.greetings.hello"
trf : List Translations -> Delims -> String -> Replacements -> String

Combines the tr and the tf function. Only use this if you want to replace placeholders and apply fallback languages at the same time.

-- If your translations are { "greet": "Hello {{name}}" }
import I18Next exposing (trf, Delims(..))
let
  langList = [germanTranslations, englishTranslations]
in
  trf langList Curly "greet" [("name", "Peter")] -- "Hello Peter"

Fetching and Decoding

fetchTranslations : (Result Error Translations -> msg) -> String -> Cmd msg

A command to load translation files. It returns a result with the decoded translations, or an error if the request or decoding failed. See translationsDecoder for an example of the correct JSON format.

Decode a JSON translations file. The JSON can be arbitrarly nested, but the leaf values can only be strings. Use this decoder directly if you are passing the translations JSON into your elm app via flags or ports. If you are loading your JSON file via Http use fetchTranslations instead. After decoding nested values will be available with any of the translate functions separated with dots.

{- The JSON could look like this:
{
  "buttons": {
    "save": "Save",
    "cancel": "Cancel"
  },
  "greetings": {
    "hello": "Hello",
    "goodDay": "Good Day {{firstName}} {{lastName}}"
  }
}
-}


--Use the decoder like this on a string
import I18Next exposing (translationsDecoder)
Json.Decode.decodeString translationsDecoder "{ \"greet\": \"Hello\" }"


-- or on a Json.Encode.Value
Json.Decode.decodeValue translationsDecoder encodedJson