marcanuy
marcanuy

Reputation: 23942

Django urls aren't being translated

Following Translating URL patterns, I can make my urls being prefixed with the active language, but I can't get them translated.

urls.py

from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
from django.utils.translation import ugettext_lazy as _

from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings

from exercises.views import ExerciseListView

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
]

urlpatterns += i18n_patterns(
   ...
   url(_(r'^exercises/$'), ExerciseListView.as_view(), name='list'),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

locale/es/LC_MESSAGES/django.po

#: myproject/urls.py:54
msgid "^exercises/$"
msgstr "^ejercicios/$"

manage.py shell

>>> from django.core.urlresolvers import reverse
>>> from django.utils.translation import activate
>>> activate('en')
>>> reverse('list')
'/en/exercises/'
>>> activate('es')
>>> reverse('list')
'/es/exercises/'          <---- should be /es/ejercicios as translated in .po

How can I make reverse('list') display '/es/ejercicios/' ?

Upvotes: 5

Views: 2328

Answers (2)

2ehr
2ehr

Reputation: 139

I had the same problem, and the solution was, in my settings.py, making match my LANGUAGE_CODE with the first language in LANGUAGES list:

# In my settings.py
LANGUAGE_CODE = 'en'

LANGUAGES = (
    ('en', _('English')),
    ('es', _('Spanish')),
)

Upvotes: 0

marcanuy
marcanuy

Reputation: 23942

The problem were not only the URL's but also all the translation strings from the message file weren't being translated.

Having the following directory structure:

-Project  #base directory
  -apps
  -templates
  -project
     -settings.py
  -locale
     -es
       -LC_MESSAGES
          -django.po

Just adding the LOCALE_PATHS config to settings.py fix the problem

LOCALE_PATHS = (
    'locale',
)

Django will look within each of these paths for the /LC_MESSAGES directories containing the actual translation files.

*Tested in Django 1.8

Upvotes: 3

Related Questions