Skip to content

Commit

Permalink
Use a Function, not a NumberFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhooper authored and ljharb committed Oct 24, 2016
1 parent d4c7ecc commit bf3acdc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,15 @@ polyglot.t("car", 2);
=> "2 cars"
```

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

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

If you like, you can provide a default value in case the phrase is missing.
Use the special option key "_" to specify a default.
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function transformPhrase(phrase, substitutions, locale, numberFormat) {

var replacement = options[argument];
if (typeof replacement === 'number') {
replacement = numberFormat.format(replacement);
replacement = numberFormat(replacement);
}

// Ensure replacement value is escaped to prevent special $-prefixed regex replace tokens.
Expand All @@ -163,7 +163,7 @@ function Polyglot(options) {
this.phrases = {};
this.extend(opts.phrases || {});
this.currentLocale = opts.locale || 'en';
this.numberFormat = opts.numberFormat || { format: String };
this.numberFormat = opts.numberFormat || String;
this.allowMissing = !!opts.allowMissing;
this.warn = opts.warn || warn;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"pretest": "npm run --silent lint",
"test": "npm run --silent tests-only",
"tests-only": "NODE_ICU_DATA=node_modules/full-icu mocha test/*.js --reporter spec",
"tests-only": "mocha test/*.js --reporter spec",
"lint": "eslint *.js test/*.js",
"docs": "docco -o docs/ index.js"
},
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ describe('t', function () {
expect(instance.t('header.sign_in')).to.equal('Sign In');
});

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

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

0 comments on commit bf3acdc

Please sign in to comment.