Comparing version 1.0.0 to 1.0.1
13
index.js
"use strict"; | ||
function defaultFallback(prefix, key, namedValues, args) { | ||
function defaultFallback(key) { | ||
if (!key) { | ||
@@ -20,2 +20,6 @@ return "(?)"; | ||
function getKey(prefix, key) { | ||
return (prefix !== null && prefix !== undefined) ? ((key !== null && key !== undefined) ? prefix + key : prefix) : key; | ||
} | ||
function defaultTranslation(prefix, keys, fallbackKey, namedValues, args) { | ||
@@ -25,8 +29,7 @@ var result, | ||
while (!result && keyNo < keys.length) { | ||
result = this.lookup.get(prefix + keys[keyNo]); | ||
result = this.lookup.get(getKey(prefix, keys[keyNo])); | ||
keyNo += 1; | ||
} | ||
if (result === null | ||
|| result === undefined) { | ||
result = this.fallback(prefix, fallbackKey, namedValues, args); | ||
if (result === null || result === undefined) { | ||
result = this.fallback(getKey(prefix, fallbackKey)); | ||
} | ||
@@ -33,0 +36,0 @@ if (namedValues && (/{{.*}}/).test(result)) { |
{ | ||
"name": "i18n-core", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Basic i18n translation. ", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# i18n-core | ||
[![Build Status](https://travis-ci.org/martinheidegger/i18n-core.svg)](https://travis-ci.org/martinheidegger/i18n-core) | ||
[![Code Climate](https://codeclimate.com/github/martinheidegger/i18n-core/badges/gpa.svg)](https://codeclimate.com/github/martinheidegger/i18n-core) | ||
[i18n-core](https://github.io/martinheidegger/i18n-core) is a no-fuzz Node.js implementation of i18n. It doesn't connect to express or any other fancy Node framework and is extensible where it needs to be and allows to reduce the complexity of other i18n implementations (thus the name). | ||
@@ -79,2 +82,2 @@ | ||
If you have any questions, please post them as issue, thanks! | ||
If you have any questions, please post them as issue, thanks! |
@@ -19,3 +19,3 @@ "use strict"; | ||
test("missing file lookup", function (done) { | ||
expect(i18n(__dirname + "/lookup").lang("de").__("b")).to.equal("b"); | ||
expect(i18n(__dirname + "/lookup").lang("de").__("b")).to.equal("de.b"); | ||
done(); | ||
@@ -63,3 +63,3 @@ }); | ||
}; | ||
expect(translator.__("a")).to.equal("a"); | ||
expect(translator.__("a")).to.equal("en.a"); | ||
done(); | ||
@@ -81,7 +81,7 @@ }); | ||
__n = translator.__n; | ||
expect(__n("%s a", "", 1)).to.equal("1 a"); | ||
expect(__n("", "%s b", 2)).to.equal("2 b"); | ||
expect(__n("%s a {{count}}", "", 1)).to.equal("1 a 1"); | ||
expect(__n("", "%s b {{count}}", 2)).to.equal("2 b 2"); | ||
expect(__n("", "{{count}} c", 3)).to.equal("3 c"); | ||
expect(__n("%s a", "", 1)).to.equal("en.1 a"); | ||
expect(__n("", "%s b", 2)).to.equal("en.2 b"); | ||
expect(__n("%s a {{count}}", "", 1)).to.equal("en.1 a 1"); | ||
expect(__n("", "%s b {{count}}", 2)).to.equal("en.2 b 2"); | ||
expect(__n("", "{{count}} c", 3)).to.equal("en.3 c"); | ||
done(); | ||
@@ -93,7 +93,7 @@ }); | ||
__n = translator.__n; | ||
expect(__n("%s a %s", "", 1, "x")).to.equal("1 a x"); | ||
expect(__n("", "%s b %s", 2, "y")).to.equal("2 b y"); | ||
expect(__n("%s c %s {{a}}", "", 1, {a: "b"}, "x")).to.equal("1 c x b"); | ||
expect(__n("", "%s d %s {{c}}", 2, {c: "d"}, "y")).to.equal("2 d y d"); | ||
expect(__n("", "%s e {{e}}", 2, {e: "f"})).to.equal("2 e f"); | ||
expect(__n("%s a %s", "", 1, "x")).to.equal("en.1 a x"); | ||
expect(__n("", "%s b %s", 2, "y")).to.equal("en.2 b y"); | ||
expect(__n("%s c %s {{a}}", "", 1, {a: "b"}, "x")).to.equal("en.1 c x b"); | ||
expect(__n("", "%s d %s {{c}}", 2, {c: "d"}, "y")).to.equal("en.2 d y d"); | ||
expect(__n("", "%s e {{e}}", 2, {e: "f"})).to.equal("en.2 e f"); | ||
done(); | ||
@@ -105,9 +105,10 @@ }); | ||
__n = translator.__n; | ||
expect(__n({one: "a", other: "%s"}, 2, "x")).to.equal("2"); | ||
expect(__n({1: "b %s"}, 1, "x")).to.equal("b 1"); | ||
expect(__n({2: "c %s"}, 2, "x")).to.equal("c 2"); | ||
expect(__n({}, {2: "d %s"}, 2, "x")).to.equal("d 2"); | ||
expect(__n({}, {other: "e %s"}, 2, "x")).to.equal("e 2"); | ||
expect(__n({one: "a", other: "%s"}, 2, "x")).to.equal("en.2"); | ||
expect(__n({1: "b %s"}, 1, "x")).to.equal("en.b 1"); | ||
expect(__n({2: "c %s"}, 2, "x")).to.equal("en.c 2"); | ||
expect(__n({}, {2: "d %s"}, 2, "x")).to.equal("en.d 2"); | ||
expect(__n({}, {other: "e %s"}, 2, "x")).to.equal("en.e 2"); | ||
done(); | ||
}); | ||
test("undefined fallback", function (done) { | ||
@@ -122,12 +123,37 @@ var translator = i18n({a: null, b: undefined}); | ||
test("empty key", function (done) { | ||
var translator = i18n("./lookup"); | ||
expect(translator.__("")).to.equal("(?)"); | ||
done(); | ||
}); | ||
test("empty key in namespace", function (done) { | ||
var translator = i18n("./lookup").lang("en"); | ||
expect(translator.__("")).to.equal("en."); | ||
done(); | ||
}); | ||
test("undefined key in namespace", function (done) { | ||
var translator = i18n("./lookup").lang("en"); | ||
expect(translator.__(undefined)).to.equal("en."); | ||
done(); | ||
}); | ||
test("null namespace with key", function (done) { | ||
var translator = i18n("./lookup").sub(null); | ||
expect(translator.__("ho")).to.equal("ho"); | ||
done(); | ||
}); | ||
test("plural fallbacks", function (done) { | ||
var translator = i18n().lang("en"), | ||
__n = translator.__n; | ||
expect(__n("a %s", 2, "x")).to.equal("a 2"); | ||
expect(__n({other: "b %s"}, 2, "x")).to.equal("b 2"); | ||
expect(__n({one: "c %s"}, 2, "x")).to.equal("c 2"); | ||
expect(__n({other: "d %s"}, null, 2, "x")).to.equal("d 2"); | ||
expect(__n({2: "e %s"}, null, 2, "x")).to.equal("e 2"); | ||
expect(__n({one: "f %s"}, null, 2, "x")).to.equal("f 2"); | ||
expect(__n("g %s", null, 2, "x")).to.equal("g 2"); | ||
expect(__n("a %s", 2, "x")).to.equal("en.a 2"); | ||
expect(__n({other: "b %s"}, 2, "x")).to.equal("en.b 2"); | ||
expect(__n({one: "c %s"}, 2, "x")).to.equal("en.c 2"); | ||
expect(__n({other: "d %s"}, null, 2, "x")).to.equal("en.d 2"); | ||
expect(__n({2: "e %s"}, null, 2, "x")).to.equal("en.e 2"); | ||
expect(__n({one: "f %s"}, null, 2, "x")).to.equal("en.f 2"); | ||
expect(__n("g %s", null, 2, "x")).to.equal("en.g 2"); | ||
done(); | ||
@@ -134,0 +160,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15379
14
425
83