Skip to content

Commit

Permalink
Merge branch 'release/0.9.0' into npm
Browse files Browse the repository at this point in the history
  • Loading branch information
mashpie committed Apr 17, 2020
2 parents 713c63e + d624d00 commit ca9e435
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
16 changes: 7 additions & 9 deletions i18n.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/**
* @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';

// dependencies
var vsprintf = require('sprintf-js').vsprintf,
pkgVersion = require('./package.json').version,
fs = require('fs'),
url = require('url'),
path = require('path'),
Expand All @@ -21,7 +20,6 @@ var vsprintf = require('sprintf-js').vsprintf,
MakePlural = require('make-plural'),
parseInterval = require('math-interval-parser').default;


// exports an instance
module.exports = (function() {

Expand Down Expand Up @@ -64,7 +62,7 @@ module.exports = (function() {
// public exports
var i18n = {};

i18n.version = '0.8.3';
i18n.version = pkgVersion;

i18n.configure = function i18nConfigure(opt) {

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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "i18n",
"description": "lightweight translation module with dynamic json storage",
"version": "0.8.6",
"version": "0.9.0",
"homepage": "http://github.com/mashpie/i18n-node",
"repository": {
"type": "git",
Expand Down
28 changes: 28 additions & 0 deletions test/i18n.plurals.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ 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");
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('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', '-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");
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
3 changes: 2 additions & 1 deletion test/i18n.setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var i18n = require('../i18n'),
pkgVersion = require('../package.json').version,
should = require("should");

i18n.configure({
Expand All @@ -10,7 +11,7 @@ i18n.configure({

describe('Module Setup', function () {
it('should export a valid version', function () {
should.equal(i18n.version, '0.8.3');
should.equal(i18n.version, pkgVersion);
});

it('should export configure as i18nConfigure', function () {
Expand Down

0 comments on commit ca9e435

Please sign in to comment.