0

I use i18n_patterns What should I do so that http://localhost:8000 didn't redirect to url prefixed with language code? In addition, I want to supply language, taken from session, not request.LANGUAGE_CODE

I found following code:

class NoPrefixLocaleRegexURLResolver(LocaleRegexURLResolver):

@property
def regex(self):
    language_code = get_language()
    if language_code not in self._regex_dict:
        regex_compiled = (re.compile('' % language_code, re.UNICODE)
                          if language_code == settings.LANGUAGE_CODE
                          else re.compile('^%s/' % language_code, re.UNICODE))

        self._regex_dict[language_code] = regex_compiled
    return self._regex_dict[language_code]

However, there is problem with that code in checking if language_code == settings.LANGUAGE_CODE. If I enter http://localchost:8000, it will not redirect, but supply a page with translation from settings.LANGUAGE_CODE instead of request.session.get('django_language'). As I understood, I can't access request, so what should be done?

Paul R
  • 2,631
  • 3
  • 38
  • 72
  • do you not want the language prefixes anywhere? might be simpler to just not use the `i18n_patterns` helper and not have prefixed urls. the `LocaleMiddleware` will then automatically use the language from the user's session instead https://docs.djangoproject.com/en/1.7/topics/i18n/translation/#how-django-discovers-language-preference and there is a view to set the langage in the user session https://docs.djangoproject.com/en/1.7/topics/i18n/translation/#the-set-language-redirect-view – Anentropic Jan 13 '15 at 21:31
  • I prefer to set language with prefix. So, localhost:8000/de automatically swithces to german. – Paul R Jan 13 '15 at 22:53
  • There is bound to be a third party app that does this for you – dan-klasson Jan 14 '15 at 00:47
  • do you have `settings.LANGUAGE_CODE` defined? maybe if that was not found it would fall back to checking the session – Anentropic Jan 14 '15 at 09:54

0 Answers0