Saturday, February 18, 2017

How to fix “plural forms could be dangerous” django error?

Leave a Comment

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:

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:

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

Why does this happen and how to fix it?

3 Answers

Answers 1

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.

Answers 2

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.

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

Answers 3

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

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

Note:This just link give a simple idea about plural

It surely helps to others

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment