support for UN M.49 region codes in language

This commit is contained in:
Lex Berezhny 2019-07-15 16:12:55 -04:00
parent 03a58cf50a
commit d27a3b6421
3 changed files with 1137 additions and 5 deletions

View file

@ -379,7 +379,9 @@ class Language(Metadata):
self.language = parts.pop(0) self.language = parts.pop(0)
if parts and len(parts[0]) == 4: if parts and len(parts[0]) == 4:
self.script = parts.pop(0) self.script = parts.pop(0)
if parts and len(parts[0]) == 2: if parts and len(parts[0]) == 2 and parts[0].isalpha():
self.region = parts.pop(0)
if parts and len(parts[0]) == 3 and parts[0].isdigit():
self.region = parts.pop(0) self.region = parts.pop(0)
assert not parts, f"Failed to parse language tag: {langtag}" assert not parts, f"Failed to parse language tag: {langtag}"
@ -404,10 +406,13 @@ class Language(Metadata):
@property @property
def region(self) -> str: def region(self) -> str:
if self.message.region: if self.message.region:
return LocationMessage.Country.Name(self.message.region) r = LocationMessage.Country.Name(self.message.region)
return r[1:] if r.startswith('R') else r
@region.setter @region.setter
def region(self, region: str): def region(self, region: str):
if len(region) == 3:
region = 'R'+region
self.message.region = LocationMessage.Country.Value(region) self.message.region = LocationMessage.Country.Value(region)

File diff suppressed because one or more lines are too long

View file

@ -72,6 +72,13 @@ class TestLanguages(TestCase):
self.assertEqual(stream.languages[2].region, 'US') self.assertEqual(stream.languages[2].region, 'US')
self.assertEqual(stream.langtags, ['en', 'en-US', 'en-Latn-US']) self.assertEqual(stream.langtags, ['en', 'en-US', 'en-Latn-US'])
stream.languages.append('es-419')
self.assertEqual(stream.languages[3].langtag, 'es-419')
self.assertEqual(stream.languages[3].language, 'es')
self.assertEqual(stream.languages[3].script, None)
self.assertEqual(stream.languages[3].region, '419')
self.assertEqual(stream.langtags, ['en', 'en-US', 'en-Latn-US', 'es-419'])
stream = Stream() stream = Stream()
stream.languages.extend(['en-Latn-US', 'es-ES', 'de-DE']) stream.languages.extend(['en-Latn-US', 'es-ES', 'de-DE'])
self.assertEqual(stream.languages[0].language, 'en') self.assertEqual(stream.languages[0].language, 'en')