Easiest way to generate localization files

landyman picture landyman · Apr 11, 2009 · Viewed 19.9k times · Source

I'm currently writing an app in Python and need to provide localization for it.

I can use gettext and the utilities that come with it to generate .po and .mo files. But editing the .po files for each language, one-by-one, seems a bit tedious. Then, creating directories for each language and generating the .mo files, one-by-one, seems like overkill. The end result being something like:

/en_US/LC_MESSAGES/en_US.mo
/en_CA/LC_MESSAGES/en_CA.mo
etc.

I could be wrong, but it seems like there's got to be a better way to do this. Does anyone have any tools, tricks or general knowledge that I haven't found yet?

Thanks in advance!

EDIT: To be a little more clear, I'm looking for something that speeds up the process. since it's already pretty easy. For example, in .NET, I can generate all of the strings that need to be translated into an excel file. Then, translators can fill out the excel file and add columns for each language. Then, I can use xls2resx to generate the language resource files. Is there anything like that for gettext? I realize I could write a script to create and read from a csv and generate the files -- I was just hoping there is something already made.

Answer

warp picture warp · Jul 3, 2009

I've just done this for a project I'm working on, for us the process is like this:

First, I have a POTFILES.in file which contains a list of source files needing translation. We actually have two files (e.g. admin.in and user.in), as the admin interface doesn't always need translating. So we can send translators only the file containing strings the users see.

Running the following command will create a .pot template from the POTFILES.in:

xgettext --files-from=POTFILES.in --directory=.. --output=messages.pot

Run the above command from the po/ directory, which should be a subdirectory of your project and contain the POTFILES.in file. The .pot file has the exact same format as a .po file, but it is a template containing no translations. You create a new template whenever new translatable strings have been added to your source files.

To update the already translated .po files and add new strings to them, run:

msgmerge --update --no-fuzzy-matching --backup=off da.po messages.pot

In the above example I disable fuzzy matching, as our strings are a mess and it did more harm than good. I also disabled backup files, as everything is in subversion here. Run the above command for each language, in this case I updated the danish translation.

Finally run msgfmt to create the .mo files from the .po files:

msgfmt nl.po --output-file nl.mo

Ofcourse, you wouldn't want to run this manually everytime, so create some scripts (or preferably one or more Makefiles) to do this for you.

Note that your directory layout is slightly different from my situation, so you will have to adjust output filenames/paths a bit, but the steps should be the same.