/kb

personal knowledgebase

Translation of Django Apps

with 2 comments

I recently created a Django app that needed translation of the Django administration. It took some googling and messing around to make it work, so this quick and dirty walkthrough will hopefully be helpful for others.

Here’s how I did it:

Add translations for all model fields:

class Stuff(models.Model):
    title = models.CharField(_('title'), max_length=255)
    the_other_model = model.ForeignKey('othermodel', verbose_name=_('the other model'), verbose_name_plural=_('the other models'))
 
    class Meta:
      verbose_name = _('stuff')
      verbose_name_plural = _('stuffs')

Then the language (.po) files must be created:

mkdir locale
python manage.py makemessages -l LANG_CODE

Then edit your language files in locale/_LANGUAGE_/LC_MESSAGES/django.po. When you’ve finished editing the language file must be compiled:

python manage.py compilemessages

If you want to translate app names as well, you can add something like this to your project __init__.py:

from django.utils.translation import ugettext_lazy as _
 
_(u'auth')
_(u'configuration')
_(u'sites')
_(u'appname')
_(u'Auth')
_(u'Configuration')
_(u'Sites')
_(u'Appname')

This feels like a dirty hack but I couldn’t find any other way to do it.

Source: Django documentation – Internationalization and localization, and some Google Groups thread that I can’t find right now, for the app name translation.

Theoretically related posts

Written by hgrimelid

March 4th, 2010 at 3:50 pm

Posted in Programming

Tagged with ,

2 Responses to 'Translation of Django Apps'

Subscribe to comments with RSS or TrackBack to 'Translation of Django Apps'.

  1. [...] names is kind of a mystery for me as well. I saw this guy using the __init__.py and adding application names there. It works for me – my application [...]

  2. [...] Затем нагуглил довольно интересный «способ» — http://grx.no/kb/2010/03/04/translation-of-django-apps/ но и он не сработал… я наверное не совсем понимаю [...]

Leave a Reply