Git Product home page Git Product logo

Comments (9)

ericf avatar ericf commented on May 21, 2024

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?

from intl-messageformat.

nfroidure avatar nfroidure commented on May 21, 2024

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

from intl-messageformat.

ericf avatar ericf commented on May 21, 2024

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

from intl-messageformat.

rlivsey avatar rlivsey commented on May 21, 2024

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.

from intl-messageformat.

rlivsey avatar rlivsey commented on May 21, 2024

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

from intl-messageformat.

ericf avatar ericf commented on May 21, 2024

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.

from intl-messageformat.

rlivsey avatar rlivsey commented on May 21, 2024

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.

from intl-messageformat.

rlivsey avatar rlivsey commented on May 21, 2024

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

from intl-messageformat.

longlho avatar longlho commented on May 21, 2024

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

from intl-messageformat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.