Fixed #28546 -- Fixed translation's to_locale() with langauge subtags.

Thanks Brent Hand for the initial patch.
This commit is contained in:
Tim Graham 2017-09-07 10:09:07 -04:00
parent 9829b75d5b
commit 0c4ac12a7b
2 changed files with 14 additions and 7 deletions

View File

@ -58,14 +58,19 @@ def reset_cache(**kwargs):
def to_locale(language):
"""Turn a language name (en-us) into a locale name (en_US)."""
p = language.find('-')
if p >= 0:
# Get correct locale for sr-latn
if len(language[p + 1:]) > 2:
return language[:p].lower() + '_' + language[p + 1].upper() + language[p + 2:].lower()
return language[:p].lower() + '_' + language[p + 1:].upper()
language = language.lower()
parts = language.split('-')
try:
country = parts[1]
except IndexError:
return language
else:
return language.lower()
# A language with > 2 characters after the dash only has its first
# character after the dash capitalized; e.g. sr-latn becomes sr_Latn.
# A language with 2 characters after the dash has both characters
# capitalized; e.g. en-us becomes en_US.
parts[1] = country.title() if len(country) > 2 else country.upper()
return parts[0] + '_' + '-'.join(parts[1:])
def to_language(locale):

View File

@ -268,6 +268,8 @@ class TranslationTests(SimpleTestCase):
('sr-latn', 'sr_Latn'),
('sr-LATN', 'sr_Latn'),
# With private use subtag (x-informal).
('nl-nl-x-informal', 'nl_NL-x-informal'),
('NL-NL-X-INFORMAL', 'nl_NL-x-informal'),
('sr-latn-x-informal', 'sr_Latn-x-informal'),
('SR-LATN-X-INFORMAL', 'sr_Latn-x-informal'),
)