Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django i18n does not work

I'm trying to activate different languages for my project. English and spanish right now.

I'll describe all steps i follow:

FIrst of all, i place myself in the directory i want to translate. Or better said, where all {% trans %} tags are:

$ cd media/templates/landing/
$ mkdir locale
$ django-admin.py makemessages --locale=en

Last command creates the directory/file /locale/en/LC_MESSAGES/django.po

I open django.po and i proceed to complete all msgstr fields in english language. msgid label is in spanish. I respect the tips about long messages case. After fill in this file, i make:

$ django-admin.py compilemessages

This process django.po and creates django.mo.

So i modify settings.py. Important lines:

TEMPLATE_CONTEXT_PROCESSORS = (
    'ism.context_processor.user_vars',
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.contrib.messages.context_processors.messages',
    'django.core.context_processors.request',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'pipeline.middleware.MinifyHTMLMiddleware',
}

TIME_ZONE = 'Atlantic/Canary'

LANGUAGE_CODE = 'es'

USE_I18N = True

_ = lambda s: s

LANGUAGES = (
    ('es', _('Espanol')),
    ('en', _('English')),
)

USE_L10N = True

USE_TZ = True

Finally, i add this line to URLS.py:

(r'^i18n/', include('django.conf.urls.i18n')),

I restart my development server, i configure my Firefox browser to choose English first as primary language and it does not work. All texts still showing in spanish instead english.

I make sure Firefox is configured in english because in Django view function (which render the .html) i make a print with request.LANGUAGE_CODE, which prints "en".

What am I making wrong?

like image 271
Rubén Jiménez Avatar asked Jun 28 '12 02:06

Rubén Jiménez


2 Answers

FIrst of all, i place myself in the directory i want to translate. Or better said, where all {% trans %} tags are:

$ cd media/templates/landing/ $ mkdir locale $ django-admin.py makemessages --locale=en

This is not how you are supposed to do the translations.

You create message files for the language you want to translate to not the one you want to translate from, which is the base language. In this case, that is English.

Also the comamnd needs to be run from your project directory; not the template directory.

So, you command should be:

$ django-admin.py makemessages --locale=es

You should run it from the root directory of your project not the templates directory. You also don't create the locale directory, the command will create that for you.

Next, you add your translations in Spanish to the file. The file will have the English labels. This tells django to load the spanish equivalent for the English version.

Once you have that, you compile the message file (again from the root)

$ django-admin.py compilemessages

Now translations are ready for your application. You need to tell django which language you want. Since you are using the built-in view for switching languages, you need to call that view with the language you want.

There is a template provided in the documentation that you can use:

<form action="/i18n/setlang/" method="post">
{% csrf_token %}
<input name="next" type="hidden" value="{{ redirect_to }}" />
<select name="language">
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<option value="{{ language.code }}">{{ language.name_local }} ({{ language.code }})</option>
{% endfor %}
</select>
<input type="submit" value="Go" />
</form>

Select the language from this form so that django knows which translations to load.

You should also read how django discovers language preference for your views and templates.

like image 66
Burhan Khalid Avatar answered Sep 28 '22 07:09

Burhan Khalid


Editor's Note: This answer was migrated from an edit to the question. It was written by the Original Poster.

There was a problem taking the locale path. Django was ignoring (or just it didn't find) my locale directory in root path so django.po/mo was not available. All this shit stuff can be solved adding LOCALE_PATHS in settings.py.

More info -> https://docs.djangoproject.com/en/dev/topics/i18n/translation/#how-django-discovers-translations

like image 41
JasonMArcher Avatar answered Sep 28 '22 06:09

JasonMArcher