-
Notifications
You must be signed in to change notification settings - Fork 77
Format array of values to sentence #99
Comments
This is supported, but you must use numeric argument names in the message, e.g: var msg = 'The message has: {0}, {1}, and {2} values.';
var mf = new IntlMessageFormat(msg);
console.log(mf.format(['one', 'two', 'three']));
// => 'The message has: one, two, and three values.' Is this what you mean? If not, could you provide a more detailed example of what you're trying to do? |
I think @rlivsey meant it to be used with an arbitrary amount of items. |
@nfroidure @rlivsey okay, then can you provide more details on what you're trying to do with an example message and expected output? |
Is it possible with an unknown/arbitrary number of items? (@nfroidure exactly) One use-case I currently have is displaying some selected days of the week, in this case I know there's a max of 7 options but in other situations it's an unbounded list which the user could generate. Eg given an array of days / any other values, I'd want to generate:
I could build a higher level helper much like to_sentence in Rails, and store the separator and other options in the locale, which by the sounds of it is probably the way to go if this kind of thing isn't in the core lib. |
Another example would be Slack's email access message when logging in. which displays a message like: "If you have an @foo.com, @bar.com or @baz.com email address, you can create an account." Currently in order to do that I'm having to convert the domains to a sentence externally and then my message format is simply: "If you have an {domainSentence} email address, you can create an account." It would be nice to be able to have that all inline somehow: "If you have an {domains, array, sentence} email address, you can create an account." |
I checked the CLDR data and we have this information available. Interesting enough the ICU project mentions a TODO to propose an API, code, and tests for the Java and C implementations. I would be for adding this feature to either this package directly, or another Here's the <listPatterns>
<listPattern>
<listPatternPart type="start">{0}, {1}</listPatternPart>
<listPatternPart type="middle">{0}, {1}</listPatternPart>
<listPatternPart type="end">{0}, and {1}</listPatternPart>
<listPatternPart type="2">{0} and {1}</listPatternPart>
</listPattern>
</listPatterns> The one thing I couldn't find is a mutually exclusive list using "or" instead of "and", but I can dig deeper to see if it exists. |
That would be fantastic. In the meantime I've added a |
For anyone else stumbling on this issue, there's work going on to specify a list formatting API here: tc39/ecma402#33 |
closing since |
(this might not be the right place to post this, feel free to close & point me elsewhere)
I've a list of values which should be displayed as a sentence, eg:
['one', 'two', 'three']
should outputone, two and three
.I'm not sure if this is possible with the ICU message format, or whether it's a higher level problem out of scope of this project?
I don't see an example of this kind of format in the ICU docs or elsewhere, but it seems like it would be a fairly common problem.
The text was updated successfully, but these errors were encountered: