Skip to content

Commit

Permalink
Improved readability of translation's to_locale().
Browse files Browse the repository at this point in the history
  • Loading branch information
jaap3 authored and timgraham committed Nov 23, 2018
1 parent c512912 commit fc71bb1
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions django/utils/translation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit fc71bb1

Please sign in to comment.