Fixed #28546 -- Fixed translation's to_locale() with langauge subtags.
Thanks Brent Hand for the initial patch.
This commit is contained in:
parent
9829b75d5b
commit
0c4ac12a7b
|
@ -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):
|
||||
|
|
|
@ -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'),
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue