设置Django翻译的正确方法是什么? [英] What's the correct way to set up Django translation?
问题描述
我在Django 1.6上无法正常翻译的问题.我已将此添加到我的settings.py:
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')),
)
还添加了中间件:
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',
)
以及我使用* 10.py的字符串时的* .py文件:
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 _
我的模板开始于:
{% extends "base.html" %}
{% load i18n %}
,在模板内部,我使用了trans
占位符.例如
and inside the template I used the trans
placeholder. E.g.
<h1>{% trans "Register a tank" %}</h1>
我在locale/de/LC_MESSAGES/django.po中提供了翻译:
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
我想念什么?
P.S.我目前不了解的项目托管在GitHub上: https://github.com/frlan/blankspot
P.S. The project I'm currently fuzzy around is hosted on GitHub: https://github.com/frlan/blankspot
推荐答案
将LOCALE_PATHS
添加到settings.py
并将其设置如下:
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'),
)
请注意,LOCALE_PATHS
必须是一个元组(请查看路径末尾的逗号).
Note that LOCALE_PATHS
must be a tuple (look at the comma at the end of the path).
现在基于LOCALE_PATHS
,locale
文件夹应该位于项目的根目录中.
Now based on LOCALE_PATHS
, the locale
folder should be in the root of your project.
并确保您从项目的根目录运行命令django-admin.py makemessages -l de
和django-admin.py compilemessages
.
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
还按重新启动服务(python manage.py runserver
),然后再次检查.
Restart your service (python manage.py runserver
) and check again.
仅要确保您的本地化已使用默认的Django django.mo
文件应用于Django管理页面,请执行以下测试:
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:
首先在项目的主要urls.py
中将patterns
替换为i18n_patterns
:
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)),
# ...
)
现在转到带有de
前缀的管理页面,例如:http://127.0.0.1:8000/de/admin/
并且管理页面应以德语显示.
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.
好的,您能看到德语版Django的管理页面吗?
OK, are you able to see the admin page of Django in German?
还要使用de
前缀检查您的视图.
Also check your view with the de
prefix too.
根据您的项目代码,某些句子不在trans
块中.将其作为:
According to your project code, some sentences are not in trans
blocks. Put them as:
{% trans "your sentence" %}
此外,您还必须在视图和模型的代码中使用ugettext_lazy
而不是ugettext
(在此处和此处.)
Also you must use ugettext_lazy
instead of ugettext
in your code for views and models (Read here and here.)
替换此:
from django.utils.translation import ugettext as _
具有:
from django.utils.translation import ugettext_lazy as _
现在一切正常.
这篇关于设置Django翻译的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!