Skip to content

Commit 2ea483b

Browse files
committed
Add LOCALE_NORMALIZATION_MAP for locale equivalences
1 parent c265fd4 commit 2ea483b

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

python/phonenumbers/geocoder.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
raise
6565

6666

67+
_LOCALE_NORMALIZATION_MAP = {"zh_TW": "zh_Hant", "zh_HK": "zh_Hant", "zh_MO": "zh_Hant"}
68+
69+
6770
def _may_fall_back_to_english(lang):
6871
# Don't fall back to English if the requested language is among the following:
6972
# - Chinese
@@ -72,16 +75,38 @@ def _may_fall_back_to_english(lang):
7275
return lang != "zh" and lang != "ja" and lang != "ko"
7376

7477

78+
def _full_locale(lang, script, region):
79+
if script is not None:
80+
if region is not None:
81+
return "%s_%s_%s" % (lang, script, region)
82+
else:
83+
return "%s_%s" % (lang, script)
84+
elif region is not None:
85+
return "%s_%s" % (lang, region)
86+
else:
87+
return lang
88+
89+
7590
def _find_lang(langdict, lang, script, region):
7691
"""Return the entry in the dictionary for the given language information."""
77-
# First look for lang, script as a combination
78-
lang_script = "%s_%s" % (lang, script)
79-
if lang_script in langdict:
80-
return langdict[lang_script]
92+
# Check if we should map this to a different locale.
93+
full_locale = _full_locale(lang, script, region)
94+
if (full_locale in _LOCALE_NORMALIZATION_MAP and
95+
_LOCALE_NORMALIZATION_MAP[full_locale] in langdict):
96+
return langdict[_LOCALE_NORMALIZATION_MAP[full_locale]]
97+
# First look for the full locale
98+
if full_locale in langdict:
99+
return langdict[full_locale]
100+
# Then look for lang, script as a combination
101+
if script is not None:
102+
lang_script = "%s_%s" % (lang, script)
103+
if lang_script in langdict:
104+
return langdict[lang_script]
81105
# Next look for lang, region as a combination
82-
lang_region = "%s_%s" % (lang, region)
83-
if lang_region in langdict:
84-
return langdict[lang_region]
106+
if region is not None:
107+
lang_region = "%s_%s" % (lang, region)
108+
if lang_region in langdict:
109+
return langdict[lang_region]
85110
# Fall back to bare language code lookup
86111
if lang in langdict:
87112
return langdict[lang]

python/tests/geocodertest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def testCoverage(self):
169169
TEST_GEOCODE_DATA['1650960'] = {'en': u'Mountain View, CA',
170170
"en_GB": u'Mountain View California',
171171
"en_US": u'Mountain View, Sunny California',
172+
"en_Xyzz_US": u'MTV - xyzz',
172173
"en_Latn": u'MountainView'}
173174
# The following test might one day return "Mountain View California"
174175
self.assertEqual("United States",
@@ -179,7 +180,18 @@ def testCoverage(self):
179180
description_for_number(US_NUMBER2, _ENGLISH, script="Latn"))
180181
self.assertEqual("United States",
181182
description_for_number(US_NUMBER2, _ENGLISH, script="Latn", region="GB"))
183+
self.assertEqual("MTV - xyzz",
184+
description_for_number(US_NUMBER2, _ENGLISH, script="Xyzz", region="US"))
185+
self.assertEqual("Mountain View, Sunny California",
186+
description_for_number(US_NUMBER2, _ENGLISH, script="Zazz", region="US"))
182187
# Get a different result when there is a script-specific variant
183188
self.assertEqual("MountainView",
184189
description_for_number(US_NUMBER2, _ENGLISH, script="Latn", region="US"))
185190
TEST_GEOCODE_DATA['1650960'] = {'en': u'Mountain View, CA'}
191+
192+
# Test the locale mapping
193+
TEST_GEOCODE_DATA['8868'] = {'zh': u'Chinese', 'zh_Hant': u'Hant-specific'}
194+
tw_number = FrozenPhoneNumber(country_code=886, national_number=810080123)
195+
self.assertEqual("Hant-specific",
196+
description_for_number(tw_number, "zh", region="TW"))
197+
del TEST_GEOCODE_DATA['8868']

0 commit comments

Comments
 (0)