Swapping languages in a Gettext PO file

TL;DR: Here you have a script for swapping the source and target language of a Gettext PO file. Grab it.

For the first time, I'm working on a project that requires full internationalization (i.e. Chívalo!). So far, I have learned 3 things: 1) Internationalization is much harder than I've ever thought, 2) most tools suck, 3) most Java developers seem to live in a bubble where Gettext never happened. Those will be interesting topics for other posts, but today I'll just explain how to solve a simple problem: swapping source and target languages of a Gettext PO file.

We started Chívalo! as a Spanish-only website, but we always kept in mind that it would go international, eventually. We externalized messages to a file referenced by keys (e.g. nav.home=Home). This is what most people seem to be doing with Play and the officially encouraged mechanism. We quickly realized that this approach sucks big time.1 Then we switched to using Spanish strings as keys, and decided that we would find or create the proper tool to use them at some point.

The point of starting real internationalization arrived. We picked Gettext, extracted the strings, and translated everything to English in a PO file. Now it's time to switch the development to English-first. So we wrote a script to replace every Spanish string with the English equivalent by using the en-US.po file.

The only thing missing is swapping the source language (Spanish) and the target language (English) in en-US.po so that we get the new es-ES.po. I'd thought this would be the easiest part. I was surprised to find out that there seems to be no quick way of doing this.2

Here's a script for doing this with just a simple command:

comments powered by Disqus