Opened 8 years ago

Closed 8 years ago

#6409 closed (fixed)

Locale middleware: user language preference ignored when set to a value of the xx-yy form

Reported by: nicholasdsj@… Owned by: simonb
Component: Internationalization Version: master
Severity: Keywords: i18n Chinese locale localemiddleware get_language_from_request HTTP_ACCEPT_LANGUAGE
Cc: bnomis@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


I found there is a bug in method "get_language_from_request" in

I want to support Chinese language in django, but my LANGUAGE_CODE is "en-gb". I added locales in my browser in this order "zh-cn, de-de, en-us" in order to emulate different languages.

While debugging this application, it never hits "zh-cn" locale. It caused by normalizing function. It changes "zh-cn" to "zh_CN", but "zh_CN" is not in settings.LANGUAGES. So Chinese locale is always missed while the settings.LANGUAGE_CODE is not set to "zh-cn".

Attachments (2)

lang-code-fix.diff (1.6 KB) - added by simonb 8 years ago.
6409-6446-with-regression-tests.diff (3.6 KB) - added by ramiro 8 years ago.
New version of simonb's patch plus regression tests and fixing also #6446

Download all attachments as: .zip

Change History (9)

comment:1 Changed 8 years ago by simonb

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to simonb
  • Patch needs improvement unset
  • Status changed from new to assigned

Changed 8 years ago by simonb

comment:2 Changed 8 years ago by simonb

  • Has patch set

comment:3 Changed 8 years ago by simonb

  • Cc bnomis@… added

comment:4 Changed 8 years ago by nicholasdsj@…

Oh, I think the easiest way is to change settings.LANGUAGES. Change 'zh-cn' to 'zh_CN'.

comment:5 Changed 8 years ago by simonb

The format for settings.LANGUAGES is defined. Take a look in django/conf/ Should be 'zh-cn' Other code is expecting the settings to follow that format. This is format the browser sends in its Accept-Language header. Changing the setting to the localized form of zh_CN may work around this bug but it may have unexpected effects elsewhere.

comment:6 Changed 8 years ago by ramiro

  • Keywords localemiddleware get_language_from_request HTTP_ACCEPT_LANGUAGE added
  • Summary changed from i18n failed in Chinese locale to Locale middleware: user language preference ignored when set to a value of the xx-yy form

I can confirm the reported behavior:

When the LocaleMiddleware is active, if one of the components of the UA language list preference sent on HTTP_ACCEPT_LANGUAGE is set to xx-yy, Django will try to validate against settings.LANGUAGES first xx_YY and then xx, the first will always fail and as a result the user preference will be ignored.

The problem can be reproduced by setting the UA preference to pt-br;de, Django includes both Brazilian Portuguese (pt-br) and Portuguese (pt) translations, but the web site language will be wrongly set
to pt for the user.

In the original report, the UA preference is set to zh-cn;de and as there is a translation to zh-cn but no one to zh in Django, the web site language is wrongly set to de for the user.

The Argentinean spanish l10n is unaffected by this just by accident because its language code in LANGUAGES is wrongly set to es_AR instead of es-ar on source:django/trunk/django/conf/, a bug reported on #6446 and that would need to be fixed simultaneously with this one.

The fix implemented by the patch submitted by simonb solves these issues.

Changed 8 years ago by ramiro

New version of simonb's patch plus regression tests and fixing also #6446

comment:7 Changed 8 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [7091]) Fixed #6409 -- Unbreak compound locale name parsing (e.g. zh-cn).

This was inadvertently broken back in [6608]. Slightly backwards-incompatible:
people specifying "es_AR" in their LANGUAGES list will need to change that to
"es-ar". Thanks, simonb and Ramiro Morales for making the effort to fix this.

Note: See TracTickets for help on using tickets.
Back to Top