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

Plural with arrays #1196

Closed
aswetlow opened this issue Feb 6, 2019 · 7 comments
Closed

Plural with arrays #1196

aswetlow opened this issue Feb 6, 2019 · 7 comments

Comments

@aswetlow
Copy link

aswetlow commented Feb 6, 2019

Hey!

I may misunderstand something, but I can't get plurals to work with arrays.

Steps to reproduce:

const i18next = require('i18next');

i18next.init({
    lng: 'en',
    returnObjects: true,
    resources: {
        en: {
            translation: {
                "keyArray": ["hello world {{count}}", "hey {{count}}"],
                "keyArray_plural": ["hello world plural {{count}}", "hey plural {{count}}"],
            }
        }
    }
});
console.log(i18next.t('keyArray', {count: 1}));
console.log(i18next.t('keyArray', {count: 3}));

What I expect:

[ 'hello world 1', 'hey 1' ]
[ 'hello world plural 3', 'hey plural 3' ]

What I get:

[ 'hello world 1', 'hey 1' ]
[ 'hello world 3', 'hey 3' ]

Thanks in advance!

@jamuhl
Copy link
Member

jamuhl commented Feb 6, 2019

It runs through all the items in the array or object -> https://github.com/i18next/i18next/blob/master/src/Translator.js#L131 (to assert those inner values in the object, array, get interpolated, postprocessed,...)

which result in taking keyArray.0 and keyArray.1 in this case as no keyArray.0_plural and keyArray.1_plural exist.

a possible fix might be changing https://github.com/i18next/i18next/blob/master/src/Translator.js#L133 setting resUsedKey to _plural (or correct suffix based on language and count) if is array and has count in options.

While i agree this is an issue - i must say this is a rather edge case use case -> what's the idea behind using an array here instead of 2 keys?

@aswetlow
Copy link
Author

aswetlow commented Feb 6, 2019

Thanks for the quick reply. We at @jovotech work on a framework for voice apps (Alexa Skills, Google Actions) were we integrated i18next (Thank you for the great library!!!)
Variability in output is very important in voice. Voice assistants who answer the same simple phrases all the time are kind of boring. We have helper methods that pick a random translation for a key from an array.

Example:

// translation
 "WELCOME": [
      "Welcome",
      "Hey",
      "Hello"
    ]

Usage in code

let speech = this.speechBuilder().addT('WELCOME');
this.tell(speech); // Alexa says "Welcome", "Hey" or "Hello"

Docs: Jovo with i18next

This works perfectly fine! Would be cool if it worked with plural with arrays, too.

@jamuhl
Copy link
Member

jamuhl commented Feb 6, 2019

Ok i see...seems to make sense. Currently i'm hard on time - so might take me a few days to get that in.

If you feel confortable enough with the i18next codebase you might try a PR to speed this up...but not an easy one as needs to work for all pluralforms...

@jamuhl jamuhl added the issue label Feb 6, 2019
@aswetlow
Copy link
Author

aswetlow commented Feb 6, 2019

Indeed, not an easy one :) I'm afraid I don't have an overview for such a task.

There's no hurry. Thank you!

@jamuhl
Copy link
Member

jamuhl commented Feb 7, 2019

could you try with [email protected]

@aswetlow
Copy link
Author

aswetlow commented Feb 7, 2019

Works perfectly! Thank you!

@aswetlow aswetlow closed this as completed Feb 7, 2019
@jamuhl
Copy link
Member

jamuhl commented Feb 7, 2019

If you like this module don’t forget to star this repo. Make a tweet, share the word or have a look at our https://locize.com to support the devs of this project -> there are many ways to help this project 🙏

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

2 participants