diff --git a/lib/i18n.js b/lib/i18n.js index 374c879688adabc933a3466cc9bc168beeeddbf5..a17cb8678f8044224cb5558697320e573e1a5c22 100644 --- a/lib/i18n.js +++ b/lib/i18n.js @@ -251,15 +251,17 @@ exports.languageFrom = function (locale) { * of values for positional replacement. * * Named Example: - * format("%(salutation)s %(place)s", {salutation: "Hello", place: "World"}, true); + * format("%(salutation)s %(place)s", {salutation: "Hello", place: "World"}); * Positional Example: * format("%s %s", ["Hello", "World"]); */ var format = exports.format = function (fmt, obj, named) { - if (! fmt) return ""; - if (named) { - return fmt.replace(/%\(\w+\)s/g, function(match){return String(obj[match.slice(2,-2)])}); - } else { + if (!fmt) return ""; + if (Array.isArray(obj) || named === false) { return fmt.replace(/%s/g, function(match){return String(obj.shift())}); + } else if (typeof obj === 'object' || named === true) { + return fmt.replace(/%\(\s*([^)]+)\s*\)/g, function(m, v){ + return String(obj[v]); + }); } }; diff --git a/resources/static/common/js/gettext.js b/resources/static/common/js/gettext.js index 2b24477c5a468f3f8ac97ebcabb135da4f3c94d1..0ee2f7e36b7416d41beed987b842b676ff392bb1 100644 --- a/resources/static/common/js/gettext.js +++ b/resources/static/common/js/gettext.js @@ -20,14 +20,16 @@ }, // See lib/i18n.js format docs format: function (fmt, obj, named) { - if (! fmt) return ""; - if (! fmt.replace) { + if (!fmt) return ""; + if (!fmt.replace) { return fmt; } - if (named) { - return fmt.replace(/%\(\w+\)s/g, function(match){return String(obj[match.slice(2,-2)])}); - } else { + if (_.isArray(obj) || named === false) { return fmt.replace(/%s/g, function(match){return String(obj.shift())}); + } else if (_.isObject(obj) || named === true) { + return fmt.replace(/%\(\s*([^)]+)\s*\)/g, function(m, v){ + return String(obj[v]); + }); } } }; @@ -40,5 +42,4 @@ var gt = new Gettext(params); window.gettext = gt.gettext.bind(gt); window.format = gt.format.bind(gt); - }());