Django国际化最小化的例子 [英] Django internationalization minimal example

查看:105
本文介绍了Django国际化最小化的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序国际化方面遇到困难,所以我在这里提供一个我的实现失败的最小例子。

I'm having difficulty in internationalizing my app, so I present here a minimal example where my implementation fails.

请考虑以下步骤来在django中生成一个网站在国际支持下:

Consider the following steps for producing a website in django with international support:

转到您最喜爱的终端文件夹,并且:

go to your favorite folder in the terminal and:

django-admin.py startproject mysite
cd mysite/
mkdir locale
python manage.py startapp main
# (1) modify mysite/urls.py
# (2) modify main/views.py
# (3) modify mysite/settings.py
django-admin.py makemessages -l de
# (4) modify locale/de/LC_MESSAGES/django.po
django-admin.py compilemessages -l de
python manage.py runserver

其中:

## (1) mysite/urls.py
urlpatterns = patterns('',
url(r'^$', 'main.views.home'),
)

## (2) main/views.py
from django.http import HttpResponse
from django.utils.translation import ugettext as _

def home(request):
    return HttpResponse(_('Hello'))

## (3) mysite/settings.py
LANGUAGE_CODE = 'de'

from django.conf import global_settings
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + \
     ('django.core.context_processors.i18n',) # ensures all django processors are used.

## (4) locale/de/LC_MESSAGES/django.po
#: main/views.py:6
msgid "Hello"
msgstr "Hallo"

我假设网站有一个,只有一个语言,我没有激活中间件区域设置 django文档

I assume the website has one and only one language, thus, I didn't activated the middleware locale by django documentation:


如果要让每个用户指定他或她最喜欢的
语言,请使用LocaleMiddleware。 LocaleMiddleware根据请求的数据启用语言
的选择。它定制每个用户
的内容。

If you want to let each individual user specify which language he or she prefers, use LocaleMiddleware. LocaleMiddleware enables language selection based on data from the request. It customizes content for each user.

此实现不会产生所需的Hello到Hallo的翻译。我做错了什么?

This implementation does not produce the desired translation of "Hello" to "Hallo". What am I doing wrong?

推荐答案

Django以这三种方式收集翻译:
https://docs.djangoproject.com/en/dev/topics/i18n/translation/ #how-django-discovers-translations

Django collects translations in these 3 ways explained here: https://docs.djangoproject.com/en/dev/topics/i18n/translation/#how-django-discovers-translations


LOCALE_PATHS中列出的目录的优先级最高,首先显示比以后出现的优先级高。

The directories listed in LOCALE_PATHS have the highest precedence, with the ones appearing first having higher precedence than the ones appearing later.

然后,如果在INSTALLED_APPS中列出的每个已安装的应用程序中存在一个locale目录,它将会查找和使用。首先出现的优先级高于稍后出现的优先级。

Then, it looks for and uses if it exists a locale directory in each of the installed apps listed in INSTALLED_APPS. The ones appearing first have higher precedence than the ones appearing later.

最后,django / conf / locale中的Django提供的基本翻译被用作回退。 p>

Finally, the Django-provided base translation in django/conf/locale is used as a fallback.

由于您的翻译文件不在这些位置,您需要使用 LOCALE_PATHS 如下所述:
https://docs.djangoproject.com / en / dev / ref / settings /#std:setting-LOCALE_PATHS

Since your translation file is in none of these places you need to set it manually using LOCALE_PATHS as explained here: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-LOCALE_PATHS

这篇关于Django国际化最小化的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆