Now, for developers or anyone else interested, I'm going to explain the basic process to go from an English-only application in Launchpad to something that can be easily translated by anyone. This will internationalize your project, and in a later post I'll explain how to localize it; that is, once you have translations, how to integrate these into your project so users in other locales see the translated strings instead of English. My project is wxBanker (wxbanker.org), so I'll be using that for reference.
- First, you'll want to make all your user-visible strings translatable in your code. I used gettext (http://en.wikipedia.org/wiki/Gettext), and it is quite easy in Python. Basically, you wrap all strings you want to be translated in a gettext call, which is typically assigned to the underscore character to be easy to type and read. So "Hello, world!" will become _("Hello, world!"). When your application is run, this call will then translate that string into the appropriate languange (once you have everything set up, which will be Part 2 of this post).
Sometimes you may want to restructure or break up larger strings into smaller ones. If you have two strings "Sentence A. Sentence B." and "Sentence A. Sentence C", you may want to gettext those as _("Sentence A.")+_("Sentence B.") and _("Sentence A.")+_("Sentence C."), so that Sentence A only needs to be translated once. However, make sure to give enough context for translators. Basically, apply principles of resuable software design to your strings.
- If you try to run your application now, it probably won't work as the underscore function won't be defined. You'll need to set the locale to the user's default and install gettext. In Python this is:
locale.setlocale(locale.LC_ALL, '') # use the environment default '' for ALL purposes
gettext.install("wxbanker") # specify the translation namespace
Now you should be able to run your application and everything should work normally in English; we haven't broken anything! To simplify this set up, however, I like to put those above four lines in a separate file such as localization.py, and import that in any file which wraps strings using gettext, so I don't have to duplicate those each time.
- Now you need to generate a translation template. This is fairly straightforward; simply run xgettext at the command line on all the files you used gettext in. For example: "xgettext file1.py file2.py file3.py". This will generate a file called messages.po, which is your template. Rename this to messages.pot (the scheme Launchpad prefers) and we can proceed!
- Now we'll set up Launchpad. Edit your projects details (in my case: https://launchpad.net/wxbanker/+edit) and check the box that says "Translations for this project are done in Launchpad", scroll to the bottom, and click "Change".
- Click the Translations tab of your project in Launchpad and upload the .pot file you generated in step #3. If this is your first time doing so, it will need to be reviewed by a human, and will take perhaps a day to get approved, at which point you will receive an email letting you know it has been and you are good to go.
- Your project can now be translated! Visit your Translations page again (such as https://translations.launchpad.net/wxbanker) to see the status. You won't see anything exciting if there aren't any translations yet and you aren't configured for more than English (click Select Languages in the lower right to change this), but otherwise you will see an option to translate the project for each language other than English that you know, and the status of the translation.