Skip to content
Snippets Groups Projects
Commit d97ab731 authored by Lloyd Hilaiel's avatar Lloyd Hilaiel
Browse files

extensive testing of i18n mechanism - including english fallback, string...

extensive testing of i18n mechanism - including english fallback, string substition from .json files, and debug locale
parent 5ca46a35
No related branches found
No related tags found
No related merge requests found
...@@ -197,6 +197,11 @@ exports.setup = function(app) { ...@@ -197,6 +197,11 @@ exports.setup = function(app) {
app.get(/^\/test\/(?:index.html)?$/, function (req, res) { app.get(/^\/test\/(?:index.html)?$/, function (req, res) {
res.render('test.ejs', {title: 'Mozilla Persona QUnit Test', layout: false}); 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 { } else {
// this is stage or production, explicitly disable all resources under /test // this is stage or production, explicitly disable all resources under /test
app.get(/^\/test/, function(req, res) { app.get(/^\/test/, function(req, res) {
......
<%- gettext("This is a translation <strong>test</strong> string.") %>
...@@ -8,16 +8,22 @@ require('./lib/test_env.js'); ...@@ -8,16 +8,22 @@ require('./lib/test_env.js');
const assert = require('assert'), const assert = require('assert'),
vows = require('vows'), 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'); var suite = vows.describe('i18n');
suite.options.error = false;
suite.addBatch({ suite.addBatch({
"format a string with place values": { "format a string with place values": {
topic: function () { topic: function () {
return i18n.format("%s %s!", ["Hello", "World"]); return i18n.format("%s %s!", ["Hello", "World"]);
}, },
"was interpolated": function (err, str) { "was interpolated": function (str) {
assert.equal(str, "Hello World!"); assert.equal(str, "Hello World!");
} }
} }
...@@ -29,7 +35,7 @@ suite.addBatch({ ...@@ -29,7 +35,7 @@ suite.addBatch({
var params = { salutation: "Hello", place: "World" }; var params = { salutation: "Hello", place: "World" };
return i18n.format("%(salutation) %(place)!", params); return i18n.format("%(salutation) %(place)!", params);
}, },
"was interpolated": function (err, str) { "was interpolated": function (str) {
assert.equal(str, "Hello World!"); assert.equal(str, "Hello World!");
} }
} }
...@@ -40,7 +46,7 @@ suite.addBatch({ ...@@ -40,7 +46,7 @@ suite.addBatch({
topic: function () { topic: function () {
return i18n.format("Hello World!"); return i18n.format("Hello World!");
}, },
"was interpolated": function (err, str) { "was interpolated": function (str) {
assert.equal(str, "Hello World!"); assert.equal(str, "Hello World!");
} }
}, },
...@@ -48,7 +54,7 @@ suite.addBatch({ ...@@ -48,7 +54,7 @@ suite.addBatch({
topic: function () { topic: function () {
return i18n.format(null); return i18n.format(null);
}, },
"was interpolated": function (err, str) { "was interpolated": function (str) {
assert.equal(str, ""); assert.equal(str, "");
} }
} }
...@@ -64,7 +70,7 @@ suite.addBatch({ ...@@ -64,7 +70,7 @@ suite.addBatch({
i18n.parseAcceptLanguage(accept), i18n.parseAcceptLanguage(accept),
supported, def); supported, def);
}, },
"For Punjabi": function (err, locale) { "For Punjabi": function (locale) {
assert.equal(locale, "pa"); assert.equal(locale, "pa");
} }
}, },
...@@ -77,7 +83,7 @@ suite.addBatch({ ...@@ -77,7 +83,7 @@ suite.addBatch({
i18n.parseAcceptLanguage(accept), i18n.parseAcceptLanguage(accept),
supported, def); supported, def);
}, },
"For Punjabi (India) serve Punjabi": function (err, locale) { "For Punjabi (India) serve Punjabi": function (locale) {
assert.equal(locale, "pa"); assert.equal(locale, "pa");
} }
}, },
...@@ -90,12 +96,87 @@ suite.addBatch({ ...@@ -90,12 +96,87 @@ suite.addBatch({
i18n.parseAcceptLanguage(accept), i18n.parseAcceptLanguage(accept),
supported, def); supported, def);
}, },
"Don't choose Punjabi (India)": function (err, locale) { "Don't choose Punjabi (India)": function (locale) {
assert.equal(locale, "en-us"); 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. // run or export the suite.
if (process.argv[1] === __filename) suite.run(); if (process.argv[1] === __filename) suite.run();
else suite.export(module); else suite.export(module);
var json_locale_data = {
messages: {
"This is a translation <strong>test</strong> string.": [
null,
"Прова? Прова? Четери, пет, шещ?"
]
}
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment