diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index a9447d7896..9ec8085dae 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -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): diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index c33677c76a..449def5363 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -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'), )