简体   繁体   中英

Django - Javascript internationalization: translation not rendered in site

I have followed the docs .

./manage.py makemessages -d djangojs works fine.

./manage.py compilemessages created the relevant .po files

However, the translation is not performed on site.

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 (The script is successfully loaded)

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

The solution was provided in this post , which states that

javascript catalog should be added to i18n urls patterns, not to normal patterns.

Therefore urls.py must be changed to:

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 will be updated accordingly:

We should add a note in the i18n_patterns documentation stating: if used all translated content views must also be placed within it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM