0

I am trying to create a translation file in simplified Chinese from an English source. This is all happening in a Flask project and I've been using Flask-Babel (so far successfully) to translate to Spanish and French. I added a Chinese translation file but I'm running into the following issue.

Consider the following strings in English:

msgid "One message"
msgid_plural "%(num)d messages"

When there is only one message, I don't want to display the number 1, I want the spelled out version.

Apparenly, in Chinese, there is no grammatical difference between singular and plural, so our translators only included one translation for both versions:

msgstr[0] "%(num)d [something in Chinese]"

When I tried to compile this file, I got the following error message:

unknown named placeholder 'num'

So I tried to duplicate the line like this ( is 1 in Chinese):

msgstr[0] "一 [something in Chinese]"
msgstr[1] "%(num)d [something in Chinese]"

But then I got this error:

msg has more translations than num_plurals of catalog

which makes sense: Chinese has a nplurals of 1 so there shouldn't be more than one msgstr.

I see two options at this stage:

  1. Cheat on my Chinese .po file and declare that nplurals = 2 with the same rule as English: "Plural-Forms: nplurals=2; plural=(n > 1)".
  2. Update all my source strings so that I always use %(num)d in the singular version if I need it in the plural version.

I'm not really satisfied with either option. Is there an alternative?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Arnaud
  • 750
  • 10
  • 25
  • Your first approach `msgstr[0] "%(num)d [something in Chinese]"` is correct and should work. Please post the entire po entry! Is there a `#, python-format` comment? And how exactly do you compile the po file? The regular GNU `msgfmt` has no problem with your example. – Guido Flohr Jul 27 '19 at 05:33

1 Answers1

0

This seems to be a bug in babel, not a problem of gettext.

The following PO excerpt is correct and compiles with msgfmt --check:

msgid ""
msgstr ""
...
"Language-Team: Chinese (simplified) <i18n-zh@googlegroups.com>\n"
"Language: zh_CN\n"
...
"Plural-Forms: nplurals=1; plural=0;\n"

#: example.c:1
#, python-format
msgid "One message"
msgid_plural "%(num)d messages"
msgstr[0] "%(num)d [something in Chinese]"

Until the bug is fixed, using option 1 (cheating on the plural-forms header) seems to be a viable workaround. Or just use msgfmt --check instead of the Python script for compiling the po file.

Guido Flohr
  • 1,871
  • 15
  • 28