Comparing version 1.3.3 to 2.0.0
55
index.js
@@ -11,3 +11,3 @@ "use strict"; | ||
function getLookup(data) { | ||
if (data && data.get) { | ||
if (data && typeof data.get === "function") { | ||
// Direct lookup implementation pass-through | ||
@@ -21,37 +21,43 @@ return data; | ||
function getKey(prefix, key) { | ||
return (key !== null && key !== undefined) ? prefix + key : prefix; | ||
function _defaultTranslation(that, value, fallbackKey, namedValues, args) { | ||
if (value === null || value === undefined) { | ||
value = that.fallback(fallbackKey); | ||
} | ||
if (namedValues && (/{{.*}}/).test(value)) { | ||
value = that.mustache.render(value, namedValues); | ||
} | ||
if (args !== null && args !== undefined && args.length > 0 && /%/.test(value)) { | ||
return that.vsprintf(value, args); | ||
} | ||
return value; | ||
} | ||
function defaultTranslation(prefix, keys, fallbackKey, namedValues, args) { | ||
var result, | ||
function defaultTranslation(key, namedValues, args) { | ||
return _defaultTranslation(this, this.raw(key), key, namedValues, args); | ||
} | ||
function defaultTranslationFirst(keys, fallbackKey, namedValues, args) { | ||
var value = null, | ||
keyNo = 0; | ||
while (!result && keyNo < keys.length) { | ||
result = this.raw(prefix, keys[keyNo]); | ||
while ((value === undefined || value === null) && keyNo < keys.length) { | ||
var key = keys[keyNo]; | ||
value = this.raw(key); | ||
keyNo += 1; | ||
} | ||
if (result === null || result === undefined) { | ||
result = this.fallback(getKey(prefix, fallbackKey)); | ||
} | ||
if (namedValues && (/{{.*}}/).test(result)) { | ||
result = this.mustache.render(result, namedValues); | ||
} | ||
if (args.length > 0 && /%/.test(result)) { | ||
return this.vsprintf(result, args); | ||
} | ||
return result; | ||
return _defaultTranslation(this, value, fallbackKey, namedValues, args); | ||
} | ||
function has(prefix, key) { | ||
var val = this.raw(prefix, key); | ||
function has(key) { | ||
var val = this.raw(key); | ||
return val !== undefined && val !== null; | ||
} | ||
function raw(prefix, key) { | ||
return this.lookup.get(getKey(prefix, key)); | ||
function raw(key) { | ||
return this.lookup.get(key); | ||
} | ||
module.exports = function (data, allowModification) { | ||
var translator = require("./lib/createTranslator")("", null, allowModification); | ||
translator.lookup = getLookup(data); | ||
var translator = require("./lib/createTranslator")("", null, allowModification), | ||
lookup = getLookup(data); | ||
translator.lookup = lookup; | ||
translator.fallback = defaultFallback; | ||
@@ -62,4 +68,5 @@ translator.has = has; | ||
translator.vsprintf = require("sprintf").vsprintf; | ||
translator.translate = defaultTranslation; | ||
translator.translate = defaultTranslation.bind(translator); | ||
translator.translateFirst = defaultTranslationFirst.bind(translator) | ||
return translator; | ||
} |
"use strict"; | ||
var slice = Array.prototype.slice; | ||
function getStart(namedValues) { | ||
@@ -8,23 +10,17 @@ return (typeof namedValues === "object" && namedValues !== null) ? 2 : 1; | ||
function getArgs(startOfArgs, args) { | ||
return (args.length > startOfArgs) ? Array.prototype.slice.call(args, startOfArgs) : []; | ||
return (args.length > startOfArgs) ? slice.call(args, startOfArgs) : null; | ||
} | ||
module.exports = function createTranslator(prefix, parent, allowModification) { | ||
var ns; | ||
if (parent) { | ||
ns = function () { | ||
return parent.ns() + prefix; | ||
}; | ||
} else { | ||
ns = function () { | ||
return prefix; | ||
}; | ||
function addPrefix(key) { | ||
return key !== null && key !== undefined ? prefix + key : prefix; | ||
} | ||
function has(key) { | ||
return parent.has(prefix, key); | ||
return parent.has(addPrefix(key)); | ||
} | ||
function raw(key) { | ||
return parent.raw(prefix, key); | ||
return parent.raw(addPrefix(key)); | ||
} | ||
@@ -35,3 +31,3 @@ | ||
args = getArgs(startOfArgs, arguments); | ||
return this.translate(ns(), [key], key, startOfArgs == 2 ? namedValues : null, args); | ||
return this.translate(key, (startOfArgs == 2 ? namedValues : null), args); | ||
} | ||
@@ -52,2 +48,3 @@ | ||
} | ||
if (typeof plural === "object") { | ||
@@ -70,3 +67,3 @@ if (plural !== null) { | ||
} | ||
args = getArgs(startOfArgs, arguments); | ||
args = getArgs(startOfArgs, arguments) || []; | ||
if (!namedValues || startOfArgs === 3) { | ||
@@ -78,12 +75,20 @@ namedValues = {}; | ||
if (count > 1) { | ||
keys = [singular + "." + count, singular + ".other", singular, singular + ".one"]; | ||
keys = [ | ||
singular + "." + count, | ||
singular + ".other", | ||
singular, | ||
singular + ".one" | ||
]; | ||
if (plural) { | ||
keys.unshift(plural) | ||
} | ||
fallbackKey = plural || singular; | ||
fallbackKey = (plural || singular); | ||
} else { | ||
keys = [singular + ".one", singular]; | ||
keys = [ | ||
singular + ".one", | ||
singular | ||
]; | ||
fallbackKey = singular; | ||
} | ||
return this.translate(ns(), keys, fallbackKey, namedValues, args); | ||
return this.translateFirst(keys, fallbackKey, namedValues, args); | ||
} | ||
@@ -95,6 +100,10 @@ | ||
function translate(prefix, keys, fallbackKey, namedValues, args) { | ||
return parent.translate(prefix, keys, fallbackKey, namedValues, args); | ||
function translate(key, fallbackKey, namedValues, args) { | ||
return parent.translate(addPrefix(key), fallbackKey, namedValues, args); | ||
} | ||
function translateFirst(keys, fallbackKey, namedValues, args) { | ||
return parent.translateFirst(keys.map(addPrefix), addPrefix(fallbackKey), namedValues, args); | ||
} | ||
function sub(prefix, allowSubModification) { | ||
@@ -116,5 +125,5 @@ if (prefix === null || prefix === undefined) { | ||
result.translate = translate.bind(result); | ||
result.translateFirst = translateFirst.bind(result); | ||
result.lang = lang.bind(result); | ||
result.sub = sub.bind(result); | ||
result.ns = ns; | ||
result.has = has.bind(result); | ||
@@ -121,0 +130,0 @@ result.raw = raw.bind(result); |
{ | ||
"name": "i18n-core", | ||
"version": "1.3.3", | ||
"version": "2.0.0", | ||
"description": "Basic i18n translation.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -73,2 +73,13 @@ # i18n-core | ||
In case you need to have several strategies that need to be chained you can use | ||
the chain lookup: | ||
```JavaScript | ||
var i18nChain = require("i18n-core/lookup/chain") | ||
var i18nObject = require("i18n-core/lookup/object") | ||
var i18n = i18n_core(i18nChain(i18nObject({a: "x"}), i18nObject({a: 0, b: 1}))) | ||
i18n.__('a') // x | ||
i18n.__('b') // 1 | ||
``` | ||
*i18n-core* does implement basic placeholder replacements like: | ||
@@ -75,0 +86,0 @@ |
@@ -268,3 +268,3 @@ "use strict"; | ||
test("mustach strings to be treated as strings", function (done) { | ||
test("mustache strings to be treated as strings", function (done) { | ||
var __ = i18n({"$": "{{data}}"}).__; | ||
@@ -288,2 +288,20 @@ expect(__('$', {data: 1})).to.equal("1"); | ||
done(); | ||
}); | ||
}); | ||
test("sprintf should be ignored when the given array has a length = 0", function (done) { | ||
expect(i18n().translate("a %2", {}, [])).to.equal("a %2"); | ||
done(); | ||
}); | ||
test("An undefined sub should work just fine", function (done) { | ||
expect(i18n({en: "a"}).sub("en").__(undefined)).to.equal("a"); | ||
expect(i18n({en: "a"}).sub("en").__(null)).to.equal("a"); | ||
done(); | ||
}); | ||
test("multiple keys with one being an empty string", function (done) { | ||
var translate = i18n({"a": "", "b": "ho"}).translate; | ||
debugger; | ||
expect(translate("a")).to.equal(""); | ||
done(); | ||
}); |
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
24142
16
643
135