[Answer]-I18n and l10n in Django with different views

1👍

Have you added django.middleware.locale.LocaleMiddleware to your MIDDLEWARE_CLASSES (docs)?

MIDDLEWARE_CLASSES = (       
    'django.contrib.sessions.middleware.SessionMiddleware',      
    'django.middleware.locale.LocaleMiddleware',    
    'django.middleware.common.CommonMiddleware', 
)

Also, you shouldn’t define your patterns twice. All patterns you wish to be handled by I18N should only be defined in the i18n_patterns(...). So if they are identical, this block will be enough:

# -*- coding: utf-8 -*-

from django.conf.urls import include, url, patterns
from django.contrib import admin
from django.conf import settings
from django.conf.urls.i18n import i18n_patterns

urlpatterns = i18n_patterns('',
    url(r'^$', 'myApp.views.home', name='home'),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^downloads/$', 'myApp.views.downloads'),
    # etc. etc.
)

Update: Building the URL in the template

In the url pattern add a name attribute to the downloads pattern:

urlpatterns = i18n_patterns('',
    # ...
    url(r'^downloads/$', 'myApp.views.downloads', name='downloads'),
    # ...
)

In the template, resolve the link with the url tag:

<a class="navbar-brand" href="{% url 'home' %}">{% trans "Home"%}</a>
<a class="navbar-brand" href="{% url 'downloads' %}">{% trans "Downloads"%}</a>

This way Django builds the url respecting the current language.


How to debug if resolving works correctly

For debugging what Django currently resolves a URL to, you could try the following in the shell.

$ ./manage.py shell

>>> from django.core.urlresolvers import reverse
>>> from django.utils.translation import activate
>>> activate('en')
>>> reverse('downloads')
'/en/downloads'    
>>> activate('es')
>>> reverse('downloads')
'/es/downloads/'
👤sthzg

Leave a comment