Skip to content

Commit e54353b

Browse files
adamhooperljharb
authored andcommitted
Use a Function, not a NumberFormat
1 parent 71b562a commit e54353b

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,15 @@ polyglot.t("car", 2);
243243
=> "2 cars"
244244
```
245245

246-
If you pass a `numberFormat` to the constructor, interpolated `Number`s will
247-
be formatted by its `format()` method. That's useful because different locales
248-
have different rules for formatting numbers: `2,000.56` in English versus
249-
`1 234,56` in French, for instance.
246+
If you pass a `numberFormat` _function_ to the constructor, Polyglot will use
247+
it to translate interpolated `Number`s to `String`s. That's useful because
248+
different locales have different rules for formatting numbers: `2,000.56` in
249+
English versus `1 234,56` in French, for instance.
250250

251251
```js
252252
polyglot = new Polyglot({
253253
phrases: { num_cars: '%{smart_count} car |||| %{smart_count} cars' },
254-
numberFormat: new Intl.NumberFormat('en') // Chrome, Firefox, IE11+, Node 0.12+ with ICU
254+
numberFormat: new Intl.NumberFormat('en').format // Chrome, Firefox, IE11+, Node 0.12+ with ICU
255255
})
256256
polyglot.t("num_cars", 2000); // internally, calls options.numberFormat.format(2000)
257257
=> "2,000 cars"
@@ -261,7 +261,9 @@ polyglot.t("num_cars", 2000); // internally, calls options.numberFormat.format(2
261261
in Node: Node 0.12+ comes with Intl as long as it's compiled with ICU (which is
262262
the default). By default, the only locale Node supports is en-US. You can add
263263
[full-icu](https://www.npmjs.com/package/full-icu) to your project to support
264-
other locales.
264+
other locales. Finally, Polyglot accepts a _function_, not an Intl.NumberFormat
265+
instance: if you have a NumberFormat instance, pass its `.format` property to
266+
Polyglot.
265267

266268
If you like, you can provide a default value in case the phrase is missing.
267269
Use the special option key "_" to specify a default.

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function transformPhrase(phrase, substitutions, locale, numberFormat) {
147147

148148
var replacement = options[argument];
149149
if (typeof replacement === 'number') {
150-
replacement = numberFormat.format(replacement);
150+
replacement = numberFormat(replacement);
151151
}
152152

153153
// Ensure replacement value is escaped to prevent special $-prefixed regex replace tokens.
@@ -163,7 +163,7 @@ function Polyglot(options) {
163163
this.phrases = {};
164164
this.extend(opts.phrases || {});
165165
this.currentLocale = opts.locale || 'en';
166-
this.numberFormat = opts.numberFormat || { format: String };
166+
this.numberFormat = opts.numberFormat || String;
167167
this.allowMissing = !!opts.allowMissing;
168168
this.warn = opts.warn || warn;
169169
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"pretest": "npm run --silent lint",
88
"test": "npm run --silent tests-only",
9-
"tests-only": "NODE_ICU_DATA=node_modules/full-icu mocha test/*.js --reporter spec",
9+
"tests-only": "mocha test/*.js --reporter spec",
1010
"lint": "eslint *.js test/*.js",
1111
"docs": "docco -o docs/ index.js"
1212
},

test/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ describe('t', function () {
9191
expect(instance.t('header.sign_in')).to.equal('Sign In');
9292
});
9393

94-
it('uses an Intl.NumberFormat', function () {
94+
it('uses numberFormat', function () {
9595
var instance = new Polyglot({
9696
phrases: phrases,
9797
// prove we're passed a Number by doing math on it and formatting it
98-
numberFormat: { format: function (n) { return 'x' + (n + 2); } }
98+
numberFormat: function (n) { return 'x' + (n + 2); }
9999
});
100100

101101
expect(instance.t('number', { number: 1234.56 })).to.equal('x1236.56');

0 commit comments

Comments
 (0)