i18next/i18next-parser

Problem understanding treatment of plural forms

Closed this issue · 2 comments

Hey,

I have a problem understanding how the parser handles plural forms.

Ordinarily, I would crate a plural key in the following way

{
 "contacts" : "__total__ contact",
 "contacts_plural" : "__total__ contacts"
}

And use it in a script via i18n.t('contacts', {total: total}), with the plural selected by i18next according to the value of total.

The parser does not detect such usages (I get only a "contacts" key in the outputted JSON). (I don't see how it could, in fact, detect these and distinguish them from translations that are just parameterized). Is there a special syntax I should be using so the parser can detect plurals that are declared in this fashion?

Moreover, in this parser unit test it appears to know that the second plural should have a special form for the value "12". How is this determined from the input text "asd t('test_plural:first') t('test_plural:second')"?

So the parser doesn't actually detect plural forms. The reason is very simple, there is no way for the parser to know which plural form you need. In your exemple, maybe you need contacts_plural, but maybe you need contacts_plural_3. The parser cannot evaluate your variable total and know what to populate the catalog with. As a result, the parser doesn't do anything, it is your job to manually add the plural forms you need.

That being said, the test you mention checks that those manually added plural forms don't get deleted by the parser (the parser cleans the catalog of any key that is not in the code and plural forms aren't very often used as such in the code).

Let me know if anything else isn't clear.

Yeah I couldn't see how the parser could determine plurals without there being a special syntax for key names (the line "Handles plural keys of the form key_plural and key_plural_0" in the Readme made me wonder if this was the case).

Thanks for your help!