diff --git a/lib/i18n.js b/lib/i18n.js index cfe804c6b894cc1407a84a6e66d48f3c4154728d..cb7979938d3c57122ac64acd58d793b5c269e8a9 100644 --- a/lib/i18n.js +++ b/lib/i18n.js @@ -189,6 +189,7 @@ exports.localeFrom = localeFrom = function (language) { * format("%s %s", ["Hello", "World"]); */ exports.format = 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 { diff --git a/resources/static/shared/gettext.js b/resources/static/shared/gettext.js index 46cca99b19b90f4f11cf02373ddca53062ac3e57..dab6ac4f144e3c6ef9643f6d7bc6d8856aca35fe 100644 --- a/resources/static/shared/gettext.js +++ b/resources/static/shared/gettext.js @@ -16,8 +16,8 @@ function Gettext(params) { }, // See lib/i18n.js format docs format: function (fmt, obj, named) { + if (! fmt) return ""; if (! fmt.replace) { - console.log("format called with", fmt); return fmt; } if (named) { diff --git a/tests/i18n-tests.js b/tests/i18n-tests.js new file mode 100755 index 0000000000000000000000000000000000000000..66c5e4bf5ced647a757836bd66da8e162b21eb3b --- /dev/null +++ b/tests/i18n-tests.js @@ -0,0 +1,59 @@ +#!/usr/bin/env node + +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +require('./lib/test_env.js'); + +const assert = require('assert'), + vows = require('vows'), + i18n = require('../lib/i18n'); + +var suite = vows.describe('i18n'); + +suite.addBatch({ + "format a string with place values": { + topic: function () { + return i18n.format("%s %s!", ["Hello", "World"]); + }, + "was interpolated": function (err, str) { + assert.equal(str, "Hello World!"); + } + } +}); + +suite.addBatch({ + "format a string with named values": { + topic: function () { + var params = { salutation: "Hello", place: "World" }; + return i18n.format("%(salutation)s %(place)s!", params, true); + }, + "was interpolated": function (err, str) { + assert.equal(str, "Hello World!"); + } + } +}); + +suite.addBatch({ + "format a string without interpolation": { + topic: function () { + return i18n.format("Hello World!"); + }, + "was interpolated": function (err, str) { + assert.equal(str, "Hello World!"); + } + }, + "format a null": { + topic: function () { + return i18n.format(null); + }, + "was interpolated": function (err, str) { + assert.equal(str, ""); + } + } +}); + +// run or export the suite. +if (process.argv[1] === __filename) suite.run(); +else suite.export(module);