From bf3acdcd9c827e46ae9a347bded6cfe553871f39 Mon Sep 17 00:00:00 2001 From: Adam Hooper Date: Mon, 24 Oct 2016 16:59:24 -0400 Subject: [PATCH] Use a Function, not a NumberFormat --- README.md | 14 ++++++++------ index.js | 4 ++-- package.json | 2 +- test/index.js | 4 ++-- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 6b4134c..51c8904 100644 --- a/README.md +++ b/README.md @@ -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" @@ -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. diff --git a/index.js b/index.js index 61702cf..576eee9 100644 --- a/index.js +++ b/index.js @@ -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. @@ -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; } diff --git a/package.json b/package.json index 1658979..aef5963 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test/index.js b/test/index.js index 151b66c..ca9bf96 100644 --- a/test/index.js +++ b/test/index.js @@ -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');