TIL how to add a locale to Elixir Gettext

February 1, 2024 1 min read

In earlier versions of Gettext you had to explicitly define your locales via config (at least that’s how I found it in some blog posts).

Apparently this example is outdated:

config :myapp, MyAppWeb.Gettext, locales: ~w(en de)

Instead, the available locales are now implicitly derived from the existing language files. Just create a new locale via the mix gettext.merge task:

$ mix gettext.merge priv/gettext --locale de
Created directory priv/gettext/de/LC_MESSAGES
Wrote priv/gettext/de/LC_MESSAGES/default.po (12 new messages, 0 removed, 0 unchanged, 0 reworded (fuzzy), 0 marked as obsolete)
Wrote priv/gettext/de/LC_MESSAGES/accounts.po (0 new messages, 0 removed, 0 unchanged, 0 reworded (fuzzy), 0 marked as obsolete)
Wrote priv/gettext/de/LC_MESSAGES/errors.po (25 new messages, 0 removed, 0 unchanged, 0 reworded (fuzzy), 0 marked as obsolete)
...

You can check known locales via:

Gettext.known_locales(MyAppWeb.Gettext)
#=> ["de", "en"]
Elixir
Phil-Bastian Berndt

Phil-Bastian Berndt
Tech Lead at Naymspace