summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArmin Ronacher <armin.ronacher@active-4.com>2013-07-28 23:13:21 +0200
committerArmin Ronacher <armin.ronacher@active-4.com>2013-07-28 23:13:21 +0200
commit4117268d15375da88cbe86da873133c67493a982 (patch)
treeaadb07b63b2f12d298f0196e0612b7b0bb1faaeb
parente38b777613b1dfdff0fa6f2433d16ca5e7e1b718 (diff)
downloadbabel-4117268d15375da88cbe86da873133c67493a982.tar.gz
Fixed #37 (likely subtag resolving)
This fixes some locales not loading correctly (like zh_CN) that are defined through simplified rules in the likely-subtag mapping.
-rw-r--r--CHANGES5
-rw-r--r--babel/core.py40
-rw-r--r--tests/test_core.py5
3 files changed, 35 insertions, 15 deletions
diff --git a/CHANGES b/CHANGES
index 286e3d6..13f1ad8 100644
--- 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
-----------
diff --git a/babel/core.py b/babel/core.py
index 6b9cd41..6e6e6d6 100644
--- a/babel/core.py
+++ b/babel/core.py
@@ -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)
diff --git a/tests/test_core.py b/tests/test_core.py
index ccc30fa..1605159 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -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'