How To Use Elixir Gettext As A Team — Tentamen Software Testing Blog
TL;DR
You are working on a Phoenix application that uses Elixir Gettext for translations. As all team members are doing changes on translation files, you have a lot of merging conflicts on those files. In the end, you sometimes lose some translations in msgstr
variable from translation files.
Elixir Gettext
Elixir Gettext is a library for managing translations in your web application. Workflow is very simple:
- use
gettext("Welcome Back") in your source code.
- sync code with translation files using
mix gettext.extract --merge
- Add translation to
.po
file, here is a German example:priv/gettext/de/LC_MESSAGES/default.po
msgid "Welcome Back"
msgstr "Herzlich Willkommen"
gettext knows which string to use based on locale
assign in session, eg. en
or de
Teamwork
You will probably work in a team where each member pushes to main
branch new code through feature branch
. Your po
files are getting bigger and bigger, and there is 100% for merge conflict on those files. You will try to resolve to merge conflict (you must break the first rule of gettext
, do not edit po
files by hand). You start you mix phx.server`
and you get gettext
the error:
duplicate msgid
Good luck with that!
Solution
The solution is very simple, use dgettext("domain_1", "Welcome Back")
which will put translation to domain_1 po
files. Let each team member has its own domain for translations. No more merge conflicts and long hours resolving them (and losing translations in msgstr
).
Originally published at https://blog.tentamen.eu on April 19, 2021.