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.
Turn your JSON into translations.
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. 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 encodedJsonGet translated values by key straight away, with replacements, fallback languages or both.
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.
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 exists 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"You probably won't need these functions for regular applications if you just want to translate some strings. But if you are looking to build a translations editor you might want to query some information about the contents of the translations.
Use this to obtain a list of keys that are contained in the translations. From this it is simple to, for example, compare two translations for keys defined in one but not the other. The order of the keys is arbitrary and should not be relied on.
This function lets you check whether a certain key is exists in your translations.
Most of the time you'll load your translations as JSON form a server, but there
may be times, when you want to build translations in your code. The following
functions let you build a Translations value programmatically.
Create a Translations value from a list of pairs.
import I18Next exposing (string, object, fromTree, t)
translations =
fromTree
[ ("custom"
, object
[ ( "morning", string "Morning" )
, ( "evening", string "Evening" )
, ( "afternoon", string "Afternoon" )
]
)
, ("hello", string "hello")
]
-- use it like this
t translations "custom.morning" -- "Morning"Represents the leaf of a translations tree. It holds the actual translation string.
A type that represents your loaded translations