From d97ab731fb95d4634b7edd8b626298eb92184e7a Mon Sep 17 00:00:00 2001 From: Lloyd Hilaiel <lloyd@hilaiel.com> Date: Fri, 13 Jul 2012 13:39:43 -0600 Subject: [PATCH] extensive testing of i18n mechanism - including english fallback, string substition from .json files, and debug locale --- lib/static/views.js | 5 ++ resources/views/i18n_test.ejs | 1 + tests/i18n-tests.js | 97 +++++++++++++++++++++++--- tests/i18n_test_files/bg/client.json | 0 tests/i18n_test_files/bg/messages.json | 8 +++ 5 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 resources/views/i18n_test.ejs create mode 100644 tests/i18n_test_files/bg/client.json create mode 100644 tests/i18n_test_files/bg/messages.json diff --git a/lib/static/views.js b/lib/static/views.js index 957bfb09c..608a2baf0 100644 --- a/lib/static/views.js +++ b/lib/static/views.js @@ -197,6 +197,11 @@ exports.setup = function(app) { app.get(/^\/test\/(?:index.html)?$/, function (req, res) { res.render('test.ejs', {title: 'Mozilla Persona QUnit Test', layout: false}); }); + + // l10n test template + app.get('/i18n_test', function(req, res) { + renderCachableView(req, res, 'i18n_test.ejs', { layout: false, title: 'l10n testing title' }); + }); } else { // this is stage or production, explicitly disable all resources under /test app.get(/^\/test/, function(req, res) { diff --git a/resources/views/i18n_test.ejs b/resources/views/i18n_test.ejs new file mode 100644 index 000000000..fcec0740e --- /dev/null +++ b/resources/views/i18n_test.ejs @@ -0,0 +1 @@ +<%- gettext("This is a translation <strong>test</strong> string.") %> diff --git a/tests/i18n-tests.js b/tests/i18n-tests.js index 88287ec5d..50bb82dad 100755 --- a/tests/i18n-tests.js +++ b/tests/i18n-tests.js @@ -8,16 +8,22 @@ require('./lib/test_env.js'); const assert = require('assert'), vows = require('vows'), - i18n = require('../lib/i18n'); + i18n = require('../lib/i18n'), + start_stop = require('./lib/start-stop.js'), + wsapi = require('./lib/wsapi.js'), + http = require('http'), + path = require('path'); var suite = vows.describe('i18n'); +suite.options.error = false; + suite.addBatch({ "format a string with place values": { topic: function () { return i18n.format("%s %s!", ["Hello", "World"]); }, - "was interpolated": function (err, str) { + "was interpolated": function (str) { assert.equal(str, "Hello World!"); } } @@ -29,7 +35,7 @@ suite.addBatch({ var params = { salutation: "Hello", place: "World" }; return i18n.format("%(salutation) %(place)!", params); }, - "was interpolated": function (err, str) { + "was interpolated": function (str) { assert.equal(str, "Hello World!"); } } @@ -40,7 +46,7 @@ suite.addBatch({ topic: function () { return i18n.format("Hello World!"); }, - "was interpolated": function (err, str) { + "was interpolated": function (str) { assert.equal(str, "Hello World!"); } }, @@ -48,7 +54,7 @@ suite.addBatch({ topic: function () { return i18n.format(null); }, - "was interpolated": function (err, str) { + "was interpolated": function (str) { assert.equal(str, ""); } } @@ -64,7 +70,7 @@ suite.addBatch({ i18n.parseAcceptLanguage(accept), supported, def); }, - "For Punjabi": function (err, locale) { + "For Punjabi": function (locale) { assert.equal(locale, "pa"); } }, @@ -77,7 +83,7 @@ suite.addBatch({ i18n.parseAcceptLanguage(accept), supported, def); }, - "For Punjabi (India) serve Punjabi": function (err, locale) { + "For Punjabi (India) serve Punjabi": function (locale) { assert.equal(locale, "pa"); } }, @@ -90,12 +96,87 @@ suite.addBatch({ i18n.parseAcceptLanguage(accept), supported, def); }, - "Don't choose Punjabi (India)": function (err, locale) { + "Don't choose Punjabi (India)": function (locale) { assert.equal(locale, "en-us"); } } }); +// point to test translation files +process.env['TRANSLATION_DIR'] = path.join(__dirname, "i18n_test_files"); + +// supported languages for the purposes of this test +process.env['SUPPORTED_LANGUAGES'] = 'en,bg,it-CH'; + +// now let's start up our servers +start_stop.addStartupBatches(suite); + +function getTestTemplate(langs) { + return function() { + var self = this; + var req = http.request({ + host: '127.0.0.1', + port: 10002, + path: '/i18n_test', + method: "GET", + headers: { 'Accept-Language': langs } + }, function (res) { + var body = ""; + res.on('data', function(chunk) { body += chunk; }) + .on('end', function() { + self.callback(null, { code: res.statusCode, body: body }); + }); + }).on('error', function (e) { + self.callback(e); + }); + req.end(); + }; +} + +suite.addBatch({ + // test default language + "test template with no headers": { + topic: getTestTemplate(undefined), + "returns english" : function(err, r) { + assert.strictEqual(r.code, 200); + assert.strictEqual( + r.body.trim(), + 'This is a translation <strong>test</strong> string.'); + } + }, + // test un-supported case + "test template with german headers": { + topic: getTestTemplate('de'), + "returns english" : function(err, r) { + assert.strictEqual(200, r.code); + assert.strictEqual( + r.body.trim(), + 'This is a translation <strong>test</strong> string.'); + } + }, + // test debug translation + "test template with debug headers": { + topic: getTestTemplate('it-CH'), + "returns gobbledygook" : function(err, r) { + assert.strictEqual(200, r.code); + assert.strictEqual( + r.body.trim(), + '.ƃuıɹʇs <strong>ʇsÇʇ</strong> uoıʇaÊ…suaɹʇ a sı sıɥ⊥'); + } + }, + // test .json extraction + "bulgarian accept headers": { + topic: getTestTemplate('bg'), + "return a translation extacted from .json file" : function(err, r) { + assert.strictEqual(200, r.code); + assert.strictEqual(r.body.trim(), "Прова? Прова? Четери, пет, шещ?"); + } + } +}); + +// and let's stop them servers +start_stop.addShutdownBatches(suite); + // run or export the suite. if (process.argv[1] === __filename) suite.run(); else suite.export(module); diff --git a/tests/i18n_test_files/bg/client.json b/tests/i18n_test_files/bg/client.json new file mode 100644 index 000000000..e69de29bb diff --git a/tests/i18n_test_files/bg/messages.json b/tests/i18n_test_files/bg/messages.json new file mode 100644 index 000000000..007689c41 --- /dev/null +++ b/tests/i18n_test_files/bg/messages.json @@ -0,0 +1,8 @@ +var json_locale_data = { + messages: { + "This is a translation <strong>test</strong> string.": [ + null, + "Прова? Прова? Четери, пет, шещ?" + ] + } +}; -- GitLab