Django - Javascript国际化:翻译未在网站上呈现

时间:2015-11-12 23:26:51

标签: javascript python django internationalization

我已关注docs

./manage.py makemessages -d djangojs效果很好。

./manage.py compilemessages创建了相关的.po个文件

但是,翻译不是在网站上进行的。

urls.py

js_info_dict = {
    'packages': ('market',),
}

urlpatterns = [url(r'^jsi18n/$', javascript_catalog, js_info_dict), ]

urlpatterns += i18n_patterns(
    url(r'^$', HomePage.as_view(), name='home'),
)

settings.py

LOCALE_PATHS = (
    pjoin(BASE_DIR, '00', 'locale'),
)

# Middleware ===================================================================
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.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

# Internationalization =========================================================
LANGUAGE_CODE = 'en'
TIME_ZONE = 'Europe/Paris'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LANGUAGES = (
    ('en', gettext_noop('English')),
    ('fr', gettext_noop('French')),
)

home.html(脚本已成功加载)

<script type="text/javascript" src="{% url 'django.views.i18n.javascript_catalog' %}"></script>

1 个答案:

答案 0 :(得分:0)

该解决方案在this post中提供,其中说明

  

javascript目录应添加到i18n网址模式中,而不是   正常模式。

因此,urls.py必须更改为:

js_info_dict = {
    'packages': ('market',),
}

urlpatterns += i18n_patterns(
    url(r'^$', HomePage.as_view(), name='home'),
    url(r'^jsi18n/$', javascript_catalog, js_info_dict),
)

Django documentation将相应地updated

  

我们应该在i18n_patterns文档中添加一条说明:如果使用了所有已翻译的内容视图,也必须放在其中。