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:

  1. use gettext("Welcome Back") in your source code.
  2. sync code with translation files using mix gettext.extract --merge
  3. 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.

Founder of Tentamen, software testing agency.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Karlo Smid

Karlo Smid

Founder of Tentamen, software testing agency.

More from Medium

Implement Sigil in Elixir

Hot Code Reloading of Elixir OTP Application

Building a simple Calendly clone with Phoenix LiveView (pt. 4)

How to report Postgres custom errors in Ecto Changeset