Skip to content

Gettext derives its locales from the existing language files

February 1, 2024

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, Gettext now derives the available locales 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"]

—

Back to TIL

Next: MDEx is the Markdown parser this site uses

Phil-Bastian Berndt

Phil-Bastian Berndt
Tech Lead at Naymspace