django - how to make translation work?

sa125 picture sa125 · Dec 2, 2009 · Viewed 32.9k times · Source

I'm trying to render a template in a different language using i18n. I did everything I could read about, from setting the language code, creating and compiling translation files, including the translation tags in the template and all that, and my template still renders in English, even through the {{ LANGUAGE_CODE }} variable points to the correct (and different) code I intended to render. What am I missing?

template:

{% extends "base.html" %}
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_current_language_bidi as LANGUAGE_BIDI %}
{% block title %}{% trans "translation test" %}{% endblock %}
{% block content %}
<div id="some-text">
  {% trans "some translated text goes here" %}
  {% blocktrans %}
  <ol>
    <li>here are some</li>
    <li>items that should be</li>
    <li>translated as well</li>
  </ol>
  {% endblocktrans %}
  <ul>
      <li>The current language is <b>{{ LANGUAGE_CODE }}</b></li>
      {% if LANGUAGE_BIDI %}
        <li>The current language is bidirectional</li>
      {% else %}
        <li>The current language is <b>not</b> bidirectional</li>
      {% endif %}
      <li>Available languages are:
        <ul>
        {% for lang in LANGUAGES %}
          <li>{{ lang.1}}</li>
        {% endfor %}
        </ul>
      </li>
  </ul>
</div>
{% endblock %}

view:

from django.shortcuts import render_to_response
from django.template import RequestContext
from pdb import set_trace as debugger
def check(request):
    return render_to_response('index.html', context_instance=RequestContext(request)

command line (I did fill in the correct translations in .po files):

$ django-admin.py makemessages -l he-il -e html
$ django-admin.py compilemessages

settings.py:

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'he-il'

gettext = lambda s: s
LANGUAGES = (
    ('he-il', gettext('Hebrew')),
    ('en-us', gettext('English')),
)

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.i18n",
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)

Answer

AbuFida picture AbuFida · Oct 3, 2011

Just add the paths of the locale files generated to the settings.py file like the following

LOCALE_PATHS = ( "/xxx/xxx/Projects/xxx/sites/avb/locale/",)