Skip to content
This repository has been archived by the owner on May 23, 2019. It is now read-only.

Format array of values to sentence #99

Closed
rlivsey opened this issue Feb 23, 2015 · 9 comments
Closed

Format array of values to sentence #99

rlivsey opened this issue Feb 23, 2015 · 9 comments

Comments

@rlivsey
Copy link

rlivsey commented Feb 23, 2015

(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 output one, 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.

@ericf
Copy link
Collaborator

ericf commented Feb 23, 2015

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?

@ericf ericf added the question label Feb 23, 2015
@nfroidure
Copy link

I think @rlivsey meant it to be used with an arbitrary amount of items.

@ericf
Copy link
Collaborator

ericf commented Feb 23, 2015

@nfroidure @rlivsey okay, then can you provide more details on what you're trying to do with an example message and expected output?

@rlivsey
Copy link
Author

rlivsey commented Feb 23, 2015

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:

  • ['Friday'] -> "Friday"
  • ['Monday', 'Friday'] => "Monday and Friday"
  • ['Sunday', 'Monday', 'Friday'] => "Sunday, Monday and Friday"

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.

@rlivsey
Copy link
Author

rlivsey commented Feb 23, 2015

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."

@ericf
Copy link
Collaborator

ericf commented Feb 23, 2015

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 intl-listformat package (maybe?)

Here's the listPatterns data for English:

<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.

@rlivsey
Copy link
Author

rlivsey commented Feb 24, 2015

That would be fantastic. In the meantime I've added a to-sentence helper and am "sentencifying" content before passing it into the message formatter.

@rlivsey
Copy link
Author

rlivsey commented Jan 28, 2016

For anyone else stumbling on this issue, there's work going on to specify a list formatting API here: tc39/ecma402#33

@longlho
Copy link
Member

longlho commented May 16, 2019

closing since Intl.PluralFormat is released and there's still no ICU spec for list formatting

@longlho longlho closed this as completed May 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants