Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django i18n not working

I cannot get translations to show up for my templates. Here is what I have done:

  1. In my template file (index.html), I have loaded i18n and used the trans tag where I want translations.
  2. In my project directory, I rand ./manage.py makemessages -l es
  3. My django.po file is in ./conf/locale/es/LC_MESSAGES/django.po
  4. I edited this file with the translations.
  5. I then ran ./manage.py compilemessages
  6. I set my LANGUAGE_CODE to 'es' in settings.py
  7. Added django.middleware.locale.LocaleMiddleware to MIDDLEWARE_CLASSES in between SessionMiddleware and CommonMiddleware.
  8. Restarted Django, refreshed my page.

Only the english representations are still showing. What am I doing wrong here? Did I miss something?

like image 500
intargc Avatar asked Oct 19 '11 23:10

intargc


2 Answers

Have you tried setting LOCALE_PATHS in your settings file? That fixed it for me...for more info check out an answer I wrote for a similar question or django's official documentation for the LOCALE_PATHS setting.

like image 103
respondcreate Avatar answered Oct 05 '22 05:10

respondcreate


Django's LocaleMiddleware tries to determine the locale from the given request and replaces the LANGUAGE_CODE accordingly. It goes through the following steps:

  1. Checks the session for a session variable called django_language. Set the language to the given locale in this session. If not existing it goes to 2.
  2. Checks the cookies for a similar variable set (you can define the name yourself). It not existing it goes to 3.
  3. Tries to determine the language from the HTTP_ACCEPT_LANGUAGE header. If not possible it will go to 4.
  4. At last it will use the LANGUAGE_CODE setting as given locale.

In all steps except 4 it will check if the language is given in your LANGUAGES setting, which is a tuple of 2-tuples representing language code and language title. Make sure you set/overwrite the default, since the default would be a tuple of all available Django languages.

If you have multiple languages in your Django site:

Make sure your session or cookies are set correctly. You could use Django's internal view for setting the language (django.views.i18n.set_language).

If you got only one language:

If you only have Spanish you usually don't need to bother with this. Just make sure LANGUAGES and LANGUAGE_CODE is set. It will not attempt to set the pages language to your browsers language if it is not provided in LANGUAGES. Additionally you should make sure that USE_I18N = True. That should be the default though, so make at least sure you didn't set it to False somewhere.

like image 32
Torsten Engelbrecht Avatar answered Oct 05 '22 05:10

Torsten Engelbrecht