All posts tagged i18n

Django Translation – Going Live!

A while back I wrote a post about Django translation and getting your websites to be translated.  Since then it has been pretty successful in our development version of the site and I have continued to learn the quirks of Django translation.  A couple of things to note:

  1. In your .po files you will occasionally see that some of your strings have a #, fuzzy tag above them.  This means that there is already a translation found elsewhere and it’s not sure if it should use your translation file in this app or the other one it knows about.  I had a lot of issues with this so I have been removing the fuzzy tag entirely when I find it and this forces Django to use the translation file as is.
  2. Ordering matters in INSTALLED_APPS.  Django will load translation files in order of your INSTALLED_APPS list.  Django has a set of built in translated strings that it will use first before using your translations.  I found this was causing an issue when I didn’t like the translation used for the Chinese version of Email.  Django’s version was four characters long and the one I was using would only be two.  I had to rearrange my INSTALLED_APPS to load my translation files first before the Django ones.  Although, now that I think about it, this may be caused by fuzzy tags…I’ll look into this.

Once we had everything ready some people from the company wanted to see a demo to play around with.  We were going to put everything up and this included translation files just as a demo.  Transferring everything was easy with the new structure of Django since 1.4.  We simply dragged the apps over, updated the settings.py file, and collected static and we were ready to go!  One problem though, when I compiled the messages I was getting an error, and then another error, and another.  Here is a list of the things I did to get translation working on our Linux server.

  1. sudo apt-get install gettext – This is what Django uses to figure out translation and must be installed.
  2. Ensure USE_I18N = True in settings.py.  Ours was but it’s good to check.
  3. django.middleware.locale.LocaleMiddleware must be listed in your MIDDLEWARE_CLASSES – This is the kicker that took me a while to figure out.  Since it took me a long time to get this working on my dev server I didn’t write down all of the steps, hence this post.  This will not throw errors to your log files either so double-check.
  4. Update your project urls.py to include url(r'^i18n/', include('django.conf.urls.i18n')) – This is what Django uses to set the language of the user and is necessary for URL calls to change it.

That should be it.  If there is anything I missed I’ll be sure to update this post.

Django Translation Creating Locale Folders

As our huge project progresses we need to start thinking about some of the major features that our users would like to see.  The first is less of a feature and more of a necessity.  Our users are located all over the world and speak four different languages.  To accommodate all of these users we need to translate every bit of text on every page of our application for all of our users.  Luckily for us Django handles translation very nicely.  Read up on the Django translation here.

This post will only cover the template tag {% trans %}, more will come as we develop more of our translation setup.

Every template generated will need the tag {% load i18n %} at the top to have access to the trans tag.  From this point on any text string will need to be surrounded by the trans tag.  For example, my button has the code <button type="button">Home</button> and to use the translation I simply change it to <button type="button">{% trans "Home" %}</button>.  That string after the trans is going to be the id for the soon-to-be-created translation files.  This string can contain spaces and for our purposes we are going to make the id the default English string.  When a translation is not found it will display the id so in our case the correct English string will save time.

Once you’ve done all of that, and every string you want translated is contained in a trans tag, we need to generate a translation list for all of those strings.  Using Django 1.5.1 get into your application directory inside of your Django project.  Use the command django-admin.py makemessages -l <language code>.  This will generate a folder named the language code inside of a locale folder inside of your application.

OK, now for the reason I wanted to write this post.  I had a list of languages and was trying to test all of this out.  Things were working great for English and German using the language codes en-us and de but was failing for Chinese Simplified, zh-cn.  The reason is actually because though the language value is zh-cn do not use that language code when creating your message folder!  It actually wants to look into the folder zh_CN.  Language folders must be created with the language followed by and underscore and the version.  Since I am not using any translation files for English it doesn’t matter what I use but if I were to do English for the United States and English for the UK I would use the makemessages command and create the folders en_US and en_UK.

Just a quick note I wanted to make so that others, and myself, won’t fall into this misstep in the future.