Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - What's the correct way to set up Django translation?

I've got an issue with translations not working on Django 1.6. I've added this to my settings.py:

LANGUAGE_CODE = 'en-us'
ugettext = lambda s: s
LANGUAGES = (
    ('en', ugettext('English')),
    ('de', ugettext('German')),
)

Also added middlewares:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

as well as to my *.py files whenever I'm using a string which shall be l10nd:

from django.utils.translation import ugettext_lazy as _

My templates start with:

{% extends "base.html" %}
{% load i18n %}

and inside the template I used the trans placeholder. E.g.

<h1>{% trans "Register a tank" %}</h1>

I have provided translations in locale/de/LC_MESSAGES/django.po:

msgid "Register a tank"
msgstr "Einen neuen Tank anmelden"

My browser is set to request German content first: Browser settings

What did I miss?

P.S. The project I'm currently fuzzy around is hosted on GitHub: https://github.com/frlan/blankspot

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Add LOCALE_PATHS to settings.py and set it as below:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)

Note that LOCALE_PATHS must be a tuple (look at the comma at the end of the path).

Now based on LOCALE_PATHS, the locale folder should be in the root of your project.

And be sure that you run the commands django-admin.py makemessages -l de and django-admin.py compilemessages from the root of your project.

djPrj
  |
  +---> djPrj
  |
  +---> djApp
  |
  +---> locale
  |
  +---> templates

Also rearrange your MIDDLEWARE_CLASSES to be LocaleMiddleware after SessionMiddleware and before CommonMiddleware as mentioned here:

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

Restart your service (python manage.py runserver) and check again.


Just to ensure that your localization is applied to your Django admin page with the default django.mo file of Django, do the following test:

First in main urls.py of project replace patterns with i18n_patterns:

from django.conf.urls.i18n import i18n_patterns

urlpatterns = i18n_patterns('',
    url(r'^admin/', include(admin.site.urls)),
    # ...
)

Now go to the admin page with a de prefix, like: http://127.0.0.1:8000/de/admin/ And the admin page should be shown in German.

OK, are you able to see the admin page of Django in German?

Also check your view with the de prefix too.


According to your project code, some sentences are not in trans blocks. Put them as:

{% trans "your sentence" %}

Also you must use ugettext_lazy instead of ugettext in your code for views and models (Read here and here.)

Replace this:

from django.utils.translation import ugettext as _ 

with:

from django.utils.translation import ugettext_lazy as _

And now everything will work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...