views:

590

answers:

4

Hi -

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',
)
+2  A: 

I may be wrong - as the only time I used translation stuff was on a test project many moons ago - but I think you don't want this:

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

But rather this:

$ django-admin.py makemessages -l he_il -e html

Notice the underscore in he_il.

I was having issues with pt-BR too, until I made the messages file with pt_br instead. Then things started working...

Yeah, it is not obvious and I couldn't find documentation about it anywhere.

Hope that helps.

celopes
+1  A: 

Yes you do need to make message files as celopes suggests and then compile them

python manage.py compilemessages

But you will still have a problem.

Disable LocaleMiddleware for a bit, i.e. remove this

django.middleware.locale.LocaleMiddleware

from your middleware list. Don't use it if you do not need to switch the language at run time, but if you do need it, then there is a solution. I had the same problem before and someone explained this to me.

Also I had this wierd issue before. Makemessages command would choke on strings wrapped with backslash in .py files.

Evgeny
django documentation on translation is good, but long and unfortunately you'll need to read most of it.
Evgeny
I removed that middleware piece, but nothing has changed. Page still renders in English only, even though the LANGUAGE_CODE points to 'he' (I changed it from 'he-il' since that's what django.conf.locale has). Any thoughts?
sa125
were there any error messages on message building and compilation? take a look into locale/he/django.po are there #,fuzzy markers?
Evgeny
do you have locale/he/django.mo file?
Evgeny
added a link to another possible issue.
Evgeny
also I remember the symptom of "bad wrapped strings" was that django.po file was very incomplete so most messages would not be translated.
Evgeny
A: 

Hi,

I had very the same issue, i tried to switch my language and django said no go. No error, no warning, but django switched language to pl-pl (in my case). However removing all folders from locale and executing command: django-admin.py makemessages -l pl_PL (underscore instead of dash and capital letter for second PL, worked this issue out).

I hope it helps some guys out there.

Drachenfels
A: 

The way I went about it is by using the exact language code that django uses in it's own translation files (and not by the link provided inside settings.py), assuming this language is supported (if not things get complicated, since you have to provide your own translation files to django as well).

I found this code by going to $DJANGO_DIR/conf/locale and looking at the folder's name (for me it was at /usr/local/lib/python2.6/dist-packages/django/conf/locale, but it may differ depending on OS and such).

sa125