Skip to content

Commit 71b562a

Browse files
adamhooperljharb
authored andcommitted
Use dependency injection instead of Intl
1 parent 8570d35 commit 71b562a

File tree

4 files changed

+21
-36
lines changed

4 files changed

+21
-36
lines changed

README.md

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

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

248251
```js
249-
polyglot.t("num_cars", 2000);
252+
polyglot = new Polyglot({
253+
phrases: { num_cars: '%{smart_count} car |||| %{smart_count} cars' },
254+
numberFormat: new Intl.NumberFormat('en') // Chrome, Firefox, IE11+, Node 0.12+ with ICU
255+
})
256+
polyglot.t("num_cars", 2000); // internally, calls options.numberFormat.format(2000)
250257
=> "2,000 cars"
251258
```
252259

253-
On a default Node install, this may only work in English. To format in
254-
non-English locales (e.g., to output "2.000" in France or use other numerals),
255-
compile Node with "full" ICU data or include the `full-icu` package in your
256-
project:
257-
258-
1. `npm install --save full-icu`
259-
2. Run `node --full-data-dir=node_modules/full-icu` instead of just `node`, or
260-
set the `NODE_ICU_DATA=node_modules/full-icu` environment variable.
261-
262-
If you're running Polyglot within a browser, it can number-format in any
263-
locale the web browser supports.
260+
(A primer on [Intl.NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat)
261+
in Node: Node 0.12+ comes with Intl as long as it's compiled with ICU (which is
262+
the default). By default, the only locale Node supports is en-US. You can add
263+
[full-icu](https://www.npmjs.com/package/full-icu) to your project to support
264+
other locales.
264265

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

index.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,7 @@ function Polyglot(options) {
163163
this.phrases = {};
164164
this.extend(opts.phrases || {});
165165
this.currentLocale = opts.locale || 'en';
166-
if (typeof Intl === 'object') {
167-
this.numberFormat = new Intl.NumberFormat(this.currentLocale);
168-
} else {
169-
// Fallback for IE<11
170-
this.numberFormat = { format: function (n) { return String(n); } };
171-
}
166+
this.numberFormat = opts.numberFormat || { format: String };
172167
this.allowMissing = !!opts.allowMissing;
173168
this.warn = opts.warn || warn;
174169
}
@@ -179,12 +174,6 @@ function Polyglot(options) {
179174
Polyglot.prototype.locale = function (newLocale) {
180175
if (newLocale) {
181176
this.currentLocale = newLocale;
182-
if (typeof Intl === 'object') {
183-
this.numberFormat = new Intl.NumberFormat(this.currentLocale);
184-
} else {
185-
// Fallback for IE<11
186-
this.numberFormat = { format: function (n) { return String(n); } };
187-
}
188177
}
189178
return this.currentLocale;
190179
};

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"eslint": "^3.9.1",
3737
"eslint-config-airbnb-base": "^10.0.1",
3838
"eslint-plugin-import": "^2.2.0",
39-
"full-icu": "^1.0.3",
4039
"mocha": "^3.1.2",
4140
"should": "^11.1.1",
4241
"uglify-js": "2.7.3"

test/index.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,13 @@ describe('t', function () {
9292
});
9393

9494
it('uses an Intl.NumberFormat', function () {
95-
var en = new Polyglot({ phrases: phrases, locale: 'en' });
96-
var fr = new Polyglot({ phrases: phrases, locale: 'fr' });
95+
var instance = new Polyglot({
96+
phrases: phrases,
97+
// prove we're passed a Number by doing math on it and formatting it
98+
numberFormat: { format: function (n) { return 'x' + (n + 2); } }
99+
});
97100

98-
expect(en.t('number', { number: 1234.56 })).to.equal('1,234.56');
99-
expect(fr.t('number', { number: 1234.56 })).to.equal('1 234,56');
101+
expect(instance.t('number', { number: 1234.56 })).to.equal('x1236.56');
100102
});
101103
});
102104

@@ -174,12 +176,6 @@ describe('locale', function () {
174176
polyglot.locale('fr');
175177
expect(polyglot.locale()).to.equal('fr');
176178
});
177-
178-
it('updates number format when setting locale', function () {
179-
polyglot.locale('fr');
180-
polyglot.extend({ x: '%{n}' });
181-
expect(polyglot.t('x', { n: 1234.56 })).to.equal('1 234,56');
182-
});
183179
});
184180

185181
describe('extend', function () {

0 commit comments

Comments
 (0)