Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

internationalization - django - how to make translation work?

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',
)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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/",)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...