summaryrefslogtreecommitdiff
path: root/django/utils/translation
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2018-05-12 12:38:53 -0400
committerTim Graham <timograham@gmail.com>2018-05-12 12:58:21 -0400
commit1b7d524cfa7b7834af26c99407af66be6813938d (patch)
treebcde6682adf3713fd9234d4c25eaa164a75e4c47 /django/utils/translation
parent305dee7bf979ed3a9e2ce7507a37a6fc484e5263 (diff)
Consolidated duplicate to_locale() implementations.
Follow up to ac59ec8f1a34ea0e82bdb3c77422694e8016e0a7.
Diffstat (limited to 'django/utils/translation')
-rw-r--r--django/utils/translation/__init__.py14
-rw-r--r--django/utils/translation/trans_null.py5
-rw-r--r--django/utils/translation/trans_real.py20
3 files changed, 15 insertions, 24 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):
diff --git a/django/utils/translation/trans_null.py b/django/utils/translation/trans_null.py
index 5cda498336..a687572b69 100644
--- a/django/utils/translation/trans_null.py
+++ b/django/utils/translation/trans_null.py
@@ -4,8 +4,6 @@
from django.conf import settings
-from .trans_real import to_locale as trans_real_to_locale
-
def gettext(message):
return message
@@ -54,9 +52,6 @@ def check_for_language(x):
return True
-to_locale = trans_real_to_locale
-
-
def get_language_from_request(request, check_path=False):
return settings.LANGUAGE_CODE
diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py
index a0cff0837b..14e9e046a6 100644
--- a/django/utils/translation/trans_real.py
+++ b/django/utils/translation/trans_real.py
@@ -15,7 +15,8 @@ from django.core.exceptions import AppRegistryNotReady
from django.core.signals import setting_changed
from django.dispatch import receiver
from django.utils.safestring import SafeData, mark_safe
-from django.utils.translation import LANGUAGE_SESSION_KEY
+
+from . import LANGUAGE_SESSION_KEY, to_locale
# Translations are cached in a dictionary for every language.
# The active translations are stored by threadid to make them thread local.
@@ -56,23 +57,6 @@ def reset_cache(**kwargs):
get_supported_language_variant.cache_clear()
-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:
- return language
- else:
- # 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):
"""Turn a locale name (en_US) into a language name (en-us)."""
p = locale.find('_')