back

iconwrite Internationalizing a Django website on Appengine

May 13, 2011, 19:43

First of all a thank-you note.

First of all my props to iCanLocalize again and especially to Amir over there who fixed any issues I had with their support for the Django .po format. I asked by e-mail on a friday night and had a solution on Monday morning. After that he kept refinining the support according to my suggestions. If that's not amazing support I don't know what to call it.

The move to AppEngine

As explained in a previous post I have been using Google AppEngine for all my iPhone apps content (horoscopes, conversion rates). I also wrote a custom minimalist statistics package (like Flurry but simpler, lighter and crashfree) and finally recently switched the main website you are reading at this very moment.

Django translations

I will NOT explain how to write the python code to use Django templates here since it is beyond the scope of this post. Not to mention I am not an expert and it was actually a friend of mine who did the job for the website. Here's the steps you can follow to prepare your translations for iCanLocalize.

1. Installing Django on your development machine

Just get the (version 1.3) package from here, uncompress it and run the install script:

cd Django-1.3
sudo python setup.py install

This will give you access to django-admin commands I refer to below. Of course this assumes you're running on a Mac or Linux system with Python installed. As for the rest of the procedure detailed here, this was only tested on Mac.

2. Place identifiers in your pages

You need is to create simple identifiers for where the translations will fit it in your pages. You can use the usual django filters (in this example |linebreaksbr obviously creates some HTML for line breaks.).

<h1> {{_("error.404Title")}}            </h1>
<p>  {{_("error.404Txt")|linebreaksbr}} </p>

3. Create a folder structure at your website's root folder in 'conf/locale'

Just create one folder for each ISO code, for example: 'en', 'fr', 'de', 'ja' ...

Now run this from terminal at the root of your appengine website:

django-admin.py makemessages -a

This will create a LC_MESSAGES/django.po folder and file in each locale folder.

3. Fill-in the english text

The english will be at conf/locale/en/LC_MESSAGES/django.po. If you open this generated file you'll see something like this:

#: pages/404.html:3
msgid "error.404Txt"
msgstr ""

Leave the msgid alone and just fill-in the msgstr:

#: pages/404.html:3
msgid "error.404Txt"
msgstr "Page not Found"

If your texts are short, or you consider yourself hardcore, or just happen to enjoy self-inflicted pain you can use a simple text editor for this task. For entries with multiple lines, using PoEdit definitely makes it simpler. I think the PoEdit UI is an abomination for a Mac user but well, it sort of works.

Note that is is common practice with .po files to use the msgid as the english version. I think it is a terrible idea and that a thousand people sometimes CAN be wrong. Thankfully that's not how django generates the .po file.

4. Get the translations done.

Now you can either edit the .po files yourself with PoEdit for the languages you know or simply upload your english django.po file to iCanLocalize and pay to get a quality translation. I would recommend the later even for languages you master. I personally edit my french changes in iCanLocalize because it allows me to keep organized in one place.

Also using a translation service like this one helps with updates. Update the english text, re-upload the django.po file and it will see changes which can then be edited by translators. Another advantage is that you can re-use strings across multiple projects. With the click of a button you can import translations from your other projects (provided you use the same identifiers).

5. Compile .mo files

After the translations have been updated, run this from terminal at the root of your appengine website:

django-admin.py compilemessages

This will update .mo files. Note that if you get your translations from iCanlocalize apparently the .mo files are already in the zip file but it doesn't hurt to run it again.

0 responses to this post

Name
E-mail (will not be published)
Comment
rss Blog RSS Feed

rss Comments RSS Feed