Python 设置 Django 翻译的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20467626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the correct way to set up Django translation?
提问by frlan
I've got an issue with translations not working on Django 1.6. I've added this to my settings.py:
我遇到了翻译在 Django 1.6 上不起作用的问题。我已将此添加到我的 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.clickHymaning.XFrameOptionsMiddleware',
)
as well as to my *.py files whenever I'm using a string which shall be l10nd:
以及我的 *.py 文件,每当我使用应为 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 transplaceholder. E.g.
在模板中我使用了trans占位符。例如
<h1>{% trans "Register a tank" %}</h1>
I have provided translations in locale/de/LC_MESSAGES/django.po:
我在 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
PS 我目前模糊的项目托管在 GitHub 上:https: //github.com/frlan/blankspot
采纳答案by Omid Raha
Add LOCALE_PATHSto settings.pyand set it as below:
添加LOCALE_PATHS到settings.py并将其设置如下:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
Note that LOCALE_PATHSmust be a tuple (look at the comma at the end of the path).
请注意,LOCALE_PATHS必须是元组(查看路径末尾的逗号)。
Now based on LOCALE_PATHS, the localefolder should be in the root of your project.
现在基于LOCALE_PATHS,该locale文件夹应位于项目的根目录中。
And be sure that you run the commands django-admin.py makemessages -l deand django-admin.py compilemessagesfrom the root of your project.
而且可以肯定的是你运行的命令django-admin.py makemessages -l de,并django-admin.py compilemessages从项目的根。
djPrj
|
+---> djPrj
|
+---> djApp
|
+---> locale
|
+---> templates
Also rearrange your MIDDLEWARE_CLASSESto be LocaleMiddlewareafter SessionMiddlewareand before CommonMiddlewareas mentioned here:
还重新安排你的MIDDLEWARE_CLASSES是LocaleMiddleware后SessionMiddleware和之前CommonMiddleware提到这里:
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.clickHymaning.XFrameOptionsMiddleware',
)
Restart your service (python manage.py runserver) and check again.
重新启动您的服务 ( python manage.py runserver) 并再次检查。
Just to ensure that your localization is applied to your Django admin page with the default django.mofile of Django, do the following test:
只是为了确保您的本地化应用到您的 Django 管理页面和 Django 的默认django.mo文件,请执行以下测试:
First in main urls.pyof project replace patternswith i18n_patterns:
首先在urls.py项目主要替换patterns为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 deprefix, like: http://127.0.0.1:8000/de/admin/And the admin page should be shown in German.
现在转到带有de前缀的管理页面,例如:http://127.0.0.1:8000/de/admin/管理页面应显示为德语。
OK, are you able to see the admin page of Django in German?
好的,你能看到德语的 Django 管理页面吗?
Also check your view with the deprefix too.
也用de前缀检查你的视图。
According to your project code, some sentences are not in transblocks. Put them as:
根据你的项目代码,有些句子没有分trans块。把它们作为:
{% trans "your sentence" %}
Also you must use ugettext_lazyinstead of ugettextin your code for views and models (Read hereand here.)
此外,您必须在视图和模型的代码中使用ugettext_lazy而不是ugettext(阅读此处和此处。)
Replace this:
替换这个:
from django.utils.translation import ugettext as _
with:
和:
from django.utils.translation import ugettext_lazy as _
And now everything will work.
现在一切都会好起来的。
回答by Nilesh
Please set translated stringin django.poand then use python manage.py compilemessages
请设置translated string中django.po,然后使用python manage.py compilemessages
for e.g
#: path/to/python/module.py:23
msgid "Welcome to my site."
msgstr "put appropriate translated string here"
Suggestion-: You can use django-rosettapackage to add translated stringfrom UI interface. It is easy to add T-string from django-admin. https://github.com/mbi/django-rosetta
建议-:您可以使用django-rosetta包translated string从 UI 界面添加。从 django-admin 添加 T-string 很容易。https://github.com/mbi/django-rosetta
回答by sk1p
You need to enable the LocaleMiddlewarein your settings, to tell Django to do language detection based on the browser-settings. Changing your language preferences effectly sets the Accept-Languageheader. You might need to check in an incognito window, because other means of language detection have a higher priority, such as the user's session and the django_languagecookie.
您需要在设置中启用LocaleMiddleware,以告诉 Django 根据浏览器设置进行语言检测。更改您的语言首选项有效地设置了Accept-Language标题。您可能需要在隐身窗口中进行检查,因为其他语言检测方式具有更高的优先级,例如用户的会话和django_languagecookie。
回答by miraculixx
Check the cookies and the session -- according to How Django Discovers Language Preference, the process is this:
检查 cookie 和会话——根据How Django Discovers Language Preference,过程是这样的:
- language prefix in the URL, when using i18n_patterns in URLconf
- _language key in the current user's session
- django_language cookie (or as specified by settings.LANGUAGE_COOKIE_NAME)
- Accept-Language HTTP header (this is what your browser setting sends)
- settings.LANGUAGE_CODE
- URL 中的语言前缀,在 URLconf 中使用 i18n_patterns 时
- 当前用户会话中的 _language 键
- django_language cookie(或由 settings.LANGUAGE_COOKIE_NAME 指定)
- Accept-Language HTTP 标头(这是您的浏览器设置发送的内容)
- settings.LANGUAGE_CODE
Since your browser settings are set to prefer 'de', I suspect the LocaleMiddleware must decide otherwise in one of the previous steps 1. - 3.
由于您的浏览器设置被设置为首选“de”,我怀疑 LocaleMiddleware 必须在前面的步骤 1. - 3 之一中做出其他决定。
回答by James J. Ye
In my case, I used en-gb as the parameter to run
就我而言,我使用 en-gb 作为参数来运行
django-admin.py makemessages -l en-gb
django-admin.py makemessages -l en-gb
Instead, it should be en_GB.
相反,它应该是 en_GB。
django-admin.py makemessages -l en_GB
django-admin.py makemessages -l en_GB

