2018-09-03 16:43:55 +08:00
|
|
|
from django.conf import settings
|
2020-01-08 15:05:43 +08:00
|
|
|
from django.utils.translation import get_supported_language_variant
|
2018-09-03 16:43:55 +08:00
|
|
|
from django.utils.translation.trans_real import language_code_re
|
|
|
|
|
|
|
|
from . import Error, Tags, register
|
|
|
|
|
2018-09-06 19:49:25 +08:00
|
|
|
E001 = Error(
|
2019-04-24 21:31:03 +08:00
|
|
|
'You have provided an invalid value for the LANGUAGE_CODE setting: {!r}.',
|
2018-09-06 19:49:25 +08:00
|
|
|
id='translation.E001',
|
|
|
|
)
|
|
|
|
|
2018-11-26 20:19:13 +08:00
|
|
|
E002 = Error(
|
2019-04-24 21:31:03 +08:00
|
|
|
'You have provided an invalid language code in the LANGUAGES setting: {!r}.',
|
2018-11-26 20:19:13 +08:00
|
|
|
id='translation.E002',
|
|
|
|
)
|
|
|
|
|
|
|
|
E003 = Error(
|
2019-04-24 21:31:03 +08:00
|
|
|
'You have provided an invalid language code in the LANGUAGES_BIDI setting: {!r}.',
|
2018-11-26 20:19:13 +08:00
|
|
|
id='translation.E003',
|
|
|
|
)
|
|
|
|
|
|
|
|
E004 = Error(
|
|
|
|
'You have provided a value for the LANGUAGE_CODE setting that is not in '
|
|
|
|
'the LANGUAGES setting.',
|
|
|
|
id='translation.E004',
|
|
|
|
)
|
|
|
|
|
2018-09-03 16:43:55 +08:00
|
|
|
|
|
|
|
@register(Tags.translation)
|
|
|
|
def check_setting_language_code(app_configs, **kwargs):
|
2019-03-08 06:40:58 +08:00
|
|
|
"""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)]
|
2018-09-06 19:49:25 +08:00
|
|
|
return []
|
2018-11-26 20:19:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
@register(Tags.translation)
|
|
|
|
def check_setting_languages(app_configs, **kwargs):
|
|
|
|
"""Error if LANGUAGES setting is invalid."""
|
|
|
|
return [
|
|
|
|
Error(E002.msg.format(tag), id=E002.id)
|
|
|
|
for tag, _ in settings.LANGUAGES if not isinstance(tag, str) or not language_code_re.match(tag)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@register(Tags.translation)
|
|
|
|
def check_setting_languages_bidi(app_configs, **kwargs):
|
|
|
|
"""Error if LANGUAGES_BIDI setting is invalid."""
|
|
|
|
return [
|
|
|
|
Error(E003.msg.format(tag), id=E003.id)
|
|
|
|
for tag in settings.LANGUAGES_BIDI if not isinstance(tag, str) or not language_code_re.match(tag)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@register(Tags.translation)
|
|
|
|
def check_language_settings_consistent(app_configs, **kwargs):
|
|
|
|
"""Error if language settings are not consistent with each other."""
|
2020-01-08 15:05:43 +08:00
|
|
|
try:
|
|
|
|
get_supported_language_variant(settings.LANGUAGE_CODE)
|
|
|
|
except LookupError:
|
2019-04-17 14:41:54 +08:00
|
|
|
return [E004]
|
2020-01-08 15:05:43 +08:00
|
|
|
else:
|
|
|
|
return []
|