smeevil

smeevil

Gettext msgid's. Keys or Strings?

Hi all,

I’m heavily conflicted on gettext and how to best use the msgsid.
The official way of using gettext is by using the msgid as the text as needs to be displayed.

For example “Hi, %name}, good to see you again!”.

As far as I understand, the big reason for this is that when you change the text, you will automatically invalidate all the derived translations and thus it’s easy to pinpoint which ones need updating as well as the code becomes clear and you directly see what kind of text / purpose a gettext call has.

Now on the flip side you could also use msgid as keys.

For example “login-welcome-message-%{name}”

This way you say that its scoped for a login , the purpose is a welcome message and the keys that are available are %{name}. This makes it a bit harder to parse mentally but now the origin language can change without invalidating other translations. Which of course is both good and bad.

Coming to mind might be conversion optimisations where a “product-view-add-line-item-button-text” might change from “Buy” to “Add to cart” because you have seen some suggestions people prefer this. Though in the already translated languages it still makes sense to use translated version of “Buy”. So In this case your key is more relaying intent and purpose then the actual text used.

Regarding translating from the origin language to others, this would not really make a difference. When using the raw PO files you will have a bit of a hard time because the msgid are keys, and you will need to open the origin language one to see the original translation. Though most (online) translation tools use the origin language PO to extract the key and the base translation, so when translating in an other language, you will see the key, and the original translation, making it quite easy.

I am wondering if anyone could provide some input on their experiences with using either form and what the pro’s and con’s are based on the form they prefer.

Thanks for your time,
Gerard

Most Liked

gflohr

gflohr

Keys have one single advantage: Somebody can correct the messages in the base language (because there is no base language). With gettext you either have to change the source code or you have to define your base language as “broken English” and provide a translation catalog for proper English.

Otherwise I know only disadvantages of keys. Apart from the ones mentioned above, these are:

  • The translation files tend to contain stale messages.
  • There are no standard tools that keep the translation files in sync with the sources and with each other.
  • Very hard to produce gramatically correct messages for plural forms (google for “ngettext”).
  • Without an extra translation cache, translations soon become unmaintainable or very expensive. Gettext has a translation cache built-in.
  • Translation errors are hard to detect in key-value files. Compare that to PO, where you see the original next to the translation.
  • Gettext allows you to easily mix a multitude of programming languages.
  • Gettext is a well-established standard with a mature and powerful toolchain and an ever-growing user base. Open-source projects are almost exclusively localized with GNU gettext.
thomasbrus

thomasbrus

I tried the key-based approach for a while because I felt like it was more… correct, but I ran into the following downsides:

  • In the template you don’t see the actual text anymore (you’re already keeping a mental model in your head of markup => design, so abstract keys don’t help there)
  • Extra choices to make regarding naming convention, and having to maintain it / keep it consistent
  • Duplication, for example: login-page-password-field-label & registration-page-password-field-label may have the same translation (“password”). (So… then you create a single translation, password-field-label, but then you deviate from the naming convention)

Hope that’s helpful:)

brightball

brightball

In addition to that, there are usually scripts available that can extract all of your gettext snippets from the code base to create the translation files for you.

When you or somebody else sits down to write the snippets in another language, having the key already be the reference language is really helpful.

Here’s a short post from about a decade ago about it dealing with the concept in CakePHP. :slight_smile:

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement