Permalink
Browse files

Make sr->sr-CYRL [bug 650363]

* Reworked how NON_SUPPORTED_LOCALES works in get_best_language().
* Can now use NON_SUPPORTED_LOCALES to override prefix fallback.
  • Loading branch information...
1 parent b2fda76 commit 1f86348a2ca9a30eaed2f550a40e206f792c206f James Socol committed May 4, 2011
Showing with 17 additions and 15 deletions.
  1. +10 −4 apps/sumo/tests/test_locale_middleware.py
  2. +5 −5 apps/sumo/urlresolvers.py
  3. +1 −0 settings.py
  4. +1 −6 webroot/.htaccess
View
14 apps/sumo/tests/test_locale_middleware.py
@@ -85,6 +85,10 @@ def test_prefix_matching(self):
best = get_best_language('en-gb, es;q=0.2')
eq_('en-US', best)
+ def test_serbian(self):
+ """sr -> sr-CYRL, not sr-LATN."""
+ eq_('sr-CYRL', get_best_language('sr'))
+
class NonSupportedTests(TestCase):
@mock.patch.object(settings._wrapped, 'NON_SUPPORTED_LOCALES',
@@ -93,14 +97,16 @@ def test_get_non_supported(self):
eq_('no', get_non_supported('nn-NO'))
eq_('no', get_non_supported('nn-no'))
eq_(settings.LANGUAGE_CODE, get_non_supported('xx'))
- eq_(None, get_non_supported('xx-YY'))
+ eq_(None, get_non_supported('yy'))
@mock.patch.object(settings._wrapped, 'NON_SUPPORTED_LOCALES',
- {'nn-NO': 'no'})
+ {'nn-NO': 'no', 'xx': None})
def test_middleware(self):
response = self.client.get('/nn-NO/home', follow=True)
self.assertRedirects(response, '/no/home', status_code=302)
- response = self.client.get('/home', follow=True,
- HTTP_ACCEPT_LANGUAGE='nn-no')
+ response = self.client.get('/nn-no/home', follow=True)
self.assertRedirects(response, '/no/home', status_code=302)
+
+ response = self.client.get('/xx/home', follow=True)
+ self.assertRedirects(response, '/en-US/home', status_code=302)
View
10 apps/sumo/urlresolvers.py
@@ -77,7 +77,12 @@ def get_best_language(accept_lang):
"""Given an Accept-Language header, return the best-matching language."""
LUM = settings.LANGUAGE_URL_MAP
+ NSL = settings.NON_SUPPORTED_LOCALES
+ LC = settings.LANGUAGE_CODE
langs = dict(LUM)
+ # Add in non-supported first to allow overriding prefix behavior.
+ langs.update((k.lower(), v if v else LC) for k, v in NSL.items() if
+ k.lower() not in langs)
langs.update((k.split('-')[0], v) for k, v in LUM.items() if
k.split('-')[0] not in langs)
ranked = parse_accept_lang_header(accept_lang)
@@ -88,11 +93,6 @@ def get_best_language(accept_lang):
pre = lang.split('-')[0]
if pre in langs:
return langs[pre]
- # Separate because it should never take precedence.
- for lang, _ in ranked:
- ns = get_non_supported(lang)
- if ns is not None:
- return ns
# Couldn't find any acceptable locale.
return False
View
1 settings.py
@@ -83,6 +83,7 @@
NON_SUPPORTED_LOCALES = {
'nb-NO': 'no',
'nn-NO': 'no',
+ 'sr': 'sr-CYRL', # Override the tendency to go sr->sr-LATN.
}
TEXT_DOMAIN = 'messages'
View
7 webroot/.htaccess
@@ -1,9 +1,4 @@
RewriteEngine On
-# Redirect locales that point to one locale in SUMO.
-# Kitsune does this by itself, mostly.
-# Just need to move sr-CYRL above sr-LATN to get this.
-RewriteRule ^sr/(.+)$ /sr-CYRL/$1 [L,R]
-
-# Everything else goes to Kitsune.
+# Everything goes to Kitsune.
RewriteRule ^(.*) /k/$1 [QSA]

0 comments on commit 1f86348

Please sign in to comment.