Skip to content

Commit

Permalink
#305 - adding support for floats in __n()
Browse files Browse the repository at this point in the history
  • Loading branch information
mashpie committed Apr 16, 2020
1 parent 902bafa commit a0aaf3e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
12 changes: 5 additions & 7 deletions i18n.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* @author Created by Marcus Spiegel <marcus.spiegel@gmail.com> on 2011-03-25.
* @author Created by Marcus Spiegel <spiegel@uscreen.de> on 2011-03-25.
* @link https://github.com/mashpie/i18n-node
* @license http://opensource.org/licenses/MIT
*
* @version 0.8.3
*/

'use strict';
Expand Down Expand Up @@ -326,7 +324,7 @@ module.exports = (function() {
args.unshift(count);

// some template engines pass all values as strings -> so we try to convert them to numbers
if (typeof plural === 'number' || parseInt(plural, 10) + '' === plural) {
if (typeof plural === 'number' || Number(plural) + '' === plural) {
count = plural;
}

Expand All @@ -337,7 +335,7 @@ module.exports = (function() {
}
} else {
// called like __n('cat', 3)
if (typeof plural === 'number' || parseInt(plural, 10) + '' === plural) {
if (typeof plural === 'number' || Number(plural) + '' === plural) {
count = plural;

// we add same string as default
Expand All @@ -357,7 +355,7 @@ module.exports = (function() {
if (count === null) count = namedValues.count;

// enforce number
count = parseInt(count, 10);
count = Number(count);

// find the correct plural rule for given locale
if (typeof msg === 'object') {
Expand Down Expand Up @@ -537,7 +535,7 @@ module.exports = (function() {

// replace the counter
if (typeof count === 'number') {
msg = vsprintf(msg, [parseInt(count, 10)]);
msg = vsprintf(msg, [Number(count)]);
}

// if the msg string contains {{Mustache}} patterns we render it as a mini tempalate
Expand Down
12 changes: 12 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
"one": "%s cat",
"other": "%s cats"
},
"%f star": {
"one": "%f star",
"other": "%f stars"
},
"%d star": {
"one": "%d star",
"other": "%d stars"
},
"%s star": {
"one": "%s star",
"other": "%s stars"
},
"cat": {
"one": "%s cat",
"other": "%s cats"
Expand Down
18 changes: 18 additions & 0 deletions test/i18n.plurals.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ describe('parsing plural intervals from strings', function() {
"a zero rule"
);
});

it('should handle floats (#305)', function() {
should.equal(pluralTest.__n('%f star', -1.5), "-1.5 stars");
should.equal(pluralTest.__n('%f star', -1), "-1 stars");
should.equal(pluralTest.__n('%f star', 0), "0 stars");
should.equal(pluralTest.__n('%f star', 0.5), "0.5 stars");
should.equal(pluralTest.__n('%f star', 1), "1 star");
should.equal(pluralTest.__n('%f star', 2), "2 stars");
should.equal(pluralTest.__n('%f star', 2.5), "2.5 stars");
});

it('should handle floats even when passed as strings (#305)', function() {
should.equal(pluralTest.__n('%f star', '-1.5'), "-1.5 stars");
should.equal(pluralTest.__n('%f star', '5'), "5 stars");
should.equal(pluralTest.__n('%f star', '5.5'), "5.5 stars");
should.equal(pluralTest.__n('%s star', '5.5'), "5.5 stars");
should.equal(pluralTest.__n('%d star', '5.5'), "5 stars");
});

it('plurals with intervals in string (no object)', function() {
var p = 'plurals with intervals in string (no object)';
Expand Down

0 comments on commit a0aaf3e

Please sign in to comment.