diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index aa5cd33f5b..29ac60ad1d 100644 --- a/django/utils/translation/__init__.py +++ b/django/utils/translation/__init__.py @@ -201,9 +201,9 @@ def to_language(locale): def to_locale(language): """Turn a language name (en-us) into a locale name (en_US).""" - language, _, country = language.lower().partition('-') + lang, _, country = language.lower().partition('-') if not country: - return language + return language[:3].lower() + language[3:] # 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 @@ -212,7 +212,7 @@ def to_locale(language): country = country.title() if len(country) > 2 else country.upper() if tail: country += '-' + tail - return language + '_' + country + return lang + '_' + country def get_language_from_request(request, check_path=False): diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index 55cd35f7f3..5dd3593cac 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -313,12 +313,16 @@ class TranslationTests(SimpleTestCase): ('EN', 'en'), ('en-us', 'en_US'), ('EN-US', 'en_US'), + ('en_US', 'en_US'), # With > 2 characters after the dash. ('sr-latn', 'sr_Latn'), ('sr-LATN', 'sr_Latn'), + ('sr_Latn', 'sr_Latn'), # 3-char language codes. ('ber-MA', 'ber_MA'), ('BER-MA', 'ber_MA'), + ('BER_MA', 'ber_MA'), + ('ber_MA', 'ber_MA'), # With private use subtag (x-informal). ('nl-nl-x-informal', 'nl_NL-x-informal'), ('NL-NL-X-INFORMAL', 'nl_NL-x-informal'),