Opened 16 years ago

Closed 16 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: Simon Blanchard
Component: Internationalization Version: dev
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: no UI/UX: no

Description

I found there is a bug in method "get_language_from_request" in trans_real.py

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 Simon Blanchard 16 years ago.
6409-6446-with-regression-tests.diff (3.6 KB ) - added by Ramiro Morales 16 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 by Simon Blanchard, 16 years ago

Owner: changed from nobody to Simon Blanchard
Status: newassigned

by Simon Blanchard, 16 years ago

Attachment: lang-code-fix.diff added

comment:2 by Simon Blanchard, 16 years ago

Has patch: set

comment:3 by Simon Blanchard, 16 years ago

Cc: bnomis@… added

comment:4 by nicholasdsj@…, 16 years ago

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

comment:5 by Simon Blanchard, 16 years ago

The format for settings.LANGUAGES is defined. Take a look in django/conf/global_settings.py 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 by Ramiro Morales, 16 years ago

Keywords: localemiddleware get_language_from_request HTTP_ACCEPT_LANGUAGE added
Summary: i18n failed in Chinese localeLocale 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/global_settings.py, 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.

by Ramiro Morales, 16 years ago

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

comment:7 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: assignedclosed

(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