Hi guys! I’m Janez Kranjc, I’ve been using Django for a bit now - since 1.3 and I’ve recently come across and issue that’s been bothering me in multiple projects that I’m working on.
Localization middleware ignores the accept language header if ANY url pattern exists that is i18n prefixed regardless of the current URL of the request.
So the problem is the following: I have some URLs that are prefixed, and a lot that are not (such as all of the API endpoints). I sometimes need to return some translated strings in the API as well and for that I rely on the accept-language header. However in the middleware it is ignored because an unrelated part of the project has an i18n prefixed url pattern.
Another way to look at the problem is this:
Let’s say I have a SPA that uses i18n on its API endpoints and you rely on accept-language to serve the responses in the correct locale. I then decide to add a new app to your django project - a sales page. Instead of relying on accept-language I wish to have i18n prefixed URLs (maybe for SEO reasons or whatever). Suddenly the behaviour of the API changes even though I’ve made changes to an entirely different part of the project.
Would it not make more sense for the middleware to check if the current URL pattern (the one that the request URL resolves to) is prefixed or not.
The way I see it, this should be changed:
i18n_patterns_used, prefixed_default_language = is_language_prefix_patterns_used(urlconf)
Instead of checking the entire urlconf it should only check the current request URL and see if it resolves to a pattern that is i18n prefixed.
To get around this I need to use a custom localization firmware in a lot of my projects. I would like to hear the devs’ opinion regarding this.