Improved readability of translation's to_locale().

This commit is contained in:
Jaap Roes 2018-11-22 15:47:01 +01:00 committed by Tim Graham
parent c512912463
commit fc71bb11b1
1 changed files with 7 additions and 7 deletions

View File

@ -204,18 +204,18 @@ def to_language(locale):
def to_locale(language): def to_locale(language):
"""Turn a language name (en-us) into a locale name (en_US).""" """Turn a language name (en-us) into a locale name (en_US)."""
language = language.lower() language, _, country = language.lower().partition('-')
parts = language.split('-') if not country:
try:
country = parts[1]
except IndexError:
return language return language
# A language with > 2 characters after the dash only has its first # A language with > 2 characters after the dash only has its first
# character after the dash capitalized; e.g. sr-latn becomes sr_Latn. # character after the dash capitalized; e.g. sr-latn becomes sr_Latn.
# A language with 2 characters after the dash has both characters # A language with 2 characters after the dash has both characters
# capitalized; e.g. en-us becomes en_US. # capitalized; e.g. en-us becomes en_US.
parts[1] = country.title() if len(country) > 2 else country.upper() country, _, tail = country.partition('-')
return parts[0] + '_' + '-'.join(parts[1:]) country = country.title() if len(country) > 2 else country.upper()
if tail:
country += '-' + tail
return language + '_' + country
def get_language_from_request(request, check_path=False): def get_language_from_request(request, check_path=False):