guillaumepotier/gettext.js

ngettext fails on with substitutions with nplurals=1

Closed this issue ยท 6 comments

We started using this gettext implementation recently, and it did everything we wanted to until we tried adding a language with a single plural form. Big thanks to @guillaumepotier ๐Ÿ‘

Our source language is English with nplurals=2, our source po file contains

msgid "You only have %1 lesson left to finish %2."
msgid_plural "You only have %1 lessons left to finish %2."
msgstr[0] ""
msgstr[1] ""

We use Transifex to translate that into Tetum which gives us back a po file like this

"Plural-Forms: nplurals=1; plural=0;\n"

msgid "You only have %1 lesson left to finish %2."
msgid_plural "You only have %1 lessons left to finish %2."
msgstr[0] "Ita hela deit lisaun %1 atu remata %2."

Note the lack of a msgstr[1]

This results ( po2json ) in a json file looking like this

    "": {
        "language": "tet",
        "plural-forms": "nplurals=1; plural=0;"
    },
    "You only have %1 lesson left to finish %2.": [
        "Ita hela deit lisaun %1 atu remata %2."
    ],

And this results in

                ngettext(
                    "You only have %1 lesson left to finish %2.",
                    "You only have %1 lessons left to finish %2.",
                    5,
                    "title"
                );

giving us Ita hela deit lisaun title atu remata undefined

I have hacked a solution where i just pad the single length arrays in the parsed json object, and I may be able to bypass that by getting transifex to correctly reference Tetum as a nplurals=2 language.

In case this represents a bug I got as far as a fork with a failing test, but no farther in the time allowed https://github.com/PeteCoward/gettext.js/tree/plurals1_behaviour

Hi @PeteCoward, thanks for your issue ! Strange that I never though of a language with only one plural form O_o. Which language it is by the way?

I'll try to think about a nice fix, thanks for your test on your fork, I'll re-use it ;)

Best

Hi @PeteCoward, just opened up a new PR that should fix your case (and your test suite). Could you please review and test it?

Best

Wow you are quick! I will test it today.

Tetum is the language spoken by most people in Timor Leste, an island ( half an island ) just north of Darwin.

It has no difference in the noun between "1 dog" and "2 dogs", "asu ida" "asu rua". So it is mostly nplurals=1.
Actually it does have nplurals=2 for some forms ( some portuguese loan words due to colonial history ) but I need to convince Transifex of that before I can support those cases.

Awesome! I'v just faced this issue also myself today in https://github.com/gbtami/pychess-variants translations for languages JA, TH, ZH where .po headers contain "Plural-Forms: nplurals=1; plural=0;\n"

Thx for your quick fix!

May i ask you to push a new release to npm also please?

Ahah very funny to have your two cases in such a short time span (and especially after so long, gettext.js is 5+ yrs old :p)

I've pushed it, v1.0.0

Enjoy