Django i18n: Common causes for translations not appearing

Viewed 10056

I am making a multilingual Django website. I created a messages file, populated and compiled it. I checked the site (the admin in this case,) in my wanted language (Hebrew) and most phrases appear in Hebrew like they should, but some don't. I checked the source and these still appear as _('Whatever') like they should, also they are translated on the messages file, and yes, I remembered to do compilemessages.

What are some common causes for translations not to appear like that?

9 Answers

Maybe the translated strings are marked as fuzzy?

Just got hit by one. I had the locale/ directory in the root of my project, but by default Django looks for translations in the INSTALLED_APPS directories, and in the default translations. So it didn't find the translations I added. But some of my strings were in the default translations that come with Django (eg "Search") so a few strings were translated, which confused me.

To add the directory where my translations were to the list of places that Django will look for translations, I had to set the LOCALE_PATHS setting. So in my case, where the locale/ directory and settings.py were both in the root of the django project I could put the following in settings.py:

from os import path
LOCALE_PATHS = (
    path.join(path.abspath(path.dirname(__file__)), 'locale'),
)

A possible cause is Lazy Translation.

In example, in views.py you should use ugettext:

from django.utils.translation import ugettext as _

But in models.py, you should use ugettext_lazy:

from django.utils.translation import ugettext_lazy as _

My answer here, all my translations were working except for 2 DateFields that I was using in a ModelForm. Turns out that I had a widget in my forms.py that was not working well with the translations.

I just removed it for now so I can enjoy Xmas =D

Related