如何修复“复数形式可能是危险的”django错误?

时间:2021-01-10 17:04:56

I don't get it. If I set the Accept-Language header to anything other than "en" (de, pl, es) or even something that doesn't exist (like xxs), the app doesn't spit out this error, but it does when I set it to "en". It happens only on windows (newest gettext tools). Here's the stack trace:

我不明白。如果我将Accept-Language标头设置为除“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

I have plural-forms set up correctly in my django.po file:

我在django.po文件中正确设置了复数形式:

"Plural-Forms: nplurals=2; plural=(n != 1);\n"

Why does this happen and how to fix it?

为什么会发生这种情况以及如何解决?

3 个解决方案

#1


5  

I found an answer - the reason for that error is my incompetence :). I ran django-admin makemessages command in the root of my project, so next to _env and all the packages inside. This command created language files for a couple of projects with the default django.po file template, so they contained something like plural-forms=INTEGER and other stuff and this was causing the above error.

我找到了答案 - 错误的原因是我的无能:)。我在我的项目的根目录中运行了django-admin makemessages命令,所以在_env旁边和所有的包里面。此命令使用默认的django.po文件模板为几个项目创建语言文件,因此它们包含复数形式= INTEGER和其他内容,这导致上述错误。

#2


2  

Here I will give you all the details about Plural-forms:

在这里,我将为您提供有关复数形式的所有详细信息:

See this link Plural-forms.It will helps to you.

看到这个链接Plural-forms.It会对你有所帮助。

Note:This just link give a simple idea about plural

注意:这个链接给出了关于复数的简单概念

It surely helps to others

这对其他人肯定有帮助

#3


1  

Your plural expression (n != 1) is most likely considered dangerous because it's too inclusive. n doesn't equal 1 means any value except 1 will evaluate to True including booleans, strings or None.

您的复数表达式(n!= 1)很可能被认为是危险的,因为它太具包容性。 n不等于1表示除1之外的任何值将评估为True,包括布尔值,字符串或无。

Try changing your expression to use equals, greater than and less than operations to narrow down the scope of the expression.

尝试更改表达式以使用等于,大于和小于操作来缩小表达式的范围。

#1


5  

I found an answer - the reason for that error is my incompetence :). I ran django-admin makemessages command in the root of my project, so next to _env and all the packages inside. This command created language files for a couple of projects with the default django.po file template, so they contained something like plural-forms=INTEGER and other stuff and this was causing the above error.

我找到了答案 - 错误的原因是我的无能:)。我在我的项目的根目录中运行了django-admin makemessages命令,所以在_env旁边和所有的包里面。此命令使用默认的django.po文件模板为几个项目创建语言文件,因此它们包含复数形式= INTEGER和其他内容,这导致上述错误。

#2


2  

Here I will give you all the details about Plural-forms:

在这里,我将为您提供有关复数形式的所有详细信息:

See this link Plural-forms.It will helps to you.

看到这个链接Plural-forms.It会对你有所帮助。

Note:This just link give a simple idea about plural

注意:这个链接给出了关于复数的简单概念

It surely helps to others

这对其他人肯定有帮助

#3


1  

Your plural expression (n != 1) is most likely considered dangerous because it's too inclusive. n doesn't equal 1 means any value except 1 will evaluate to True including booleans, strings or None.

您的复数表达式(n!= 1)很可能被认为是危险的,因为它太具包容性。 n不等于1表示除1之外的任何值将评估为True,包括布尔值,字符串或无。

Try changing your expression to use equals, greater than and less than operations to narrow down the scope of the expression.

尝试更改表达式以使用等于,大于和小于操作来缩小表达式的范围。