2019-03-08 06:40:58 +08:00
|
|
|
from django.core.checks import Error
|
|
|
|
from django.core.checks.translation import check_setting_language_code
|
|
|
|
from django.test import SimpleTestCase
|
2018-09-03 16:43:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TranslationCheckTests(SimpleTestCase):
|
|
|
|
|
2018-09-06 19:49:25 +08:00
|
|
|
def test_valid_language_code(self):
|
|
|
|
tags = (
|
|
|
|
'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
|
|
|
|
)
|
|
|
|
for tag in tags:
|
|
|
|
with self.subTest(tag), override_settings(LANGUAGE_CODE=tag):
|
|
|
|
self.assertEqual(check_setting_language_code(None), [])
|
2018-09-03 16:43:55 +08:00
|
|
|
|
2018-09-06 19:49:25 +08:00
|
|
|
def test_invalid_language_code(self):
|
|
|
|
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.
|
|
|
|
)
|
2019-03-08 06:40:58 +08:00
|
|
|
msg = 'You have provided an invalid value for the LANGUAGE_CODE setting: %s.'
|
2018-09-06 19:49:25 +08:00
|
|
|
for tag in 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'),
|
|
|
|
])
|