mirror of https://github.com/django/django.git
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
|
from . import Error, Tags, register
|
||||||
|
|
||||||
E001 = Error(
|
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',
|
id='translation.E001',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@register(Tags.translation)
|
@register(Tags.translation)
|
||||||
def check_setting_language_code(app_configs, **kwargs):
|
def check_setting_language_code(app_configs, **kwargs):
|
||||||
"""
|
"""Error if LANGUAGE_CODE setting is invalid."""
|
||||||
Errors if language code setting is invalid.
|
tag = settings.LANGUAGE_CODE
|
||||||
"""
|
if not isinstance(tag, str) or not language_code_re.match(tag):
|
||||||
if not language_code_re.match(settings.LANGUAGE_CODE):
|
return [Error(E001.msg.format(tag), id=E001.id)]
|
||||||
return [E001]
|
|
||||||
return []
|
return []
|
||||||
|
|
|
@ -440,7 +440,7 @@ Translation
|
||||||
The following checks are performed on your translation configuration:
|
The following checks are performed on your translation configuration:
|
||||||
|
|
||||||
* **translation.E001**: You have provided an invalid value for the
|
* **translation.E001**: You have provided an invalid value for the
|
||||||
:setting:`LANGUAGE_CODE` setting.
|
:setting:`LANGUAGE_CODE` setting: ``<value>``.
|
||||||
|
|
||||||
URLs
|
URLs
|
||||||
----
|
----
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from django.core.checks.translation import E001, check_setting_language_code
|
from django.core.checks import Error
|
||||||
from django.test import SimpleTestCase, override_settings
|
from django.core.checks.translation import check_setting_language_code
|
||||||
|
from django.test import SimpleTestCase
|
||||||
|
|
||||||
|
|
||||||
class TranslationCheckTests(SimpleTestCase):
|
class TranslationCheckTests(SimpleTestCase):
|
||||||
|
@ -22,6 +23,9 @@ class TranslationCheckTests(SimpleTestCase):
|
||||||
|
|
||||||
def test_invalid_language_code(self):
|
def test_invalid_language_code(self):
|
||||||
tags = (
|
tags = (
|
||||||
|
None, # invalid type: None.
|
||||||
|
123, # invalid type: int.
|
||||||
|
b'en', # invalid type: bytes.
|
||||||
'eü', # non-latin characters.
|
'eü', # non-latin characters.
|
||||||
'en_US', # locale format.
|
'en_US', # locale format.
|
||||||
'en--us', # empty subtag.
|
'en--us', # empty subtag.
|
||||||
|
@ -33,9 +37,9 @@ class TranslationCheckTests(SimpleTestCase):
|
||||||
# FIXME: The following should be invalid:
|
# FIXME: The following should be invalid:
|
||||||
# 'sr@latin', # locale instead of language tag.
|
# 'sr@latin', # locale instead of language tag.
|
||||||
)
|
)
|
||||||
|
msg = 'You have provided an invalid value for the LANGUAGE_CODE setting: %s.'
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
with self.subTest(tag), override_settings(LANGUAGE_CODE=tag):
|
with self.subTest(tag), self.settings(LANGUAGE_CODE=tag):
|
||||||
result = check_setting_language_code(None)
|
self.assertEqual(check_setting_language_code(None), [
|
||||||
self.assertEqual(result, [E001])
|
Error(msg % tag, id='translation.E001'),
|
||||||
self.assertEqual(result[0].id, 'translation.E001')
|
])
|
||||||
self.assertEqual(result[0].msg, 'You have provided an invalid value for the LANGUAGE_CODE setting.')
|
|
||||||
|
|
Loading…
Reference in New Issue