Comments (9)
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.
I think @rlivsey meant it to be used with an arbitrary amount of items.
from intl-messageformat.
@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.
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.
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.
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.
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.
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.
closing since Intl.PluralFormat
is released and there's still no ICU spec for list formatting
from intl-messageformat.
Related Issues (20)
- Mustache (and Backbone) integration HOT 3
- Formatting dynamic lists? HOT 4
- Better way to display currently selected item from a list HOT 1
- How to use formatMessage API ? HOT 1
- Outdated browser support information in README
- values for 0
- Need default format for time Type HOT 1
- Support datetime type HOT 3
- JS Import, Webpack HOT 6
- Typescript typings HOT 1
- Allow passing date param in string format HOT 1
- Empty string in plural syntax throws error HOT 1
- Use Intl.PluralRules
- ES6 module for browser HOT 2
- Expose parentLocale info via public API HOT 3
- How can I output the 24 hour time format (14:00 instead 2:00 PM)? HOT 2
- If adding a judgement of environment variable can be better HOT 1
- Causing crash in production environment HOT 3
- How to define default value for context variables? HOT 2
- 3.0 - TypeError: Cannot read property 'parse' of undefined HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from intl-messageformat.