Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complicated plurals don't work for me #45

Closed
eliOcs opened this issue Nov 14, 2012 · 7 comments
Closed

Complicated plurals don't work for me #45

eliOcs opened this issue Nov 14, 2012 · 7 comments

Comments

@eliOcs
Copy link

eliOcs commented Nov 14, 2012

I'm trying to get complicated plurals to work, here is a test code:

var i18next = require("i18next");

i18next.init({ fallbackLng: "en", debug: true }, function (t) {
    console.log("Doesn't work");
    console.log(i18next.t("key", { count: 0 }));
    console.log(i18next.t("key", { count: 1 }));
    console.log(i18next.t("key", { count: 2 }));
    console.log();
    console.log("Works!");
    console.log(i18next.t("simple", { count: 0 }));
    console.log(i18next.t("simple", { count: 1 }));
    console.log(i18next.t("simple", { count: 2 }));
});

Which produces the following output

currentLng set to: en
loaded file: locales/en/translation.json
Doesn't work
0 like
1 like
2 like

Works!
0 likes
1 like
2 likes

With this translation file:

{
    "key": "__count__ like",
    "key_plural_0": "No likes",
    "key_plural_2": "__count__ likes",

    "simple": "__count__ like",
    "simple_plural": "__count__ likes"
}

I've been trying to understand the code behind this, but I can't get it to work.

@jamuhl
Copy link
Member

jamuhl commented Nov 15, 2012

english has only singular and plural form -> so you don't need to suffix the keys for en. Only locales with more than one plural form (or zero) need to be suffixed.

@eliOcs
Copy link
Author

eliOcs commented Nov 15, 2012

With this translation .json

{
    "key": "__count__ like",
    "key_plural": "__count__ likes",
    "key_plural_0": "No likes",

    "simple": "__count__ like",
    "simple_plural": "__count__ likes"
}

The special case for 0 doesn't work. I was expecting:

No likes
1 like
2 likes

Instead of:

0 likes
1 like
2 likes

@jamuhl
Copy link
Member

jamuhl commented Nov 15, 2012

key_plural_0 would be for a zero based specialform in english - but english has no specialform for 0 (zero uses the regular plural).

i18next supports the plural like in given language and doesn't lookup special cases. You can solve what you try with context http://i18next.com/pages/doc_features.html#context

{
    "key_zero": "__count__ like", /* should never be called as zero uses plural form */
    "key_zero_plural": "No likes",
    "key_many": "__count__ like",
    "key_many_plural": "__count__ likes",
}

i18n.t('key', {
  count: 0, /* as normal */
  context: (count !== 0) ? 'many' : 'zero'
});

@eliOcs
Copy link
Author

eliOcs commented Nov 15, 2012

I completely misunderstood the plural functionality, but your workaround is perfect. Thanks!

@eliOcs eliOcs closed this as completed Nov 15, 2012
@jamuhl
Copy link
Member

jamuhl commented Nov 15, 2012

glad we found a solution. if you encounter any other issue, just let me know.

@EFF
Copy link

EFF commented May 14, 2013

I still can't manage to make plural form work in any language from Jade template.

.text=t("key", {count : 0}) // displays : default
.text=t("key", {count : 1}) // displays : default
.text=t("key", {count : 2}) // displays : default

{
"key" : "default"
"key_zero": "you have no foo"
"key_1_plural": "you have count foo",
"key_plural": "you have count foos",
}

I understand that plural forms are specific to languages but it looks like i still don't understand how it works.

@jamuhl
Copy link
Member

jamuhl commented May 14, 2013

looks strange to me:

// having
{
"key" : "default"
"key_plural": "you have __count__ foos",
}

// it should
.text=t("key", {count : 0}) // should displays : you have 0 foos
.text=t("key", {count : 1}) // displays : default (this one is ok)
.text=t("key", {count : 2}) // should displays : you have 2 foos

the key_zero and key_1_plural will only be accessed if you use the respecting keys (key_zero, key_1) or add the context option ( context = zero, context = 1).

when i run the tests in the client repository https://github.com/jamuhl/i18next/blob/master/spec/translate/translate.plurals.spec.js it works correctly...wondering where the problem with 0 and 2 comes from

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants