Integrating gettext-based localization into Cocoa

Virgil Dupras
2011-11-15

Being busy with my local Occupy movement, Occupons Québec, I don't work much but I nonetheless completed the localization improvements I wrote about earlier. I have to say: it feels good to finally be freed from the burden of manually updating localization files. It kind of feels like when, two years ago (only two?), I automated HS apps' build system. In the before time, the app release process was a complicated series of step that I did manually. How crazy was that?

For developers familiar with gettext's system, you know that the process is rather straightforward. You update pot files from the source code using a gettext like command, then you merge those changes with all your po files with msgfmt and you're done. In HS apps, that's basically what I do as well, but there's an extra challenge: the Cocoa localization system.

Although it's possible to use gettext in a Cocoa app, a large part of the localizations live in XIB's strings files and these have nothing to do with gettext. The tools to localize XIBs are specific to Cocoa. Therefore, if a cross-platform and cross-toolkit app wants to centralize and automate its localizations, it has to create a two-ways bridge between its central localizations and the Cocoa layer's strings files. That's what I did.

First, we have to take the localizable strings from the strings file and add them to our pot. To do so, we just have to take our baseline en.lproj, parse all strings files in it and update our pot (I use the polib for that), and then propagate to our po files.

Once we have that and that our localizers have correctly updated our po files, we have to convert these translations back to our XIB's strings files. To do so, we continue to use en.lproj as our baseline. We copy all the strings files from en.lproj, replace the right-hand part of our strings with the translation that we have in our po files, and put these files in the appropriate lproj folder.

That's it! And the code for it is all in hscommon's new loc.py unit.


This site is best viewed with Opera while listening to The White Stripes