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

String with more than one count #908

Closed
victornpb opened this issue Apr 13, 2017 · 14 comments
Closed

String with more than one count #908

victornpb opened this issue Apr 13, 2017 · 14 comments

Comments

@victornpb
Copy link

victornpb commented Apr 13, 2017

Today I encountered a situation that need to have a string with more than one count, and I was not sure how to deal with it.

i18next.t('foo', { count_cars: x, count_persons: y });
{
     "foo": "There are {count_cars} car(s) with {count_persons} person(s) in it."
}

How would I structure this to offer the 4 possible plural/singular combinations?

@jamuhl
Copy link
Member

jamuhl commented Apr 13, 2017

should be possible like: http://i18next.com/translate/nesting/#withpluralandoptions

@jamuhl
Copy link
Member

jamuhl commented Apr 20, 2017

did this solve your demand?

@victornpb
Copy link
Author

yes, not as clean as I wish, but it does.

@jamuhl
Copy link
Member

jamuhl commented Apr 26, 2017

yes...not the easiest thing to reach...looking at languages having multiple pluralforms that stuff gets complicated very fast. If you got an idea to improve that?

@victornpb
Copy link
Author

I thought we can have something like this.

t('foo.msg', {girl: 2, boy: 1}) // "They have 2 girls and 1 boy."
function t(key, options){
	var string = i18next.t(key, options);
	string = string.replace(/\{\{\+(\w+)\}\}/g, function(m, token){
		return i18next.t(key+"_"+token, {count: token});
	});
	return string;
}

Your locale files would look like this.

{
	"foo": {
		"msg": "They have {{+girl}} and {{+boy}}.",
		"msg_girl": "{{count}} girl",
		"msg_girl_plural": "{{count}} girls",
		"msg_boy": "{{count}} boy",
		"msg_boy_plural": "{{count}} boys"
	}
}

or this will also works

{
	"foo": {
		"msg": "They have {{girl}} {{+girl}} and {{boy}} {{+boy}}.",
		"msg_girl": "girl",
		"msg_girl_plural": "girls",
		"msg_boy": "boy",
		"msg_boy_plural": "boys"
	}
}

Let me know if you like the concept.

@jamuhl
Copy link
Member

jamuhl commented Apr 27, 2017

hm...rather inspiring, what might work is:

{
	"foo": {
		"msg": "They have {{girl}} ${girl} and {{boy}} ${boy}.",
		"msg_girl": "girl",
		"msg_girl_plural": "girls",
		"msg_boy": "boy",
		"msg_boy_plural": "boys"
	}
}

which is a 'regular' nesting call, but here:

https://github.com/i18next/i18next/blob/master/src/Interpolator.js#L126

value = fc(handleHasOptions.call(this, match[1].trim()), clonedOptions);

we call basically i18next.t with new cloned options there we might transform the girl/boy count value into a regular count for the nested call (match[1].trim() is girl or boy).

// eventual we should add a flag for this behaviour to keep it backwards compatible?!?
t('foo.msg', {girl: 2, boy: 1, transformCounts: true })

@victornpb
Copy link
Author

Can I make it a contribution?

Oh and I see lots of things that I would argue about the nesting operator. I have feelings that it should be deprecated on how it is now, but I don't have enough background on the motivation to have this feature to begin with. I have a strong feeling that nesting with options shouldn't be a thing, I can see it being useful in some circumstances, but at least shouldn't be encouraged.

@jamuhl
Copy link
Member

jamuhl commented Apr 27, 2017

Agree on nesting with options is somehow a little over the top...was introduced in v1. But as it's not overly complicated i would keep it.

But i think we could add the convert for "girl" to count without the need of transformCounts - if new options included girl and count i think it's save and backwards compatible.

If you like you can make the contribution. PR would be welcome...else i will see if i got on this (hopefully sooner than later)

@jamuhl
Copy link
Member

jamuhl commented May 10, 2017

argh...missed that one...will reopen for implementation...hopefully get this done soon...as it looks like a nice improvement

@victornpb
Copy link
Author

Oh, I started implementing this to submit it as a PR, but I got stuck at some point, I'm not familiar with the code base and then I totally forgot about it.

@jamuhl
Copy link
Member

jamuhl commented Oct 30, 2017

didn't got to it myself...so still hanging here for a long time :( but lets keep it in the hope xmas holiday gives the time

@jamuhl
Copy link
Member

jamuhl commented Apr 18, 2018

guess we will close this for now...PR would still be welcome but i guess for the few cases this is needed the current solution is an option.

If more often needed i guess using ICU would be easier: https://github.com/i18next/i18next-icu

@jamuhl jamuhl closed this as completed Apr 18, 2018
@andreialecu
Copy link

andreialecu commented Mar 16, 2021

I stumbled upon this issue while looking for similar functionality.

Almost switched away from the i18next format because of it and was thinking of moving to icu or fluent.

This seems to be now be possible via https://www.i18next.com/translation-function/nesting#passing-options-to-nestings

The example in the docs works, but I find this clearer:

{
      "girlsAndBoys": "They have $t(girls, {\"count\": {{girls}} }) and $t(boys, {\"count\": {{boys}} })",
      "boys": "{{count}} boy",
      "boys_plural": "{{count}} boys",
      "girls": "{{count}} girl",
      "girls_plural": "{{count}} girls"
}
i18next.t('girlsAndBoys', {girls: 3, boys: 2}); // They have 3 girls and 2 boys 

@ScreamZ
Copy link

ScreamZ commented Jan 4, 2022

@andreialecu hello :) the world is small mate

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

No branches or pull requests

4 participants