-
-
Notifications
You must be signed in to change notification settings - Fork 629
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
Pick random values from array? #1070
Comments
Hm...it goes through every individual value in the array and applies that postProcessor: https://github.com/i18next/i18next/blob/master/src/Translator.js#L106 But still it should go through it with the array...https://github.com/i18next/i18next/blob/master/src/Translator.js#L171 Can you debug why it does not enter here: https://github.com/i18next/i18next/blob/master/src/Translator.js#L210 |
It goes here: https://github.com/i18next/i18next/blob/master/src/Translator.js#L92 Either way, my postProcessor is only given access to 1 value at a time. What I want to do is receive all array values, pick one at random, and return that so as to randomize strings. Seems like a fairly simple and common use case. If that way is not possible given how arrays are treated, how would you implement string randomization in i18next? Thanks, Andrea |
Hm...then you should get res set here: https://github.com/i18next/i18next/blob/master/src/Translator.js#L110 -> your array (and n time called postprocessor with each array items) then it goes here: https://github.com/i18next/i18next/blob/master/src/Translator.js#L172 and finally here: https://github.com/i18next/i18next/blob/master/src/Translator.js#L211 right? looks to me like it should theoretically work - guess need to create a sample to reproduce |
In the original issue, if you copy and paste all the code snippets sequentially (and require i18next) and then do |
Should have written "guess I need to create a sample to reproduce" -> not hard to do...just time consuming |
Arghh...make sense. Code lands here after array gets set to res: https://github.com/i18next/i18next/blob/master/src/Translator.js#L182 -> there is no extend https://github.com/i18next/i18next/blob/master/src/Translator.js#L172 as we wrote all extend functions to expect a string -> which makes sense as we go over each array item separately. Guess we can't change this without making postProcessors, ... complexer and without making a breaking change...so i would suggest doing: i18next.randomFromT(key, opts) {
const value = i18next.t(key, opts);
if (Array.isArray(value)) {
return value[Math.floor(Math.random() * value.length - 1)];
} else {
return value;
}
} And call that function for those cases |
Ok, so we're 50% of the way there. The following test case works fine: const i18n = require('i18next')
const langStrings = {
'en' : {
'translation' : {
'WELCOME_MESSAGE' : 'Hello',
'MULTIPLE_WELCOME': ['Hello' , 'Hello2']
}
}
}
const opts = {
lng: 'en',
resources: langStrings
};
i18n.init(opts);
i18n.randomT = function (key, values) {
const value = i18n.t(key, { returnObjects: true });
if (Array.isArray(value)) {
return value[Math.floor(Math.random() * value.length)];
} else {
return value;
}
}
console.log(i18n.randomT('MULTIPLE_WELCOME')); However 😄 What if I want to use sprintf? const i18n = require('i18next')
const sprintf = require('i18next-sprintf-postprocessor')
const langStrings = {
'en' : {
'translation' : {
'WELCOME_MESSAGE' : 'Hello',
'MULTIPLE_WELCOME': ['Hello %s' , 'Hello2 %s'],
}
}
}
const opts = {
lng: 'en',
resources: langStrings,
overloadTranslationOptionHandler : sprintf.overloadTranslationOptionHandler
};
i18n
.use(sprintf)
.init(opts);
i18n.randomT = function (key, values) {
const value = i18n.t(key, {
returnObjects: true,
postProcess: 'sprintf',
sprintf: values
});
if (Array.isArray(value)) {
return value[Math.floor(Math.random() * value.length)];
} else {
return value;
}
}
console.log('Final output', i18n.randomT('MULTIPLE_WELCOME', 'sir'));
// -> outputs 'Hello %s' instead of 'Hello sir' |
You will have to https://github.com/i18next/i18next-sprintf-postProcessor/blob/master/src/index.js#L19 in your function |
I did not :( Any ideas here would be great, as it seems to be a limitation
with the architecture of the i18next code (e.g. array values get processed
individually before being passed on to the postprocessors)
…On Tue, Aug 7, 2018 at 4:40 PM Blake DeLee ***@***.***> wrote:
I'm having the same issue as @muttoni <https://github.com/muttoni>, and
I've tried to put @jamuhl <https://github.com/jamuhl>'s suggestion in,
but still have this issue. I can't get the postprocess to work with
sprintf. Did you end up solving this, @muttoni
<https://github.com/muttoni>?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1070 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AZzJo8FOfgeZzTnGLefGsd75MzoQ6IMWks5uObT_gaJpZM4VG6W->
.
|
i18n.randomSprintfT = function () {
const args = arguments;
let values = [];
for (var i = 1; i < args.length; i++) {
values.push(args[i]);
}
const value = i18n.t(args[0], {
returnObjects: true,
postProcess: 'sprintf',
sprintf: values
});
if (Array.isArray(value)) {
return value[Math.floor(Math.random() * value.length)];
} else {
return value;
}
} console.log('Final output', i18n.randomT('MULTIPLE_WELCOME', 'sir')); |
Could this be closed? Seems to be inactive for some time. Honestly not even can catch up current state of this issue. |
Yes, thanks. Solved with your help. |
Hey, I'm a bit stumped. I'm trying to write a postProcessor plugin that will detect if a value is an Array and return a random value from it. This is to help with randomization of responses. It's not working because when the value is an array the postProcessor is not even called and instead returns undefined.
What I have so far:
my localization:
my postProcessor:
my istantiation:
When I run:
I also tried making a overloadTranslationOptionHandler where I specify
returnObjects : true
, but the issue is I only get access to the individual items of the array one at a time in the postProcessor.Any ideas?
The text was updated successfully, but these errors were encountered: