diff --git a/python/phonenumbers/prefix.py b/python/phonenumbers/prefix.py index 0e3680ef..04a82dee 100644 --- a/python/phonenumbers/prefix.py +++ b/python/phonenumbers/prefix.py @@ -85,6 +85,5 @@ def _prefix_description_for_number(data, longest_prefix, numobj, lang, script=No name = _find_lang(data[prefix], lang, script, region) if name is not None: return name - else: - return U_EMPTY_STRING + # Language not available for this prefix; try a less-specific (shorter) prefix. return U_EMPTY_STRING diff --git a/python/tests/geocodertest.py b/python/tests/geocodertest.py index 755d96e0..695979f4 100644 --- a/python/tests/geocodertest.py +++ b/python/tests/geocodertest.py @@ -178,6 +178,30 @@ def testGetDescriptionForNonGeographicalNumberWithGeocodingPrefix(self): # We have a geocoding prefix, but we shouldn't use it since this is not geographical. self.assertEqual("South Korea", description_for_number(KO_MOBILE, _ENGLISH)) + def testPrefixFallsBackToShorterPrefixWhenLanguageMissing(self): + # When a longer prefix exists in the geocoding data but lacks the requested + # language, the lookup must continue to shorter prefixes rather than returning + # empty immediately. This can occur when only a subset of sub-prefixes carry + # alternative-language entries for a region. + # + # Simulate: '1212' has 'en', '12123' exists only for 'de'. + # Asking for 'fr' on a number whose digits start with 12123 should fall back + # to '1212' and return the English name (since French falls back to English), + # not the empty string from '12123'. + TEST_GEOCODE_DATA['12123'] = {'de': u("TestDistrict")} + try: + # US_NUMBER3 is +1 212-812-0000; its prefix digits start with '12128' so + # it won't hit '12123'. Use a fabricated number whose digits begin '12123'. + test_num = FrozenPhoneNumber(country_code=1, national_number=2123000000) + # '12123' has no 'fr', '1212' has 'en' -> French should fall back to 'NY' + self.assertEqual("NY", _prefix_description_for_number( + TEST_GEOCODE_DATA, TEST_GEOCODE_LONGEST_PREFIX, test_num, "fr")) + # 'de' is directly present in '12123', so German gets the detailed name + self.assertEqual("TestDistrict", _prefix_description_for_number( + TEST_GEOCODE_DATA, TEST_GEOCODE_LONGEST_PREFIX, test_num, "de")) + finally: + del TEST_GEOCODE_DATA['12123'] + def testCoverage(self): # Python version extra tests invalid_number = PhoneNumber(country_code=210, national_number=123456)