Django - ValueError:复数形式表达式可能很危险

时间:2016-04-14 07:50:35

标签: python django internationalization gettext

我正在使用Microsoft Visual Studio中的Django 1.9。此外,我正在使用虚拟环境,其中安装了所有需要的python包。当我尝试编译消息以向站点添加翻译功能时,会引发以下错误:

ValueError: plural forms expression could be dangerous

更新1. Stacktrace:

Internal Server Error: /
Traceback (most recent call last):
File "C:\path-to-project\env\lib\site-packages\django\core\handlers\base.py", line 123, in get_response
response = middleware_method(request)
File "C:\path-to-project\env\lib\site-packages\django\middleware\locale.py", line 26, in process_request
translation.activate(language)
File "C:\path-to-project\env\lib\site-packages\django\utils\translation\__init__.py", line 154, in activate
return _trans.activate(language)
File "C:\path-to-project\env\lib\site-packages\django\utils\translation\trans_real.py", line 216, in activate
_active.value = translation(language)
File "C:\path-to-project\env\lib\site-packages\django\utils\translation\trans_real.py", line 205, in translation
_translations[language] = DjangoTranslation(language)
File "C:\path-to-project\env\lib\site-packages\django\utils\translation\trans_real.py", line 114, in __init__
self._add_installed_apps_translations()
File "C:\path-to-project\env\lib\site-packages\django\utils\translation\trans_real.py", line 160, in _add_installed_apps_translations
translation = self._new_gnu_trans(localedir)
File "C:\path-to-project\env\lib\site-packages\django\utils\translation\trans_real.py", line 140, in _new_gnu_trans
fallback=use_null_fallback)
File "C:\Python27\Lib\gettext.py", line 481, in translation
t = _translations.setdefault(key, class_(fp))
File "C:\Python27\Lib\gettext.py", line 182, in __init__
self._parse(fp)
File "C:\Python27\Lib\gettext.py", line 318, in _parse
self.plural = c2py(plural)
File "C:\Python27\Lib\gettext.py", line 95, in c2py
raise ValueError, 'plural forms expression could be dangerous'
ValueError: plural forms expression could be dangerous

我已经了解了这一点(plural forms expression could be dangerousDjango: gettext raises ValueError: 'plural forms expression could be dangerous'),但我发现的唯一解决方案是删除引用多个翻译的行,例如:

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

如果我在翻译文件中删除这些行,它仍会引发相同的错误,因为它会考虑位于虚拟环境中的其他文件。

更新2:如果我从命令行运行服务器,请使用

python manage.py runserver 

没有问题,一切都正常。但是,当我从Visual Studio运行该项目时,会引发错误。

有什么方法可以解决这个问题吗?

提前致谢。

0 个答案:

没有答案