]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Fixed #37 (likely subtag resolving)
authorArmin Ronacher <armin.ronacher@active-4.com>
Sun, 28 Jul 2013 21:13:21 +0000 (23:13 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sun, 28 Jul 2013 21:13:21 +0000 (23:13 +0200)
This fixes some locales not loading correctly (like zh_CN) that
are defined through simplified rules in the likely-subtag mapping.

CHANGES
babel/core.py
tests/test_core.py

diff --git a/CHANGES b/CHANGES
index 286e3d6d1af426d4c33b48694e0515ee150ad551..13f1ad8a63460ee45866978f55ecd3f53bd2ec84 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,11 @@ Version 1.3
 
 (bugfix release, release date to be decided)
 
+- Fixed a bug in likely-subtag resolving for some common locales.
+  This primarily makes ``zh_CN`` work again which was broken
+  due to how it was defined in the likely subtags combined with
+  our broken resolving.  This fixes #37.
+
 Version 1.2
 -----------
 
index 6b9cd41ed33589aeaf234947bd507fa3125b39d8..6e6e6d6194498d12e17b0e232d7e5c6b8555b2f5 100644 (file)
@@ -248,7 +248,6 @@ class Locale(object):
             raise TypeError('Unxpected value for identifier: %r' % (identifier,))
 
         parts = parse_locale(identifier, sep=sep)
-
         input_id = get_locale_identifier(parts)
 
         def _try_load(parts):
@@ -257,6 +256,17 @@ class Locale(object):
             except UnknownLocaleError:
                 return None
 
+        def _try_load_reducing(parts):
+            # Success on first hit, return it.
+            locale = _try_load(parts)
+            if locale is not None:
+                return locale
+
+            # Now try without script and variant
+            locale = _try_load(parts[:2])
+            if locale is not None:
+                return locale
+
         locale = _try_load(parts)
         if locale is not None:
             return locale
@@ -283,22 +293,22 @@ class Locale(object):
 
         parts = language, territory, script, variant
 
+        # First match: try the whole identifier
         new_id = get_locale_identifier(parts)
         likely_subtag = get_global('likely_subtags').get(new_id)
-        if likely_subtag is None:
-            raise UnknownLocaleError(input_id)
-
-        parts2 = parse_locale(likely_subtag)
-
-        # Success on first hit, return it.
-        locale = _try_load(parts2)
-        if locale is not None:
-            return locale
-
-        # Now try without script and variant
-        locale = _try_load(parts2[:2])
-        if locale is not None:
-            return locale
+        if likely_subtag is not None:
+            locale = _try_load_reducing(parse_locale(likely_subtag))
+            if locale is not None:
+                return locale
+
+        # If we did not find anything so far, try again with a
+        # simplified identifier that is just the language
+        likely_subtag = get_global('likely_subtags').get(language)
+        if likely_subtag is not None:
+            language2, _, script2, variant2 = parse_locale(likely_subtag)
+            locale = _try_load_reducing((language2, territory, script2, variant2))
+            if locale is not None:
+                return locale
 
         raise UnknownLocaleError(input_id)
 
index ccc30fa62ff93789c5cfe69f648366180a14c175..1605159411768df17ed75dfbfef97d23b4dbead6 100644 (file)
@@ -97,6 +97,11 @@ class TestLocaleClass:
         assert l.territory == 'TW'
         assert l.script == 'Hant'
 
+        l = Locale.parse('zh_CN')
+        assert l.language == 'zh'
+        assert l.territory == 'CN'
+        assert l.script == 'Hans'
+
         l = Locale.parse('und_AT')
         assert l.language == 'de'
         assert l.territory == 'AT'