2019-03-08 06:40:58 +08:00
|
|
|
from django.core.checks import Error
|
2018-11-26 20:19:13 +08:00
|
|
|
from django.core.checks.translation import (
|
|
|
|
check_language_settings_consistent, check_setting_language_code,
|
|
|
|
check_setting_languages, check_setting_languages_bidi,
|
|
|
|
)
|
2019-03-08 06:40:58 +08:00
|
|
|
from django.test import SimpleTestCase
|
2018-09-03 16:43:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TranslationCheckTests(SimpleTestCase):
|
|
|
|
|
2018-11-26 20:19:13 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.valid_tags = (
|
2018-09-06 19:49:25 +08:00
|
|
|
'en', # language
|
|
|
|
'mas', # language
|
|
|
|
'sgn-ase', # language+extlang
|
|
|
|
'fr-CA', # language+region
|
|
|
|
'es-419', # language+region
|
|
|
|
'zh-Hans', # language+script
|
|
|
|
'ca-ES-valencia', # language+region+variant
|
|
|
|
# FIXME: The following should be invalid:
|
|
|
|
'sr@latin', # language+script
|
|
|
|
)
|
2018-11-26 20:19:13 +08:00
|
|
|
self.invalid_tags = (
|
2019-03-08 06:40:58 +08:00
|
|
|
None, # invalid type: None.
|
|
|
|
123, # invalid type: int.
|
|
|
|
b'en', # invalid type: bytes.
|
2018-09-06 19:49:25 +08:00
|
|
|
'eü', # non-latin characters.
|
|
|
|
'en_US', # locale format.
|
|
|
|
'en--us', # empty subtag.
|
|
|
|
'-en', # leading separator.
|
|
|
|
'en-', # trailing separator.
|
|
|
|
'en-US.UTF-8', # language tag w/ locale encoding.
|
2019-03-09 20:17:50 +08:00
|
|
|
'en_US.UTF-8', # locale format - language w/ region and encoding.
|
2018-09-06 19:49:25 +08:00
|
|
|
'ca_ES@valencia', # locale format - language w/ region and variant.
|
|
|
|
# FIXME: The following should be invalid:
|
|
|
|
# 'sr@latin', # locale instead of language tag.
|
|
|
|
)
|
2018-11-26 20:19:13 +08:00
|
|
|
|
|
|
|
def test_valid_language_code(self):
|
|
|
|
for tag in self.valid_tags:
|
|
|
|
with self.subTest(tag), self.settings(LANGUAGE_CODE=tag):
|
|
|
|
self.assertEqual(check_setting_language_code(None), [])
|
|
|
|
|
|
|
|
def test_invalid_language_code(self):
|
2019-04-24 21:31:03 +08:00
|
|
|
msg = 'You have provided an invalid value for the LANGUAGE_CODE setting: %r.'
|
2018-11-26 20:19:13 +08:00
|
|
|
for tag in self.invalid_tags:
|
2019-03-08 06:40:58 +08:00
|
|
|
with self.subTest(tag), self.settings(LANGUAGE_CODE=tag):
|
|
|
|
self.assertEqual(check_setting_language_code(None), [
|
|
|
|
Error(msg % tag, id='translation.E001'),
|
|
|
|
])
|
2018-11-26 20:19:13 +08:00
|
|
|
|
|
|
|
def test_valid_languages(self):
|
|
|
|
for tag in self.valid_tags:
|
|
|
|
with self.subTest(tag), self.settings(LANGUAGES=[(tag, tag)]):
|
|
|
|
self.assertEqual(check_setting_languages(None), [])
|
|
|
|
|
|
|
|
def test_invalid_languages(self):
|
2019-04-24 21:31:03 +08:00
|
|
|
msg = 'You have provided an invalid language code in the LANGUAGES setting: %r.'
|
2018-11-26 20:19:13 +08:00
|
|
|
for tag in self.invalid_tags:
|
|
|
|
with self.subTest(tag), self.settings(LANGUAGES=[(tag, tag)]):
|
|
|
|
self.assertEqual(check_setting_languages(None), [
|
|
|
|
Error(msg % tag, id='translation.E002'),
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_valid_languages_bidi(self):
|
|
|
|
for tag in self.valid_tags:
|
|
|
|
with self.subTest(tag), self.settings(LANGUAGES_BIDI=[tag]):
|
|
|
|
self.assertEqual(check_setting_languages_bidi(None), [])
|
|
|
|
|
|
|
|
def test_invalid_languages_bidi(self):
|
2019-04-24 21:31:03 +08:00
|
|
|
msg = 'You have provided an invalid language code in the LANGUAGES_BIDI setting: %r.'
|
2018-11-26 20:19:13 +08:00
|
|
|
for tag in self.invalid_tags:
|
|
|
|
with self.subTest(tag), self.settings(LANGUAGES_BIDI=[tag]):
|
|
|
|
self.assertEqual(check_setting_languages_bidi(None), [
|
|
|
|
Error(msg % tag, id='translation.E003'),
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_inconsistent_language_settings(self):
|
|
|
|
msg = (
|
|
|
|
'You have provided a value for the LANGUAGE_CODE setting that is '
|
|
|
|
'not in the LANGUAGES setting.'
|
|
|
|
)
|
2019-04-17 14:41:54 +08:00
|
|
|
with self.settings(LANGUAGE_CODE='fr', LANGUAGES=[('en', 'English')]):
|
2018-11-26 20:19:13 +08:00
|
|
|
self.assertEqual(check_language_settings_consistent(None), [
|
|
|
|
Error(msg, id='translation.E004'),
|
|
|
|
])
|