I18n for elm application

Hi everyone!

As working on an elm application, I was wondering how I can apply I18n to the app. I saw this package providing I18n in pure elm, but I would like to know if there’s an official/near release of an official package. :slight_smile: Or at least something considered as correct, to avoid rewriting code later…

Thanks!

2 Likes

Since the set of Elm core libraries is still relatively small, I think that in many situations using a package like the one you linked is just a necessity. My team and I use Elm for our production application and we have had to make the decision to import packages numerous times.

I can share some things that I look for when evaluating a package:

  • Consider the number of alternatives (e.g. search ‘i18n’ on package.elm-lang.org). If there are many different variants of the same package, this suggests that there are many ways of doing the particular task.
  • Consider when the package was initially released vs. last updated. If the package was released a while ago and is still receiving updates (or the author is accepting PRs), that’s a great sign. At the same time, a small package with relatively simple functionality might not need frequent updates.
  • Look at the source code (always click that ‘Browse Source’ button in the sidebar!). Do you understand what it is doing? Does it look well organized? Is it well documented?
  • Look at the total amount of code in the package. If the author did abandon development of the package, would you be willing to take ownership of the code? (200 LoC is easier to adopt than 2,000 LoC)
  • Look at the total number of dependencies of the package. Does it rely only on core libraries? Or does it have a laundry list of other dependencies? Would you be willing to support all of them, if necessary?

This list certainly isn’t exhaustive, and I’m sure other people can give other suggestions for things to look for, but I would say that there is no reason to hold out for an ‘official’ package if there are good options today. The Elm ecosystem only gets better when the community supports its package authors.

2 Likes

I currently have great success by using the simple technique described in https://www.gizra.com/content/elm-i18n-type-safety/ (which I found by following the advice in https://github.com/lukewestby/elm-i18n).

What is great with it is that you can’t forget to translate things and you can also have localization of any type, for example date formats, 12/24 hour format, number formats, etc.

I have something like this:

module I10n exposing (Language(..), TranslationId(..), i10n)

import FormatNumber exposing (format)
import FormatNumber.Locales exposing (frenchLocale, usLocale)
import Time.DateTime as DateTime exposing (DateTime)

type Language
    = English
    | French

type TranslationId
    = Hello
    | Date DateTime
    | FloatValue Float


i10n : Language -> TranslationId -> String
i10n lang id =
    case lang of
        English ->
            case id of
                Hello ->
                    "Hello"

                Date datetime ->
                    String.concat
                        [ leadingZero (DateTime.month datetime)
                        , "/"
                        , leadingZero (DateTime.day datetime)
                        , "/"
                        , toString (DateTime.year datetime)
                        ]

                FloatValue value ->
                    format { usLocale | decimals = 2 } value

        French ->
            case id of
                Hello ->
                    "Bonjour"

                Date datetime ->
                    String.concat
                        [ leadingZero (DateTime.day datetime)
                        , "/"
                        , leadingZero (DateTime.month datetime)
                        , "/"
                        , toString (DateTime.year datetime)
                        ]

                FloatValue value ->
                    format { frenchLocale | decimals = 2 } value

For now, I am really satisfied by this method, but I don’t have that many strings in my app, and I don’t mind having all translations embedded, so YMMV. I guess translated strings could still be loaded through HTTP if needed.

Edit: they also have a script to translate i18n key/value JSON files to Elm types:

https://github.com/dragonwasrobot/i18n-to-elm

1 Like