intl-messageformat 1.0.3 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -19
- package/dist/intl-messageformat-with-locales.js +1095 -346
- package/dist/intl-messageformat-with-locales.js.map +1 -1
- package/dist/intl-messageformat-with-locales.min.js +4 -2
- package/dist/intl-messageformat-with-locales.min.js.map +1 -1
- package/dist/intl-messageformat.js +309 -186
- package/dist/intl-messageformat.js.map +1 -1
- package/dist/intl-messageformat.min.js +1 -1
- package/dist/intl-messageformat.min.js.map +1 -1
- package/dist/locale-data/af.js +2 -1
- package/dist/locale-data/agq.js +1 -0
- package/dist/locale-data/ak.js +1 -1
- package/dist/locale-data/am.js +1 -1
- package/dist/locale-data/ar.js +28 -1
- package/dist/locale-data/as.js +1 -1
- package/dist/locale-data/asa.js +1 -1
- package/dist/locale-data/ast.js +1 -1
- package/dist/locale-data/az.js +4 -1
- package/dist/locale-data/bas.js +1 -0
- package/dist/locale-data/be.js +1 -1
- package/dist/locale-data/bem.js +1 -1
- package/dist/locale-data/bez.js +1 -1
- package/dist/locale-data/bg.js +1 -1
- package/dist/locale-data/bh.js +1 -0
- package/dist/locale-data/bm.js +2 -1
- package/dist/locale-data/bn.js +2 -1
- package/dist/locale-data/bo.js +2 -1
- package/dist/locale-data/br.js +1 -1
- package/dist/locale-data/brx.js +1 -1
- package/dist/locale-data/bs.js +3 -1
- package/dist/locale-data/ca.js +6 -1
- package/dist/locale-data/ce.js +1 -0
- package/dist/locale-data/cgg.js +1 -1
- package/dist/locale-data/chr.js +1 -1
- package/dist/locale-data/ckb.js +2 -0
- package/dist/locale-data/cs.js +1 -1
- package/dist/locale-data/cu.js +1 -0
- package/dist/locale-data/cy.js +1 -1
- package/dist/locale-data/da.js +2 -1
- package/dist/locale-data/dav.js +1 -0
- package/dist/locale-data/de.js +6 -1
- package/dist/locale-data/dje.js +1 -0
- package/dist/locale-data/dsb.js +1 -0
- package/dist/locale-data/dua.js +1 -0
- package/dist/locale-data/dv.js +1 -0
- package/dist/locale-data/dyo.js +1 -0
- package/dist/locale-data/dz.js +1 -1
- package/dist/locale-data/ebu.js +1 -0
- package/dist/locale-data/ee.js +2 -1
- package/dist/locale-data/el.js +2 -1
- package/dist/locale-data/en.js +107 -1
- package/dist/locale-data/eo.js +1 -1
- package/dist/locale-data/es.js +26 -1
- package/dist/locale-data/et.js +1 -1
- package/dist/locale-data/eu.js +1 -1
- package/dist/locale-data/ewo.js +1 -0
- package/dist/locale-data/fa.js +2 -1
- package/dist/locale-data/ff.js +4 -1
- package/dist/locale-data/fi.js +1 -1
- package/dist/locale-data/fil.js +1 -1
- package/dist/locale-data/fo.js +2 -1
- package/dist/locale-data/fr.js +46 -1
- package/dist/locale-data/fur.js +1 -1
- package/dist/locale-data/fy.js +1 -1
- package/dist/locale-data/ga.js +1 -1
- package/dist/locale-data/gd.js +1 -1
- package/dist/locale-data/gl.js +1 -1
- package/dist/locale-data/gsw.js +3 -1
- package/dist/locale-data/gu.js +1 -1
- package/dist/locale-data/guw.js +1 -0
- package/dist/locale-data/guz.js +1 -0
- package/dist/locale-data/gv.js +1 -1
- package/dist/locale-data/ha.js +4 -1
- package/dist/locale-data/haw.js +1 -1
- package/dist/locale-data/he.js +1 -1
- package/dist/locale-data/hi.js +1 -1
- package/dist/locale-data/hr.js +2 -1
- package/dist/locale-data/hsb.js +1 -0
- package/dist/locale-data/hu.js +1 -1
- package/dist/locale-data/hy.js +1 -1
- package/dist/locale-data/id.js +1 -1
- package/dist/locale-data/ig.js +1 -1
- package/dist/locale-data/ii.js +1 -1
- package/dist/locale-data/in.js +1 -0
- package/dist/locale-data/is.js +1 -1
- package/dist/locale-data/it.js +3 -1
- package/dist/locale-data/iu.js +2 -0
- package/dist/locale-data/iw.js +1 -0
- package/dist/locale-data/ja.js +1 -1
- package/dist/locale-data/jbo.js +1 -0
- package/dist/locale-data/jgo.js +1 -1
- package/dist/locale-data/ji.js +1 -0
- package/dist/locale-data/jmc.js +1 -1
- package/dist/locale-data/jv.js +1 -0
- package/dist/locale-data/jw.js +1 -0
- package/dist/locale-data/ka.js +1 -1
- package/dist/locale-data/kab.js +1 -1
- package/dist/locale-data/kaj.js +1 -0
- package/dist/locale-data/kam.js +1 -0
- package/dist/locale-data/kcg.js +1 -0
- package/dist/locale-data/kde.js +1 -1
- package/dist/locale-data/kea.js +1 -1
- package/dist/locale-data/khq.js +1 -0
- package/dist/locale-data/ki.js +1 -0
- package/dist/locale-data/kk.js +1 -1
- package/dist/locale-data/kkj.js +1 -1
- package/dist/locale-data/kl.js +1 -1
- package/dist/locale-data/kln.js +1 -0
- package/dist/locale-data/km.js +1 -1
- package/dist/locale-data/kn.js +1 -1
- package/dist/locale-data/ko.js +2 -1
- package/dist/locale-data/kok.js +1 -0
- package/dist/locale-data/ks.js +1 -1
- package/dist/locale-data/ksb.js +1 -1
- package/dist/locale-data/ksf.js +1 -0
- package/dist/locale-data/ksh.js +1 -1
- package/dist/locale-data/ku.js +1 -0
- package/dist/locale-data/kw.js +1 -1
- package/dist/locale-data/ky.js +1 -1
- package/dist/locale-data/lag.js +1 -1
- package/dist/locale-data/lb.js +1 -0
- package/dist/locale-data/lg.js +1 -1
- package/dist/locale-data/lkt.js +1 -1
- package/dist/locale-data/ln.js +4 -1
- package/dist/locale-data/lo.js +1 -1
- package/dist/locale-data/lrc.js +2 -0
- package/dist/locale-data/lt.js +1 -1
- package/dist/locale-data/lu.js +1 -0
- package/dist/locale-data/luo.js +1 -0
- package/dist/locale-data/luy.js +1 -0
- package/dist/locale-data/lv.js +1 -1
- package/dist/locale-data/mas.js +2 -1
- package/dist/locale-data/mer.js +1 -0
- package/dist/locale-data/mfe.js +1 -0
- package/dist/locale-data/mg.js +1 -1
- package/dist/locale-data/mgh.js +1 -0
- package/dist/locale-data/mgo.js +1 -1
- package/dist/locale-data/mk.js +1 -1
- package/dist/locale-data/ml.js +1 -1
- package/dist/locale-data/mn.js +2 -1
- package/dist/locale-data/mo.js +1 -0
- package/dist/locale-data/mr.js +1 -1
- package/dist/locale-data/ms.js +4 -1
- package/dist/locale-data/mt.js +1 -1
- package/dist/locale-data/mua.js +1 -0
- package/dist/locale-data/my.js +1 -1
- package/dist/locale-data/mzn.js +1 -0
- package/dist/locale-data/nah.js +1 -0
- package/dist/locale-data/naq.js +1 -1
- package/dist/locale-data/nb.js +2 -1
- package/dist/locale-data/nd.js +1 -1
- package/dist/locale-data/ne.js +2 -1
- package/dist/locale-data/nl.js +7 -1
- package/dist/locale-data/nmg.js +1 -0
- package/dist/locale-data/nn.js +1 -1
- package/dist/locale-data/nnh.js +1 -1
- package/dist/locale-data/no.js +1 -0
- package/dist/locale-data/nqo.js +1 -0
- package/dist/locale-data/nr.js +1 -1
- package/dist/locale-data/nso.js +1 -1
- package/dist/locale-data/nus.js +1 -0
- package/dist/locale-data/ny.js +1 -0
- package/dist/locale-data/nyn.js +1 -1
- package/dist/locale-data/om.js +2 -1
- package/dist/locale-data/or.js +1 -1
- package/dist/locale-data/os.js +2 -1
- package/dist/locale-data/pa.js +3 -1
- package/dist/locale-data/pap.js +1 -0
- package/dist/locale-data/pl.js +1 -1
- package/dist/locale-data/prg.js +1 -0
- package/dist/locale-data/ps.js +1 -1
- package/dist/locale-data/pt.js +9 -1
- package/dist/locale-data/qu.js +3 -0
- package/dist/locale-data/rm.js +1 -1
- package/dist/locale-data/rn.js +1 -0
- package/dist/locale-data/ro.js +2 -1
- package/dist/locale-data/rof.js +1 -1
- package/dist/locale-data/ru.js +6 -1
- package/dist/locale-data/rw.js +1 -0
- package/dist/locale-data/rwk.js +1 -1
- package/dist/locale-data/sah.js +1 -1
- package/dist/locale-data/saq.js +1 -1
- package/dist/locale-data/sbp.js +1 -0
- package/dist/locale-data/sdh.js +1 -0
- package/dist/locale-data/se.js +3 -1
- package/dist/locale-data/seh.js +1 -1
- package/dist/locale-data/ses.js +1 -1
- package/dist/locale-data/sg.js +1 -1
- package/dist/locale-data/sh.js +1 -0
- package/dist/locale-data/shi.js +3 -1
- package/dist/locale-data/si.js +1 -1
- package/dist/locale-data/sk.js +1 -1
- package/dist/locale-data/sl.js +1 -1
- package/dist/locale-data/sma.js +1 -0
- package/dist/locale-data/smi.js +1 -0
- package/dist/locale-data/smj.js +1 -0
- package/dist/locale-data/smn.js +1 -0
- package/dist/locale-data/sms.js +1 -0
- package/dist/locale-data/sn.js +1 -1
- package/dist/locale-data/so.js +4 -1
- package/dist/locale-data/sq.js +3 -1
- package/dist/locale-data/sr.js +9 -1
- package/dist/locale-data/ss.js +1 -1
- package/dist/locale-data/ssy.js +1 -1
- package/dist/locale-data/st.js +1 -1
- package/dist/locale-data/sv.js +3 -1
- package/dist/locale-data/sw.js +4 -1
- package/dist/locale-data/syr.js +1 -0
- package/dist/locale-data/ta.js +4 -1
- package/dist/locale-data/te.js +1 -1
- package/dist/locale-data/teo.js +2 -1
- package/dist/locale-data/th.js +1 -1
- package/dist/locale-data/ti.js +2 -1
- package/dist/locale-data/tig.js +1 -1
- package/dist/locale-data/tk.js +1 -0
- package/dist/locale-data/tl.js +1 -0
- package/dist/locale-data/tn.js +1 -1
- package/dist/locale-data/to.js +1 -1
- package/dist/locale-data/tr.js +2 -1
- package/dist/locale-data/ts.js +1 -1
- package/dist/locale-data/twq.js +1 -0
- package/dist/locale-data/tzm.js +1 -1
- package/dist/locale-data/ug.js +1 -1
- package/dist/locale-data/uk.js +1 -1
- package/dist/locale-data/ur.js +2 -1
- package/dist/locale-data/uz.js +4 -1
- package/dist/locale-data/vai.js +3 -0
- package/dist/locale-data/ve.js +1 -1
- package/dist/locale-data/vi.js +1 -1
- package/dist/locale-data/vo.js +1 -1
- package/dist/locale-data/vun.js +1 -1
- package/dist/locale-data/wa.js +1 -0
- package/dist/locale-data/wae.js +1 -1
- package/dist/locale-data/wo.js +1 -0
- package/dist/locale-data/xh.js +1 -1
- package/dist/locale-data/xog.js +1 -1
- package/dist/locale-data/yav.js +1 -0
- package/dist/locale-data/yi.js +1 -0
- package/dist/locale-data/yo.js +2 -1
- package/dist/locale-data/zgh.js +1 -0
- package/dist/locale-data/zh.js +8 -1
- package/dist/locale-data/zu.js +1 -1
- package/lib/compiler.js +21 -10
- package/lib/core.js +54 -35
- package/lib/en.js +2 -1
- package/lib/es5.js +8 -0
- package/lib/locales.js +554 -161
- package/lib/main.js +5 -3
- package/lib/utils.js +8 -0
- package/package.json +10 -9
- package/src/compiler.js +13 -10
- package/src/core.js +44 -33
- package/src/en.js +1 -1
- package/bower.json +0 -40
package/README.md
CHANGED
|
@@ -7,8 +7,8 @@ Formats ICU Message strings with number, date, plural, and select placeholders t
|
|
|
7
7
|
[![Build Status][travis-badge]][travis]
|
|
8
8
|
[![Dependency Status][david-badge]][david]
|
|
9
9
|
|
|
10
|
+
[![Sauce Test Status][sauce-badge]][sauce]
|
|
10
11
|
|
|
11
|
-
[](https://saucelabs.com/u/intl-messageformat)
|
|
12
12
|
|
|
13
13
|
Overview
|
|
14
14
|
--------
|
|
@@ -29,7 +29,7 @@ Messages are provided into the constructor as a `String` message, or a [pre-pars
|
|
|
29
29
|
var msg = new IntlMessageFormat(message, locales, [formats]);
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
The string `message` is parsed, then stored internally in a compiled form that is optimized for the `format()` method to produce the formatted string for
|
|
32
|
+
The string `message` is parsed, then stored internally in a compiled form that is optimized for the `format()` method to produce the formatted string for displaying to the user.
|
|
33
33
|
|
|
34
34
|
```js
|
|
35
35
|
var output = msg.format(values);
|
|
@@ -37,7 +37,7 @@ var output = msg.format(values);
|
|
|
37
37
|
|
|
38
38
|
### Common Usage Example
|
|
39
39
|
|
|
40
|
-
A very common example is formatting messages that have numbers with plural
|
|
40
|
+
A very common example is formatting messages that have numbers with plural labels. With this package you can make sure that the string is properly formatted for a person's locale, e.g.:
|
|
41
41
|
|
|
42
42
|
```js
|
|
43
43
|
var MESSAGES = {
|
|
@@ -75,7 +75,7 @@ The message syntax that this package uses is not proprietary, in fact it's a com
|
|
|
75
75
|
|
|
76
76
|
* Uses industry standards: [ICU Message syntax][ICU] and [CLDR locale data][CLDR].
|
|
77
77
|
|
|
78
|
-
* Supports **plural** and **
|
|
78
|
+
* Supports **plural**, **select**, and **selectordinal** message arguments.
|
|
79
79
|
|
|
80
80
|
* Formats numbers and dates/times in messages using [`Intl.NumberFormat`][Intl-NF] and [`Intl.DateTimeFormat`][Intl-DTF], respectively.
|
|
81
81
|
|
|
@@ -124,21 +124,13 @@ _Note: When using the Intl.js Polyfill in Node.js, it will automatically load th
|
|
|
124
124
|
<script src="intl-messageformat/intl-messageformat.min.js"></script>
|
|
125
125
|
```
|
|
126
126
|
|
|
127
|
-
By default, Intl MessageFormat ships with the locale data for English built-in to the runtime
|
|
127
|
+
By default, Intl MessageFormat ships with the locale data for English (`en`) built-in to the library's runtime. When you need to format data in another locale, include its data; e.g., for French:
|
|
128
128
|
|
|
129
129
|
```html
|
|
130
130
|
<script src="intl-messageformat/locale-data/fr.js"></script>
|
|
131
131
|
```
|
|
132
132
|
|
|
133
|
-
_Note: All
|
|
134
|
-
|
|
135
|
-
#### Using Raw Git CDN
|
|
136
|
-
|
|
137
|
-
If you'd like to load this package via a CDN, instead of hosting it yourself, one option is using [Raw Git][rawgit]. **Be sure to reference a `dist/` file using a Git tag:**
|
|
138
|
-
|
|
139
|
-
```html
|
|
140
|
-
<script src="https://cdn.rawgit.com/yahoo/intl-messageformat/v1.0.1/dist/intl-messageformat.min.js"></script>
|
|
141
|
-
```
|
|
133
|
+
_Note: All 200+ languages supported by this package use their root BCP 47 language tag; i.e., the part before the first hyphen (if any)._
|
|
142
134
|
|
|
143
135
|
### Loading Intl MessageFormat in Node.js
|
|
144
136
|
|
|
@@ -148,7 +140,7 @@ Simply `require()` this package:
|
|
|
148
140
|
var IntlMessageFormat = require('intl-messageformat');
|
|
149
141
|
```
|
|
150
142
|
|
|
151
|
-
_Note: in Node.js, the data for all
|
|
143
|
+
_Note: in Node.js, the data for all 200+ languages is loaded along with the library._
|
|
152
144
|
|
|
153
145
|
### Public API
|
|
154
146
|
|
|
@@ -157,7 +149,7 @@ To create a message to format, use the `IntlMessageFormat` constructor. The cons
|
|
|
157
149
|
|
|
158
150
|
- **message** - _{String | AST}_ - String message (or pre-parsed AST) that serves as formatting pattern.
|
|
159
151
|
|
|
160
|
-
- **locales** - _{String | String[]}_ - A string with a BCP 47 language tag, or an array of such strings. If you do not provide a locale, the default locale will be used,
|
|
152
|
+
- **locales** - _{String | String[]}_ - A string with a BCP 47 language tag, or an array of such strings. If you do not provide a locale, the default locale will be used. When an array of locales is provided, each item and its ancestor locales are checked and the first one with registered locale data is returned. **See: [Locale Resolution](#locale-resolution) for more details.**
|
|
161
153
|
|
|
162
154
|
- **[formats]** - _{Object}_ - Optional object with user defined options for format styles.
|
|
163
155
|
|
|
@@ -165,6 +157,38 @@ To create a message to format, use the `IntlMessageFormat` constructor. The cons
|
|
|
165
157
|
var msg = new IntlMessageFormat('My name is {name}.', 'en-US');
|
|
166
158
|
```
|
|
167
159
|
|
|
160
|
+
#### Locale Resolution
|
|
161
|
+
|
|
162
|
+
`IntlMessageFormat` uses a locale resolution process similar to that of the built-in `Intl` APIs to determine which locale data to use based on the `locales` value passed to the constructor. The result of this resolution process can be determined by call the `resolvedOptions()` prototype method.
|
|
163
|
+
|
|
164
|
+
The following are the abstract steps `IntlMessageFormat` goes through to resolve the locale value:
|
|
165
|
+
|
|
166
|
+
* If no extra locale data is loaded, the locale will _always_ resolved to `"en"`.
|
|
167
|
+
|
|
168
|
+
* If locale data is missing for a leaf locale like `"fr-FR"`, but there _is_ data for one of its ancestors, `"fr"` in this case, then its ancestor will be used.
|
|
169
|
+
|
|
170
|
+
* If there's data for the specified locale, then that locale will be resolved; i.e.,
|
|
171
|
+
|
|
172
|
+
```js
|
|
173
|
+
var mf = new IntlMessageFormat('', 'en-US');
|
|
174
|
+
assert(mf.resolvedOptions().locale === 'en-US'); // true
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
* The resolved locales are now normalized; e.g., `"en-us"` will resolve to: `"en-US"`.
|
|
178
|
+
|
|
179
|
+
_Note: When an array is provided for `locales`, the above steps happen for each item in that array until a match is found._
|
|
180
|
+
|
|
181
|
+
#### `resolvedOptions()` Method
|
|
182
|
+
|
|
183
|
+
This method returns an object with the options values that were resolved during instance creation. It currently only contains a `locale` property; here's an example:
|
|
184
|
+
|
|
185
|
+
```js
|
|
186
|
+
var msg = new IntlMessageFormat('', 'en-us');
|
|
187
|
+
console.log(msg.resolvedOptions().locale); // => "en-US"
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Notice how the specified locale was the all lower-case value: `"en-us"`, but it was resolved and normalized to: `"en-US"`.
|
|
191
|
+
|
|
168
192
|
#### `format(values)` Method
|
|
169
193
|
|
|
170
194
|
Once the message is created, formatting the message is done by calling the `format()` method on the instance and passing a collection of `values`:
|
|
@@ -174,7 +198,7 @@ var output = msg.format({name: "Eric"});
|
|
|
174
198
|
console.log(output); // => "My name is Eric."
|
|
175
199
|
```
|
|
176
200
|
|
|
177
|
-
|
|
201
|
+
_Note: A value **must** be supplied for every argument in the message pattern the instance was constructed with._
|
|
178
202
|
|
|
179
203
|
#### User Defined Formats
|
|
180
204
|
|
|
@@ -225,7 +249,7 @@ console.log(msg.format({numPhotos: 1})); // => "You have one photo."
|
|
|
225
249
|
console.log(msg.format({numPhotos: 1000})); // => "You have 1,000 photos."
|
|
226
250
|
```
|
|
227
251
|
|
|
228
|
-
_Note how when `numPhotos` was `1000`, the number is formatted with the correct thousands separator._
|
|
252
|
+
_Note: how when `numPhotos` was `1000`, the number is formatted with the correct thousands separator._
|
|
229
253
|
|
|
230
254
|
|
|
231
255
|
License
|
|
@@ -240,7 +264,9 @@ See the [LICENSE file][LICENSE] for license text and copyright information.
|
|
|
240
264
|
[david]: https://david-dm.org/yahoo/intl-messageformat
|
|
241
265
|
[david-badge]: https://img.shields.io/david/yahoo/intl-messageformat.svg?style=flat-square
|
|
242
266
|
[travis]: https://travis-ci.org/yahoo/intl-messageformat
|
|
243
|
-
[travis-badge]: https://img.shields.io/travis/yahoo/intl-messageformat.svg?style=flat-square
|
|
267
|
+
[travis-badge]: https://img.shields.io/travis/yahoo/intl-messageformat/master.svg?style=flat-square
|
|
268
|
+
[sauce]: https://saucelabs.com/u/intl-messageformat
|
|
269
|
+
[sauce-badge]: https://saucelabs.com/browser-matrix/intl-messageformat.svg
|
|
244
270
|
[strawman]: http://wiki.ecmascript.org/doku.php?id=globalization:messageformatting
|
|
245
271
|
[parser]: https://github.com/yahoo/intl-messageformat-parser
|
|
246
272
|
[ICU]: http://userguide.icu-project.org/formatparse/messages
|