I'm using internationalization.
So, everyhting works fine, when I access http://localhost:8000/en/
and http://localhost:8000/de/
But when I access http://localhost:8000/
it redirects me to http://localhost:8000/en/
even when the last accessed page was http://localhost:8000/de/
Basically, I want to save language code, based on the page accessed, e.g. if I access http://localhost:8000/de/
then language is german. Next, when I access http://localhost:8000
, it should point me to http://localhost:8000/de/
, not default http://localhost:8000/en/
How this can be done?
The issue was that Django didn't add request.session['django_language'] = lang_code
I just extend locale middleware and use the following code.
def process_request(self, request):
if self.is_language_prefix_patterns_used():
lang_code = (get_language_from_path(request.path_info) or
request.session.get('django_language', settings.LANGUAGE_CODE))
activate(lang_code)
request.session['django_language'] = lang_code
request.LANGUAGE_CODE = get_language()
Now, I have following question. What should I do so that http://localhost:8000 didn't redirect (to prefix with 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?