1
0
mirror of https://github.com/django/django.git synced 2025-03-06 07:22:32 +00:00

Consolidated duplicate to_locale() implementations.

Follow up to ac59ec8f1a34ea0e82bdb3c77422694e8016e0a7.
This commit is contained in:
Tim Graham 2018-05-12 12:38:53 -04:00
parent 305dee7bf9
commit 1b7d524cfa
4 changed files with 15 additions and 25 deletions

View File

@ -194,7 +194,19 @@ def check_for_language(lang_code):
def to_locale(language): 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): def get_language_from_request(request, check_path=False):

View File

@ -4,8 +4,6 @@
from django.conf import settings from django.conf import settings
from .trans_real import to_locale as trans_real_to_locale
def gettext(message): def gettext(message):
return message return message
@ -54,9 +52,6 @@ def check_for_language(x):
return True return True
to_locale = trans_real_to_locale
def get_language_from_request(request, check_path=False): def get_language_from_request(request, check_path=False):
return settings.LANGUAGE_CODE return settings.LANGUAGE_CODE

View File

@ -15,7 +15,8 @@ from django.core.exceptions import AppRegistryNotReady
from django.core.signals import setting_changed from django.core.signals import setting_changed
from django.dispatch import receiver from django.dispatch import receiver
from django.utils.safestring import SafeData, mark_safe 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. # Translations are cached in a dictionary for every language.
# The active translations are stored by threadid to make them thread local. # 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() 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): def to_language(locale):
"""Turn a locale name (en_US) into a language name (en-us).""" """Turn a locale name (en_US) into a language name (en-us)."""
p = locale.find('_') p = locale.find('_')

View File

@ -286,7 +286,6 @@ class TranslationTests(SimpleTestCase):
for lang, locale in tests: for lang, locale in tests:
with self.subTest(lang=lang): with self.subTest(lang=lang):
self.assertEqual(to_locale(lang), locale) self.assertEqual(to_locale(lang), locale)
self.assertEqual(trans_null.to_locale(lang), locale)
def test_to_language(self): def test_to_language(self):
self.assertEqual(trans_real.to_language('en_US'), 'en-us') self.assertEqual(trans_real.to_language('en_US'), 'en-us')