From d4783048efac81f73f3b8785696111547a31bcf1 Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Thu, 7 Mar 2019 22:40:58 +0000 Subject: [PATCH] Refs #29713 -- Improved error message when LANGUAGE_CODE is invalid. --- django/core/checks/translation.py | 11 +++++------ docs/ref/checks.txt | 2 +- tests/check_framework/test_translation.py | 18 +++++++++++------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/django/core/checks/translation.py b/django/core/checks/translation.py index cf69a5c084..7ad6d2364c 100644 --- a/django/core/checks/translation.py +++ b/django/core/checks/translation.py @@ -4,16 +4,15 @@ from django.utils.translation.trans_real import language_code_re from . import Error, Tags, register E001 = Error( - 'You have provided an invalid value for the LANGUAGE_CODE setting.', + 'You have provided an invalid value for the LANGUAGE_CODE setting: {}.', id='translation.E001', ) @register(Tags.translation) def check_setting_language_code(app_configs, **kwargs): - """ - Errors if language code setting is invalid. - """ - if not language_code_re.match(settings.LANGUAGE_CODE): - return [E001] + """Error if LANGUAGE_CODE setting is invalid.""" + tag = settings.LANGUAGE_CODE + if not isinstance(tag, str) or not language_code_re.match(tag): + return [Error(E001.msg.format(tag), id=E001.id)] return [] diff --git a/docs/ref/checks.txt b/docs/ref/checks.txt index 117830ffbc..9f2c538fed 100644 --- a/docs/ref/checks.txt +++ b/docs/ref/checks.txt @@ -440,7 +440,7 @@ Translation The following checks are performed on your translation configuration: * **translation.E001**: You have provided an invalid value for the - :setting:`LANGUAGE_CODE` setting. + :setting:`LANGUAGE_CODE` setting: ````. URLs ---- diff --git a/tests/check_framework/test_translation.py b/tests/check_framework/test_translation.py index fe811fe77b..7f6d93b38f 100644 --- a/tests/check_framework/test_translation.py +++ b/tests/check_framework/test_translation.py @@ -1,5 +1,6 @@ -from django.core.checks.translation import E001, check_setting_language_code -from django.test import SimpleTestCase, override_settings +from django.core.checks import Error +from django.core.checks.translation import check_setting_language_code +from django.test import SimpleTestCase class TranslationCheckTests(SimpleTestCase): @@ -22,6 +23,9 @@ class TranslationCheckTests(SimpleTestCase): def test_invalid_language_code(self): tags = ( + None, # invalid type: None. + 123, # invalid type: int. + b'en', # invalid type: bytes. 'eü', # non-latin characters. 'en_US', # locale format. 'en--us', # empty subtag. @@ -33,9 +37,9 @@ class TranslationCheckTests(SimpleTestCase): # FIXME: The following should be invalid: # 'sr@latin', # locale instead of language tag. ) + msg = 'You have provided an invalid value for the LANGUAGE_CODE setting: %s.' for tag in tags: - with self.subTest(tag), override_settings(LANGUAGE_CODE=tag): - result = check_setting_language_code(None) - self.assertEqual(result, [E001]) - self.assertEqual(result[0].id, 'translation.E001') - self.assertEqual(result[0].msg, 'You have provided an invalid value for the LANGUAGE_CODE setting.') + with self.subTest(tag), self.settings(LANGUAGE_CODE=tag): + self.assertEqual(check_setting_language_code(None), [ + Error(msg % tag, id='translation.E001'), + ])