diff options
| author | Jaap Roes <jaap.roes@gmail.com> | 2018-11-22 15:47:01 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-11-23 14:53:19 -0500 |
| commit | fc71bb11b164c68ad952cb5c3048fc858e6bc61e (patch) | |
| tree | fb0d3504b7253bd8becd001fcdb98159f6a5989b | |
| parent | c512912463c11428e572fb409704b351b1b26dfd (diff) | |
Improved readability of translation's to_locale().
| -rw-r--r-- | django/utils/translation/__init__.py | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index 28ab9c015f..7f5600be04 100644 --- a/django/utils/translation/__init__.py +++ b/django/utils/translation/__init__.py @@ -204,18 +204,18 @@ def to_language(locale): def to_locale(language): """Turn a language name (en-us) into a locale name (en_US).""" - language = language.lower() - parts = language.split('-') - try: - country = parts[1] - except IndexError: + language, _, country = language.lower().partition('-') + if not country: return language # 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:]) + country, _, tail = country.partition('-') + 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): |
