summaryrefslogtreecommitdiff
path: root/django/utils/translation/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/utils/translation/__init__.py')
-rw-r--r--django/utils/translation/__init__.py14
1 files changed, 13 insertions, 1 deletions
diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
index 5c4a5e8c70..1788e82878 100644
--- a/django/utils/translation/__init__.py
+++ b/django/utils/translation/__init__.py
@@ -194,7 +194,19 @@ def check_for_language(lang_code):
def to_locale(language):
- return _trans.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:
+ 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:])
def get_language_from_request(request, check_path=False):