Opened 8 years ago

Closed 7 years ago

Last modified 7 years ago

#25751 closed Cleanup/optimization (fixed)

Docs about i18n_patterns() + JS translation catalog view needs enhancements

Reported by: George Tantiras Owned by: Ben Wilkins
Component: Documentation Version: 1.8
Severity: Normal Keywords: javascript translation
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by George Tantiras)

To translate a string in a javascript file, the docs propose the following setup in the urls.py:

from django.views.i18n import javascript_catalog

js_info_dict = {
    'packages': ('your.app.package',),
}

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

This will not work unless the javascript url is added to urlpatterns under i18n_patterns:

from django.views.i18n import javascript_catalog
from django.conf.urls.i18n import i18n_patterns

js_info_dict = {
    'packages': ('application.name',),
}

urlpatterns = [
    ...
]

urlpatterns += i18n_patterns(
    url(r'^jsi18n/$', javascript_catalog, js_info_dict),
)

Change History (16)

comment:1 by George Tantiras, 8 years ago

Summary: Translation of strings in a javascript file docs are incorrectTranslation of strings in a javascript file: Docs are incorrect

comment:2 by George Tantiras, 8 years ago

Description: modified (diff)

comment:3 by Simon Charette, 8 years ago

I think you don't need i18n_patterns as long as you use LocaleMiddleware.

comment:4 by George Tantiras, 8 years ago

I had placed LocaleMiddleware to the specific place described in the docs.

I could not receive the translation whatsoever although makemessages was working as expected.

Then I bumped to a forgotten stackoverflow post.

It felt like I won the lottery!

This was my configuration:

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>

Last edited 8 years ago by George Tantiras (previous) (diff)

comment:5 by Simon Charette, 8 years ago

Using i18n_patterns will make translations available under a language specific namespace instead of relying on the _language session key, django_language cookie and Accept-Language header.

What happens if you try access the non-i18n namespaced URL with an empty session and an Accept-Language header set to fr?

comment:6 by George Tantiras, 8 years ago

Actually I do not have access to my computer now, but what was actually happening was that when I reached the url:
/fr/my_page everything was translated to French except for the Javascript strings.

I will also have to study some more in order to achieve accessing the url with an empty session and an Accept-Language header set to fr!

comment:7 by Simon Charette, 8 years ago

Needs documentation: set
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

From what I understand your main url_patterns (the one defined by ROOT_URLCONF) uses i18n_patterns.

In this case LocaleMiddleware will behave differently by solely relying on the request path to determine the active language.

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

comment:8 by George Tantiras, 8 years ago

That seems to be the issue, after all.

Thank you!

comment:9 by George Tantiras, 8 years ago

Summary: Translation of strings in a javascript file: Docs are incorrectTranslation of strings in a javascript file: Docs seem to be incorrect

comment:10 by Tim Graham, 8 years ago

Component: InternationalizationDocumentation
Needs documentation: unset

comment:11 by Tim Graham, 7 years ago

Has patch: set

comment:12 by Claude Paroz, 7 years ago

Patch needs improvement: set

comment:13 by Ben Wilkins, 7 years ago

Owner: changed from nobody to Ben Wilkins
Patch needs improvement: unset
Status: newassigned

comment:14 by Ramiro Morales, 7 years ago

Patch needs improvement: set
Summary: Translation of strings in a javascript file: Docs seem to be incorrectDocs about i18n_patterns() + JS translation catalog view needs enhancements

comment:15 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 9524fd9:

Fixed #25751 -- Doc'd how to use JavaScriptCatalog with i18n_patterns().

comment:16 by Tim Graham <timograham@…>, 7 years ago

In cdc343ca:

[1.10.x] Fixed #25751 -- Doc'd how to use JavaScriptCatalog with i18n_patterns().

Backport of 9524fd9133e47fa90846904f81fe91c72f331e56 from master

Note: See TracTickets for help on using tickets.
Back to Top