elm-i18n provides tools and a concept for localizing elm apps. The idea is to treat localized text as constants (or functions). To achieve this, localized content is placed in separate modules. Each language can consist of multiple modules, but each module contains only one language.
The correct set of language modules is symlinked or copied into place before compiling the elm app. The result is a localized compiled version of your app. When repeating this process for multiple languages the compiler re-uses the cache and only the translation specific modules are cleared from the cache.
The elm-package is aimed at tool developers who want to parse elm-code into localized elements, read or write CSV or PO files. If you want to use this tool for your elm-project you only need the information in this README.
CSV <-> ELM <-> PO
Note that the language identifier is only included in the directory name and excluded from the Translation module names:
project
├── src/
│ ├── Main.elm (e.g. imports Translation.Main)
│ ├── View.elm (e.g. imports Translation.View)
│ └──>Translation (sym-linked to current directory, e.g. project/Translation/De/)
└── Translation/
├── De/
│ ├── Main.elm (module Translation.Main)
│ └── View.elm (module Translation.View)
└── En/
├── Main.elm (module Translation.Main)
└── View.elm (module Translation.View)
The tool-set is available as a node package and is backed by elm code:
npm install elm-i18n -g
In order to switch the language for compilation to En
, simply execute the
following command at the root of your elm app:
elm-i18n-switch -l En
To switch the language and compile a localized version of your app (to dist/en.js
):
elm-i18n-switch -l En --output dist
If your code is not stored in src
or your main app module is not Main.elm
:
elm-i18n-switch -l En --output dist --src myDir --elmFile MyMain.elm
If your root Translation
module is called MyTranslation
:
elm-i18n-switch -l En --rootModule MyTranslation
This repository provides a few tools to extract string functions and constants from modules containing translations (where one language can consist of multiple modules, but each module only contains one language).
elm-i18n-generator --format CSV --root example/Translation --language De --export
Result:
Module,Key,Comment,Supported Placeholders,Translation
"Translation.Main","greeting","A short greeting.","","Hi"
"Translation.Main","greetingWithName","A personalized greeting. Use placeholder name for the user's name.","name","Guten Tag, {{name}}"
elm-i18n-generator --format CSV -l De --import export.csv
Result in import/De/Translation/Main.elm
:
module Translation.Main exposing (..)
{-| -}
{-| A short greeting.
-}
greeting : String
greeting =
"Hi"
{-| A personalized greeting. Use placeholder name for the user's name.
-}
greetingWithName : String -> String
greetingWithName name =
"Guten Tag, "
++ name
For more information about the PO file format visit: https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/PO-Files.html
elm-i18n-generator --format PO --root example/Translation --language De --export
Result:
#. A short greeting.
msgid "Translation.Main.greeting"
msgstr "Hi"
#. A personalized greeting. Use placeholder name for the user's name.
#. i18n: placeholders: name
msgid "Translation.Main.greetingWithName"
msgstr "Guten Tag, %(name)s"
elm-i18n-generator --format PO --language De --import export.po
Results in the same import/De/Translation/Main.elm
as in the CSV example.
Test
and set your code base to use
that before running tests. This way your tests do not change if you change the
wording of your buttons, labels and fallbacks.Translation
modules.An issue has been created to address this disadvantage.
The tool is built using node.js with an Elm-Core.
To build the elm backend of the node.js part (if developing locally):
make dist
.