forked from django/django
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #29713 -- Added check that LANGUAGE_CODE uses standard language…
… id format.
- Loading branch information
1 parent
ee52044
commit 5db8d61
Showing
6 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import re | ||
|
||
from django.conf import settings | ||
from django.utils.translation.trans_real import language_code_re | ||
|
||
from . import Error, Tags, register | ||
|
||
|
||
@register(Tags.translation) | ||
def check_setting_language_code(app_configs, **kwargs): | ||
""" | ||
Errors if language code is in the wrong format. Language codes specification outlined by | ||
https://en.wikipedia.org/wiki/IETF_language_tag#Syntax_of_language_tags | ||
""" | ||
match_result = re.match(language_code_re, settings.LANGUAGE_CODE) | ||
errors = [] | ||
if not match_result: | ||
errors.append(Error( | ||
"LANGUAGE_CODE in settings.py is {}. It should be in the form ll or ll-cc where ll is the language and cc " | ||
"is the country. Examples include: it, de-at, es, pt-br. The full set of language codes specifications is " | ||
"outlined by https://en.wikipedia.org/wiki/IETF_language_tag#Syntax_of_language_tags".format( | ||
settings.LANGUAGE_CODE), | ||
id="translation.E001", | ||
)) | ||
return errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from django.core.checks.translation import check_setting_language_code | ||
from django.test import SimpleTestCase, override_settings | ||
|
||
|
||
class TranslationCheckTests(SimpleTestCase): | ||
|
||
@override_settings(LANGUAGE_CODE="eu") | ||
def test_valid_language_code_format_ll_only(self): | ||
result = check_setting_language_code(None) | ||
self.assertEqual(len(result), 0) | ||
|
||
@override_settings(LANGUAGE_CODE="eü") | ||
def test_invalid_language_code_format_ll_only(self): | ||
result = check_setting_language_code(None) | ||
self.assertEqual(len(result), 1) | ||
error = result[0] | ||
self.assertEqual(error.id, 'translation.E001') | ||
self.assertEqual(error.msg, ( | ||
"LANGUAGE_CODE in settings.py is eü. It should be in the form ll or ll-cc where ll is the language and cc " | ||
"is the country. Examples include: it, de-at, es, pt-br. The full set of language codes specifications is " | ||
"outlined by https://en.wikipedia.org/wiki/IETF_language_tag#Syntax_of_language_tags" | ||
)) | ||
|
||
@override_settings(LANGUAGE_CODE="en-US") | ||
def test_valid_language_code_format_ll_cc(self): | ||
result = check_setting_language_code(None) | ||
self.assertEqual(len(result), 0) | ||
|
||
@override_settings(LANGUAGE_CODE="en_US") | ||
def test_invalid_language_code_format_ll_cc(self): | ||
result = check_setting_language_code(None) | ||
self.assertEqual(len(result), 1) | ||
error = result[0] | ||
self.assertEqual(error.id, 'translation.E001') | ||
self.assertEqual(error.msg, ( | ||
"LANGUAGE_CODE in settings.py is en_US. It should be in the form ll or ll-cc where ll is the language and " | ||
"cc is the country. Examples include: it, de-at, es, pt-br. The full set of language codes specifications " | ||
"is outlined by https://en.wikipedia.org/wiki/IETF_language_tag#Syntax_of_language_tags" | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters