Tuesday, July 10, 2018

“django-admin.py makemessages -l en” adds Plural-Forms to the output file

Leave a Comment

Every time I run django-admin.py makemessages -l en, it adds the following line to the djangojs.po file:

"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" 

after that running python manage.py runserver break out whith this error:

ValueError: plural forms expression could be dangerous 

Of course removing that line fix the error and make it go away.
How can I prevent this line from being added?

1 Answers

Answers 1

Replacing the line with "Plural-Forms: nplurals=2; plural= (n != 1);\n" in all the occurences of .po files shdould get you going!

gettext usually translates word to word when you use Django translations (i18n & l10n). But it gets trickier when you want to translate (mostly) nouns, into its singular/plural form. For instance, consider this example:

if n == 1:   string = "%d file is deleted"%n else:   string = "%d files are deleted"%n 

To handle the translation of plurals in internationalization, Plural-forms is the standard to follow. Python-Django implements ngettext in the following manner: (visit doc for your Django version)

ngettext('%d file is deleted', '%d files are deleted', n) % n 

In most of the languages, n!=1 is considered plural. In the above example, when n=1 Django has to consider '%d file is deleted' and '%d files are deleted' when n!=1 for translations. Hence the denifition for plural-forms in .po files goes like this: "Plural-Forms: nplurals=2; plural= (n != 1);\n"

Now, when you run makemessages, Django is trying to help you by adding "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" line in the .po files, hinting this line has to be replaced with our definition of plural-forms.

Note: Not every language would have 2 plurals, for instance, Japanese, Vietnamese, Korean, Thai doesn't need to differentiate between singular and plural (hence Plural-Forms: nplurals=1; plural=0;). On the other hand, there are some languages like Romanian, Lithuanian, Russian, Polish where nplurals=3. Check out this link for further examples.

Hopefully in your case, nplurals=1. If not visit this link for plurals-form.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment