Django

Code

Ticket #6409 (closed: fixed)

Opened 6 months ago

Last modified 5 months ago

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

Reported by: nicholasdsj@gmail.com Assigned to: simonb
Milestone: Component: Internationalization
Version: SVN Keywords: i18n Chinese locale localemiddleware get_language_from_request HTTP_ACCEPT_LANGUAGE
Cc: bnomis@gmail.com Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

lang-code-fix.diff (1.6 kB) - added by simonb on 01/22/08 05:34:45.
6409-6446-with-regression-tests.diff (3.6 kB) - added by ramiro on 02/04/08 20:47:29.
New version of simonb's patch plus regression tests and fixing also #6446

Change History

01/22/08 05:31:41 changed by simonb

  • owner changed from nobody to simonb.
  • needs_better_patch changed.
  • status changed from new to assigned.
  • needs_tests changed.
  • needs_docs changed.

01/22/08 05:34:45 changed by simonb

  • attachment lang-code-fix.diff added.

01/22/08 05:36:39 changed by simonb

  • has_patch set to 1.

01/22/08 05:37:36 changed by simonb

  • cc set to bnomis@gmail.com.

01/22/08 07:24:32 changed by nicholasdsj@gmail.com

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

01/22/08 21:42:34 changed by simonb

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.

02/03/08 12:56:50 changed by ramiro

  • keywords changed from i18n Chinese locale to i18n Chinese locale localemiddleware get_language_from_request HTTP_ACCEPT_LANGUAGE.
  • 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/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.

02/04/08 20:47:29 changed by ramiro

  • attachment 6409-6446-with-regression-tests.diff added.

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

02/05/08 19:04:30 changed by mtredinnick

  • status changed from assigned to closed.
  • resolution set to fixed.

(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.


Add/Change #6409 (Locale middleware: user language preference ignored when set to a value of the xx-yy form)




Change Properties
Action