4

Reading stack overflow for a long time, first time I need to ask sth here.

The app is bilingual. I defined urls in urlpatterns to be translated with ugettext_lazy. User chooses first language and urls work, if he changes lang to the second one, it works also well.

But if user chooses first language and enters url in second lang, he gets 404, because there is no match, because urlpatterns are translated to first lang.

How could I force Django to check again urlpatterns translated to the second language? I want to display a page if it exists, as if url would be entered in the first lang.

I use LocaleMiddleware.

I thought about setting cookie and redirect, but if not found, in user url will be displayed url translated to the second lang, not what user entered, which can be misleading.

Any ideas?

Regards, Mike

Edit: I don't use those i18n patterns. I've got sth like:

url(_(r'^contact/'), include('contact.urls')),

and would like Django to display appropriate view regardless of chosen language. If the user types in /contact/ or translated to second language /kontakt/ the view should be shown.

okrutny
  • 1,070
  • 10
  • 16
  • I recently did translated url patterns in django. So I got some trouble to understand your question. What exactly leads to 404? Normally translated patterns get language prefixed (https://docs.djangoproject.com/en/dev/topics/i18n/translation/#translating-url-patterns). So /en/products/ should display the english version while /de/produkte/ displays its german counterpart. Can you give an example, what url design leads to your problem? – Jingo Oct 26 '12 at 18:01

1 Answers1

0

I don't really like the idea of translating the URLs, but try something like this:

en/django.po:

msgid "^contact/"
msgstr "^en/contact/"

msgid "^wrong_lang/(?P<url_part>.+)"
msgstr "^ru/(?P<url_part>.+)"

urls.py:

url(_(r'^contact/'), include('contact.urls')), # matches "en/contact/" URL
url(_(r'^wrong_lang/(?P<url_part>.+)', redirect_to_current_lang), # matches "ru/..." URLs

views.py:

def redirect_to_current_lang(request, url_part):
   return HttpResponseRedirect(_('^%s' % url_part))
Anton Morozov
  • 1,090
  • 10
  • 7