Playing With Gettext in the Sandbox

I’ve done a translation of the Sandbox theme for my swedish section of the site using the GNU gettext tools. It’s pretty simple.

The following command line examples assume you already are in the sandbox theme directory.

Generate a PO Template

First we have to create a PO Template.

  1. $> xgettext --foreign-user -k__ -k_e -d sandbox -n *.php
  2. $> mv sandbox.po sandbox.pot
  3. Edit the charset to read UTF-8.

Creating a PO File

Start by creating a PO File from the PO Template, in this case using the locale sv_SE (swedish as language, Sweden as country) and output a file using the locale as name.

Now time has come to translating the theme. You can either use a simple text editor or something more fancy like KDE’s KBabel. Anyhow, the editor (in this example) need to spit out UTF-8 encoded output.

  1. $> msginit -l sv_SE -i sandbox.pot -o sv_SE.po
  2. Translate the messages in the sv_SE.po file.

Updating a PO File

When code may have changed (for example a new version being released) we need to update the translation project.

Begin with regenerating the sandbox.pot PO Template as described above. Continue by updating the translated PO File using the new template and edit the updated file for new or missing translations.

  1. $> msgmerge -U sv_SE.po sandbox.pot
  2. Translate any new messages in the sv_SE.po file.

Generate a MO File

Unfortunately the plain text PO file isn’t very efficient to use by the gettext library. Instead we have to generate a binary MO file from it that can be read by the gettext library efficiently.

  1. $> msgfmt -o sv_SE.mo sv_SE.po
  2. That’s it, really!

Install your translation

Just drop the MO file into the theme directory and don’t forget to set the locale in wp-config.php, and your site will be both localized and internationalized!

This entry was posted in Tutorials and tagged , , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.

5 Comments

  1. Posted August 13, 2006 at 22:28 | Permalink

    Great with the translation, Anders. Please be aware, though, that the Sandbox is still a less-than-1 release (0.6.1 is current), so I imagine some of these translates will change.

    But the more translates, the better! Thanks for getting on the Sandbox wagon. We’ve made the Sandbox SVN public (for viewing), so check my site for that.

    Good job! :-D

  2. Posted August 13, 2006 at 22:59 | Permalink

    Thank you Scott!

    I haven’t upgraded to the latest version yet. But I will start track the SVN version, it’s actually easier to track any changes and manage my private patches/modifications to the theme that way.

    BTW, I’m thinking about creating a en_US translation so the wording and style on this blog match up against the swedish one. ;-)

  3. Posted August 14, 2006 at 04:29 | Permalink

    Hey Anders, I believe someone is working on another this for the Sandbox, though it seems the link is magically disappeared. Have a look at Zeo’s site, because I know I remember seeing a post of it there. :-O

  4. Posted August 14, 2006 at 05:42 | Permalink

    Hi all,

    Yeah sorry for the missing link. I was moving the folders. Here’s the untranslated POT http://sandboxk2.googlecode.com/svn/sandbox-i18n/

  5. Posted January 28, 2008 at 21:45 | Permalink

    Check out

    http://www.plaintxt.org/2008/01/27/help-translate-the-sandbox/

    Pulls much on the Danish page which used you as a reference.

    If you have or can make Swedish or any other languages for 1.3 the Sandbox author will be very happy.

    Kjell Knudsen

4 Trackbacks