Django i18n_patterns default language

I am using i18n_patterns to change the language prefix in url. It s working fine ones the language cookie is set. The problem is that it’s adding /en/ when I trying to access a page without the language code in private window, even though my preferred language is not en (the default one set in settings.LANGUAGE_CODE).

Django documentation says it will use the following to choose the language:

First, it looks for the language prefix in the requested URL.Failing that, it looks for the LANGUAGE_SESSION_KEY key in the current user’s session.Failing that, it looks for a cookie. The name of the cookie used is set by the LANGUAGE_COOKIE_NAME setting. (The default name is django_language.) Failing that, it looks at the Accept-Language HTTP header. This header is sent by your browser and tells the server which language(s) you prefer, in order by priority. Django tries each language in the header until it finds one with available translations. Failing that, it uses the global LANGUAGE_CODE setting.

So the reason why I get /en/ prefix in my url, when there is no any cookie set is because of the Accept-Language HTTP header. How can i solve that if somebody visits my site for the first time when there is no cookie, the i18n to go to the final step to get the language from the global LANGUAGE_CODE?

Is there any solution to add language prefix to the URL from the settings.LANGUAGE_CODE which is my preferred language. Instead from “Accept-Language” header. I do not want to use the browser language?

This should be possible to do, but not easily, because that logic is embedded inside the django.utils.translation.trans_real.get_language_from_request function.

So at first sight, I don’t see another way than rewriting that function in your code (removing the accept part) and then also rewriting your own version of django.middleware.locale.LocaleMiddleware (inherit the class and rewrite the process_request method) by calling your version of get_language_from_request.

Good luck!

Claude