vue3-gettext
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -63,56 +63,58 @@ 'use strict'; | ||
*/ | ||
var interpolate = function (plugin) { return function (msgid, context, parent, disableHtmlEscaping) { | ||
if (context === void 0) { context = {}; } | ||
if (disableHtmlEscaping === void 0) { disableHtmlEscaping = false; } | ||
var silent = plugin.silent; | ||
if (!silent && MUSTACHE_SYNTAX_RE.test(msgid)) { | ||
console.warn("Mustache syntax cannot be used with vue-gettext. Please use \"%{}\" instead of \"{{}}\" in: ".concat(msgid)); | ||
} | ||
var result = msgid.replace(INTERPOLATION_RE, function (_match, token) { | ||
var expression = token.trim(); | ||
var evaluated; | ||
var escapeHtmlMap = { | ||
"&": "&", | ||
"<": "<", | ||
">": ">", | ||
'"': """, | ||
"'": "'", | ||
}; | ||
// Avoid eval() by splitting `expression` and looping through its different properties if any, see #55. | ||
function getProps(obj, expression) { | ||
var arr = expression.split(EVALUATION_RE).filter(function (x) { return x; }); | ||
while (arr.length) { | ||
obj = obj[arr.shift()]; | ||
} | ||
return obj; | ||
var interpolate = function (plugin) { | ||
return function (msgid, context, disableHtmlEscaping, parent) { | ||
if (context === void 0) { context = {}; } | ||
if (disableHtmlEscaping === void 0) { disableHtmlEscaping = false; } | ||
var silent = plugin.silent; | ||
if (!silent && MUSTACHE_SYNTAX_RE.test(msgid)) { | ||
console.warn("Mustache syntax cannot be used with vue-gettext. Please use \"%{}\" instead of \"{{}}\" in: ".concat(msgid)); | ||
} | ||
function evalInContext(context, expression, parent) { | ||
try { | ||
evaluated = getProps(context, expression); | ||
var result = msgid.replace(INTERPOLATION_RE, function (_match, token) { | ||
var expression = token.trim(); | ||
var evaluated; | ||
var escapeHtmlMap = { | ||
"&": "&", | ||
"<": "<", | ||
">": ">", | ||
'"': """, | ||
"'": "'", | ||
}; | ||
// Avoid eval() by splitting `expression` and looping through its different properties if any, see #55. | ||
function getProps(obj, expression) { | ||
var arr = expression.split(EVALUATION_RE).filter(function (x) { return x; }); | ||
while (arr.length) { | ||
obj = obj[arr.shift()]; | ||
} | ||
return obj; | ||
} | ||
catch (e) { | ||
// Ignore errors, because this function may be called recursively later. | ||
} | ||
if (evaluated === undefined) { | ||
if (parent) { | ||
// Recursively climb the parent chain to allow evaluation inside nested components, see #23 and #24. | ||
return evalInContext(parent.ctx, expression, parent.parent); | ||
function evalInContext(context, expression, parent) { | ||
try { | ||
evaluated = getProps(context, expression); | ||
} | ||
else { | ||
console.warn("Cannot evaluate expression: ".concat(expression)); | ||
evaluated = expression; | ||
catch (e) { | ||
// Ignore errors, because this function may be called recursively later. | ||
} | ||
if (evaluated === undefined) { | ||
if (parent) { | ||
// Recursively climb the parent chain to allow evaluation inside nested components, see #23 and #24. | ||
return evalInContext(parent.ctx, expression, parent.parent); | ||
} | ||
else { | ||
console.warn("Cannot evaluate expression: ".concat(expression)); | ||
evaluated = expression; | ||
} | ||
} | ||
var result = evaluated.toString(); | ||
if (disableHtmlEscaping) { | ||
// Do not escape HTML, see #78. | ||
return result; | ||
} | ||
// Escape HTML, see #78. | ||
return result.replace(/[&<>"']/g, function (m) { return escapeHtmlMap[m]; }); | ||
} | ||
var result = evaluated.toString(); | ||
if (disableHtmlEscaping) { | ||
// Do not escape HTML, see #78. | ||
return result; | ||
} | ||
// Escape HTML, see #78. | ||
return result.replace(/[&<>"']/g, function (m) { return escapeHtmlMap[m]; }); | ||
} | ||
return evalInContext(context, expression, parent); | ||
}); | ||
return result; | ||
}; }; | ||
return evalInContext(context, expression, parent); | ||
}); | ||
return result; | ||
}; | ||
}; | ||
// Store this values as function attributes for easy access elsewhere to bypass a Rollup | ||
@@ -275,6 +277,7 @@ // weak point with `export`: | ||
*/ | ||
getTranslation: function (msgid, n, context, defaultPlural, languageKey, parameters) { | ||
getTranslation: function (msgid, n, context, defaultPlural, languageKey, parameters, disableHtmlEscaping) { | ||
if (n === void 0) { n = 1; } | ||
if (context === void 0) { context = null; } | ||
if (defaultPlural === void 0) { defaultPlural = null; } | ||
if (disableHtmlEscaping === void 0) { disableHtmlEscaping = false; } | ||
if (languageKey === undefined) { | ||
@@ -284,3 +287,3 @@ languageKey = language.current; | ||
var interp = function (message, parameters) { | ||
return parameters ? language.interpolate(message, parameters) : message; | ||
return parameters ? language.interpolate(message, parameters, disableHtmlEscaping) : message; | ||
}; | ||
@@ -346,7 +349,10 @@ if (!msgid) { | ||
* @param {String} msgid - The translation key | ||
* @param {Object} parameters - The interpolation parameters | ||
* @param {Boolean} disableHtmlEscaping - Disable html escaping | ||
* | ||
* @return {String} The translated string | ||
*/ | ||
gettext: function (msgid, parameters) { | ||
return this.getTranslation(msgid, undefined, undefined, undefined, undefined, parameters); | ||
gettext: function (msgid, parameters, disableHtmlEscaping) { | ||
if (disableHtmlEscaping === void 0) { disableHtmlEscaping = false; } | ||
return this.getTranslation(msgid, undefined, undefined, undefined, undefined, parameters, disableHtmlEscaping); | ||
}, | ||
@@ -359,7 +365,10 @@ /* | ||
* @param {String} msgid - The translation key | ||
* @param {Object} parameters - The interpolation parameters | ||
* @param {Boolean} disableHtmlEscaping - Disable html escaping | ||
* | ||
* @return {String} The translated string | ||
*/ | ||
pgettext: function (context, msgid, parameters) { | ||
return this.getTranslation(msgid, 1, context, undefined, undefined, parameters); | ||
pgettext: function (context, msgid, parameters, disableHtmlEscaping) { | ||
if (disableHtmlEscaping === void 0) { disableHtmlEscaping = false; } | ||
return this.getTranslation(msgid, 1, context, undefined, undefined, parameters, disableHtmlEscaping); | ||
}, | ||
@@ -374,7 +383,10 @@ /* | ||
* @param {Number} n - The number to switch between singular and plural | ||
* @param {Object} parameters - The interpolation parameters | ||
* @param {Boolean} disableHtmlEscaping - Disable html escaping | ||
* | ||
* @return {String} The translated string | ||
*/ | ||
ngettext: function (msgid, plural, n, parameters) { | ||
return this.getTranslation(msgid, n, null, plural, undefined, parameters); | ||
ngettext: function (msgid, plural, n, parameters, disableHtmlEscaping) { | ||
if (disableHtmlEscaping === void 0) { disableHtmlEscaping = false; } | ||
return this.getTranslation(msgid, n, null, plural, undefined, parameters, disableHtmlEscaping); | ||
}, | ||
@@ -390,7 +402,10 @@ /* | ||
* @param {Number} n - The number to switch between singular and plural | ||
* @param {Object} parameters - The interpolation parameters | ||
* @param {Boolean} disableHtmlEscaping - Disable html escaping | ||
* | ||
* @return {String} The translated string | ||
*/ | ||
npgettext: function (context, msgid, plural, n, parameters) { | ||
return this.getTranslation(msgid, n, context, plural, undefined, parameters); | ||
npgettext: function (context, msgid, plural, n, parameters, disableHtmlEscaping) { | ||
if (disableHtmlEscaping === void 0) { disableHtmlEscaping = false; } | ||
return this.getTranslation(msgid, n, context, plural, undefined, parameters, disableHtmlEscaping); | ||
}, | ||
@@ -479,3 +494,3 @@ }); }; | ||
var translatedMsg = translate(plugin).getTranslation(msgid.value, props.translateN, props.translateContext, isPlural ? props.translatePlural : null, plugin.current); | ||
return interpolate(plugin)(translatedMsg, props.translateParams, (_a = vue.getCurrentInstance()) === null || _a === void 0 ? void 0 : _a.parent); | ||
return interpolate(plugin)(translatedMsg, props.translateParams, undefined, (_a = vue.getCurrentInstance()) === null || _a === void 0 ? void 0 : _a.parent); | ||
}); | ||
@@ -508,3 +523,3 @@ // The text must be wraped inside a root HTML element, so we use a <span> by default. | ||
var context = Object.assign(binding.instance, binding.value); | ||
var msg = interpolate(language)(translation, context, null, disableHtmlEscaping); | ||
var msg = interpolate(language)(translation, context, disableHtmlEscaping, null); | ||
el.innerHTML = msg; | ||
@@ -511,0 +526,0 @@ }; |
@@ -119,12 +119,12 @@ import * as vue from 'vue'; | ||
[key: string]: string; | ||
}) => string; | ||
}, disableHtmlEscaping?: boolean) => string; | ||
$pgettext: (context: string, msgid: string, parameters?: { | ||
[key: string]: string; | ||
}) => string; | ||
}, disableHtmlEscaping?: boolean) => string; | ||
$ngettext: (msgid: string, plural: string, n: number, parameters?: { | ||
[key: string]: string; | ||
}) => string; | ||
}, disableHtmlEscaping?: boolean) => string; | ||
$npgettext: (context: string, msgid: string, plural: string, n: number, parameters?: { | ||
[key: string]: string; | ||
}) => string; | ||
}, disableHtmlEscaping?: boolean) => string; | ||
interpolate: (msgid: string, context: object, disableHtmlEscaping?: boolean) => string; | ||
@@ -181,12 +181,12 @@ install: (app: App) => void; | ||
[key: string]: string; | ||
} | undefined) => string; | ||
} | undefined, disableHtmlEscaping?: boolean | undefined) => string; | ||
$pgettext: (context: string, msgid: string, parameters?: { | ||
[key: string]: string; | ||
} | undefined) => string; | ||
} | undefined, disableHtmlEscaping?: boolean | undefined) => string; | ||
$ngettext: (msgid: string, plural: string, n: number, parameters?: { | ||
[key: string]: string; | ||
} | undefined) => string; | ||
} | undefined, disableHtmlEscaping?: boolean | undefined) => string; | ||
$npgettext: (context: string, msgid: string, plural: string, n: number, parameters?: { | ||
[key: string]: string; | ||
} | undefined) => string; | ||
} | undefined, disableHtmlEscaping?: boolean | undefined) => string; | ||
interpolate: (msgid: string, context: object, disableHtmlEscaping?: boolean | undefined) => string; | ||
@@ -193,0 +193,0 @@ install: (app: App<any>) => void; |
{ | ||
"name": "vue3-gettext", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Translate Vue 3 applications with gettext", | ||
@@ -5,0 +5,0 @@ "homepage": "https://jshmrtn.github.io/vue3-gettext/", |
Sorry, the diff of this file is not supported yet
93762
1988