Refs #29713 -- Improved error message when LANGUAGE_CODE is invalid.
This commit is contained in:
parent
07daa487ae
commit
d4783048ef
|
@ -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 []
|
||||
|
|
|
@ -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: ``<value>``.
|
||||
|
||||
URLs
|
||||
----
|
||||
|
|
|
@ -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'),
|
||||
])
|
||||
|
|
Loading…
Reference in New Issue