Showing posts with label django-i18n. Show all posts
Showing posts with label django-i18n. Show all posts

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.

Read More

Wednesday, April 27, 2016

Alowing 'fuzzy' translations in django pages?

Leave a Comment

I've done some research and found that django translations don't show up when a string is marked as "fuzzy".

However, I haven't been able to find any documentation on whether I can override this behaviour.

Is there a Django setting that can be used to allow Django (or gettext) to use "fuzzy translations"?


I know a lot of the automated translations won't be perfect, but this is for demonstration, development and testing for an open-source product.

I'd rather have users be able to develop in their own language with "approximate" translations then use that as an incentive to check them off as they go.

1 Answers

Answers 1

It would be unfortunate to show these translations as some of them are most certainly wrong. You are supposed to remove the fuzzy tag when you update the translations and revise the guessed translations that are marked as fuzzy.

However, you may run a tool to quickly delete the fuzzy markers from a .po file: Removing all fuzzy entries of a PO file


UPDATE

Here is a great overview of the GNU gettext work-flow: https://www.gnu.org/software/gettext/manual/gettext.html#Overview

It is msgfmt that strips the fuzzy translations. It has an option --use-fuzzy that includes the fuzzy translations.

msgfmt is wrapped by compilemessages django admin command, which since version 1.8 has the --use-fuzzy option too (https://docs.djangoproject.com/en/1.9/ref/django-admin/#compilemessages)

Read More