Skip to content

Commit

Permalink
Use dependency injection instead of Intl
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhooper authored and ljharb committed Oct 23, 2016
1 parent 8570d35 commit 71b562a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 36 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,24 +243,25 @@ polyglot.t("car", 2);
=> "2 cars"
```

Interpolated `Number`s will be number-formatted according to the `locale`:
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.

```js
polyglot.t("num_cars", 2000);
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
})
polyglot.t("num_cars", 2000); // internally, calls options.numberFormat.format(2000)
=> "2,000 cars"
```

On a default Node install, this may only work in English. To format in
non-English locales (e.g., to output "2.000" in France or use other numerals),
compile Node with "full" ICU data or include the `full-icu` package in your
project:

1. `npm install --save full-icu`
2. Run `node --full-data-dir=node_modules/full-icu` instead of just `node`, or
set the `NODE_ICU_DATA=node_modules/full-icu` environment variable.

If you're running Polyglot within a browser, it can number-format in any
locale the web browser supports.
(A primer on [Intl.NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat)
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.

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
13 changes: 1 addition & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,7 @@ function Polyglot(options) {
this.phrases = {};
this.extend(opts.phrases || {});
this.currentLocale = opts.locale || 'en';
if (typeof Intl === 'object') {
this.numberFormat = new Intl.NumberFormat(this.currentLocale);
} else {
// Fallback for IE<11
this.numberFormat = { format: function (n) { return String(n); } };
}
this.numberFormat = opts.numberFormat || { format: String };
this.allowMissing = !!opts.allowMissing;
this.warn = opts.warn || warn;
}
Expand All @@ -179,12 +174,6 @@ function Polyglot(options) {
Polyglot.prototype.locale = function (newLocale) {
if (newLocale) {
this.currentLocale = newLocale;
if (typeof Intl === 'object') {
this.numberFormat = new Intl.NumberFormat(this.currentLocale);
} else {
// Fallback for IE<11
this.numberFormat = { format: function (n) { return String(n); } };
}
}
return this.currentLocale;
};
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"eslint": "^3.9.1",
"eslint-config-airbnb-base": "^10.0.1",
"eslint-plugin-import": "^2.2.0",
"full-icu": "^1.0.3",
"mocha": "^3.1.2",
"should": "^11.1.1",
"uglify-js": "2.7.3"
Expand Down
16 changes: 6 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ describe('t', function () {
});

it('uses an Intl.NumberFormat', function () {
var en = new Polyglot({ phrases: phrases, locale: 'en' });
var fr = new Polyglot({ phrases: phrases, locale: 'fr' });
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); } }
});

expect(en.t('number', { number: 1234.56 })).to.equal('1,234.56');
expect(fr.t('number', { number: 1234.56 })).to.equal('1 234,56');
expect(instance.t('number', { number: 1234.56 })).to.equal('x1236.56');
});
});

Expand Down Expand Up @@ -174,12 +176,6 @@ describe('locale', function () {
polyglot.locale('fr');
expect(polyglot.locale()).to.equal('fr');
});

it('updates number format when setting locale', function () {
polyglot.locale('fr');
polyglot.extend({ x: '%{n}' });
expect(polyglot.t('x', { n: 1234.56 })).to.equal('1 234,56');
});
});

describe('extend', function () {
Expand Down

0 comments on commit 71b562a

Please sign in to comment.