French translation raises "ValueError('invalid token in plural form: %s' % value)"

Viewed 1306

I want to handle a french version of my website.

I use Django 2.2 with i18n and I already set locale variables in settings.py.

# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGES = (
    ('en', _('English')),
    ('fr', _('French')),
    ('it', _('Italian')),
    ('es', _('Spanish')),
)

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)

TIME_ZONE = 'Europe/Paris'

USE_I18N = True

USE_L10N = True

USE_TZ = True

When I use ./manage.py makemessages -l fr, I correctly have a django.po french file but after ./manage.py compilemessages -l fr the server crashes with the following error (trimed) :

  File "/usr/lib/python3.7/gettext.py", line 93, in _tokenize
    raise ValueError('invalid token in plural form: %s' % value)
ValueError: invalid token in plural form: EXPRESSION

English, Italian and Spanish translations work well

EDIT : Well, the issue has been resolved, but I'm not really sure how. I deleted my venv, recreated it and french translation suddenly worked. Upgrading from Django 2.2.1 to 2.2.2 may be what caused the resolution.

2 Answers

For other language facing this error:

There exists a line that tells Django evaluating this expression, decide which form of the word it should use, and for some languages, this expression is not written, e.g. Farsi.

For these languages, a default line is written in your main .po file(not the specific ones):

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

Here the EXPRESSION part should be changed to your language.

HERE you can read the exact format of EXPRESSION, but for short if your language has just two forms for singular and plural form change the line to this:

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

And recompile your messages.

I had the same issue. The reason was that i accidentally wrote a word in messages.po file in configuration lines (that are on top of .po file, containing "Project-Id-Version", "Plural-Forms", ...)

So i checked what changes i made with my VSC (git) and it was fixed. Don't forget to recompile your .mo files

Related