Django translation: translating URLs

Viewed 932

I'm using Django 1.11 with translations enabled. My URLs start with language code, e.g. "/en/tickets/" or "/cs/tickets/". I need to solve the following problem: I have a URL in some language (could be any of the ones I'm using - the ones in settings.LANGUAGES) and I need to convert it into one particular language.

Now Django offers django.urls.translate_url() which should be doing exactly what I want, however it seems that it works only when the original URL is in the current language.

Example (Django shell):

>>> from django.urls import translate_url
>>> from django.utils.translation import activate
>>> activate('en')
>>> translate_url('/en/tickets/', 'cs')
'/cs/tickets/'
>>> translate_url('/de/tickets/', 'cs')
'/de/tickets/'
>>> activate('de')
>>> translate_url('/en/tickets/', 'cs')
'/en/tickets/'
>>> translate_url('/de/tickets/', 'cs')
'/cs/tickets/'

I need it to be working for all the languages. How do I do that? Thank you.

Update Here are my urlpatterns:

Project-wide:

urlpatterns = i18n_patterns(
    url(r'^tickets/', include('tickets.urls')),
    # all the other apps, similar to the row above
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Application (sample):

urlpatterns = [
    url(r'^$', views.overview, name='tickets-overview'),
    url(r'^archive/$', views.archive, name='tickets-archive'),
    # all the other views, similar to the row above
]
0 Answers
Related