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.
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.
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.
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"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")]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"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"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
A type that represents your loaded translations