如何修复 "plural forms could be dangerous" django 错误?
How to fix "plural forms could be dangerous" django error?
我不明白。如果我将 Accept-Language
header 设置为 "en"(de、pl、es)以外的任何值或什至不存在的值(如 xxs),应用程序不会吐出这个错误,但是当我将它设置为 "en" 时它会出现。它只发生在 windows(最新的 gettext 工具)上。这是堆栈跟踪:
Traceback (most recent call last):
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\core\han
dlers\exception.py", line 39, in inner
response = get_response(request)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\core\han
dlers\base.py", line 244, in _legacy_get_response
response = middleware_method(request)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\middlewa
re\locale.py", line 29, in process_request
translation.activate(language)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\__init__.py", line 161, in activate
return _trans.activate(language)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\trans_real.py", line 238, in activate
_active.value = translation(language)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\trans_real.py", line 227, in translation
_translations[language] = DjangoTranslation(language)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\trans_real.py", line 129, in __init__
self._add_installed_apps_translations()
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\trans_real.py", line 176, in _add_installed_apps_translations
translation = self._new_gnu_trans(localedir)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\trans_real.py", line 156, in _new_gnu_trans
fallback=use_null_fallback)
File "C:\Python35\lib\gettext.py", line 426, in translation
t = _translations.setdefault(key, class_(fp))
File "C:\Python35\lib\gettext.py", line 162, in __init__
self._parse(fp)
File "C:\Python35\lib\gettext.py", line 297, in _parse
self.plural = c2py(plural)
File "C:\Python35\lib\gettext.py", line 76, in c2py
raise ValueError('plural forms expression could be dangerous')
ValueError: plural forms expression could be dangerous
我的 django.po
文件中 plural-forms
设置正确:
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
为什么会发生这种情况以及如何解决?
您的复数表达方式 (n != 1)
很可能被认为是危险的,因为它的包容性太大。 n doesn't equal 1
表示除 1
之外的任何值都将计算为 True
,包括布尔值、字符串或 None
.
尝试将表达式更改为使用等于、大于和小于运算来缩小表达式的范围。
在这里,我将向您详细介绍Plural-forms:
看到这个linkPlural-forms。它会对你有所帮助。
Note:This just link give a simple idea about plural
肯定对别人有帮助
我找到了答案 - 该错误的原因是我的无能:)。我 运行 django-admin makemessages
命令在我的项目的根目录中,所以在 _env 和里面的所有包旁边。此命令使用默认 django.po 文件模板为几个项目创建了语言文件,因此它们包含类似 plural-forms=INTEGER
和其他内容的内容,这导致了上述错误。
我不明白。如果我将 Accept-Language
header 设置为 "en"(de、pl、es)以外的任何值或什至不存在的值(如 xxs),应用程序不会吐出这个错误,但是当我将它设置为 "en" 时它会出现。它只发生在 windows(最新的 gettext 工具)上。这是堆栈跟踪:
Traceback (most recent call last):
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\core\han
dlers\exception.py", line 39, in inner
response = get_response(request)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\core\han
dlers\base.py", line 244, in _legacy_get_response
response = middleware_method(request)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\middlewa
re\locale.py", line 29, in process_request
translation.activate(language)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\__init__.py", line 161, in activate
return _trans.activate(language)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\trans_real.py", line 238, in activate
_active.value = translation(language)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\trans_real.py", line 227, in translation
_translations[language] = DjangoTranslation(language)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\trans_real.py", line 129, in __init__
self._add_installed_apps_translations()
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\trans_real.py", line 176, in _add_installed_apps_translations
translation = self._new_gnu_trans(localedir)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\trans_real.py", line 156, in _new_gnu_trans
fallback=use_null_fallback)
File "C:\Python35\lib\gettext.py", line 426, in translation
t = _translations.setdefault(key, class_(fp))
File "C:\Python35\lib\gettext.py", line 162, in __init__
self._parse(fp)
File "C:\Python35\lib\gettext.py", line 297, in _parse
self.plural = c2py(plural)
File "C:\Python35\lib\gettext.py", line 76, in c2py
raise ValueError('plural forms expression could be dangerous')
ValueError: plural forms expression could be dangerous
我的 django.po
文件中 plural-forms
设置正确:
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
为什么会发生这种情况以及如何解决?
您的复数表达方式 (n != 1)
很可能被认为是危险的,因为它的包容性太大。 n doesn't equal 1
表示除 1
之外的任何值都将计算为 True
,包括布尔值、字符串或 None
.
尝试将表达式更改为使用等于、大于和小于运算来缩小表达式的范围。
在这里,我将向您详细介绍Plural-forms:
看到这个linkPlural-forms。它会对你有所帮助。
Note:This just link give a simple idea about plural
肯定对别人有帮助
我找到了答案 - 该错误的原因是我的无能:)。我 运行 django-admin makemessages
命令在我的项目的根目录中,所以在 _env 和里面的所有包旁边。此命令使用默认 django.po 文件模板为几个项目创建了语言文件,因此它们包含类似 plural-forms=INTEGER
和其他内容的内容,这导致了上述错误。