Commit e88ec8d3 authored by Adrien Pavie's avatar Adrien Pavie
Browse files

Testing translator functions

parent 86cacb06
......@@ -18,62 +18,62 @@
/**
* Allows label translating according to given translations.
*/
var Translator = {
var Translator = function () {
/** The loaded translations **/
translations: {},
this.translations = {};
/** The currently used locale **/
currentLocale: 'en',
this.currentLocale = 'en';
};
/** Change the current locale **/
Translator.prototype.setLocale = function(lng) {
if(this.translations[lng] !== undefined) {
this.currentLocale = lng;
}
else if(this.translations[lng.split('-')[0]]) {
this.currentLocale = lng.split('-')[0];
}
};
/** Add a new language **/
Translator.prototype.addTranslation = function(lng, data) {
this.translations[lng] = data;
};
/**
* Given a string identifier, try to find that string in the current
* language, and return it.
* @param s string identifier
* @param o Optional parameters to replace in the label
* @param lng Optional, the locale to use
* @returns locale string
*/
Translator.prototype.t = function(s, o, loc) {
loc = loc || this.currentLocale;
var path = s.split('.').reverse();
var rep = this.translations[loc];
while (rep !== undefined && path.length) rep = rep[path.pop()];
/** Change the current locale **/
setLocale: function(lng) {
if(translations[lng] !== undefined) {
currentLocale = lng;
}
else if(translations[lng.split('-')[0]]) {
currentLocale = lng.split('-')[0];
}
},
if (rep !== undefined) {
if (o) for (var k in o) rep = rep.replace('{' + k + '}', o[k]);
return rep;
}
/** Add a new language **/
addTranslation: function(lng, data) {
translations[lng] = data;
},
if (loc !== 'en') {
return this.t(s, o, 'en');
}
/**
* Given a string identifier, try to find that string in the current
* language, and return it.
* @param s string identifier
* @param o Optional parameters to replace in the label
* @param lng Optional, the locale to use
* @returns locale string
*/
t: function(s, o, loc) {
loc = loc || currentLocale;
var path = s.split('.').reverse();
var rep = translations[loc];
while (rep !== undefined && path.length) rep = rep[path.pop()];
if (rep !== undefined) {
if (o) for (var k in o) rep = rep.replace('{' + k + '}', o[k]);
return rep;
}
if (loc !== 'en') {
return t(s, o, 'en');
}
if (o && 'default' in o) {
return o.default;
}
var missing = 'Missing ' + loc + ' translation: ' + s;
if (typeof console !== 'undefined') console.error(missing); // eslint-disable-line
return missing;
if (o && 'default' in o) {
return o.default;
}
console.error('[Translator] Missing ' + loc + ' translation: ' + s);
return 'Missing label';
};
module.exports = Translator;
/*
* Test script for Translator.js
*/
var $ = require("jquery");
var QUnit = require("qunitjs");
var Translator = require("../../src/ctrl/service/Translator");
QUnit.module("Ctrl > Service > Translator");
/*
* CONSTRUCTOR
*/
QUnit.test("Constructor", function(assert) {
var tsl = new Translator();
assert.equal(tsl.currentLocale, 'en');
assert.ok($.isEmptyObject(tsl.translations));
});
/*
* ACCESSORS
*/
//t
QUnit.test("t with valid parameters", function(assert) {
var tsl2 = new Translator();
tsl2.addTranslation('en', { "label1": "val1", "label2": "val2" });
tsl2.addTranslation('fr', { "label1": "val1fr", "label2": "val2fr" });
assert.equal(tsl2.t("label1"), "val1");
assert.equal(tsl2.t("label2"), "val2");
tsl2.setLocale('fr');
assert.equal(tsl2.t("label1"), "val1fr");
assert.equal(tsl2.t("label2"), "val2fr");
});
QUnit.test("t with subchains", function(assert) {
var tsl2 = new Translator();
tsl2.addTranslation('en', {
"main": { "l1": "v1", "l2": "v2" },
"second": { "l1": "v1s", "l2": "v2s" },
"third": { "sub": { "l1": "l1t" } }
});
tsl2.addTranslation('fr', {
"main": { "l1": "v1fr", "l2": "v2fr" },
"second": { "l1": "v1sfr", "l2": "v2sfr" },
"third": { "sub": { "l1": "l1tfr" } }
});
assert.equal(tsl2.t("main.l1"), "v1");
assert.equal(tsl2.t("main.l2"), "v2");
assert.equal(tsl2.t("second.l1"), "v1s");
assert.equal(tsl2.t("second.l2"), "v2s");
assert.equal(tsl2.t("third.sub.l1"), "l1t");
tsl2.setLocale('fr');
assert.equal(tsl2.t("main.l1"), "v1fr");
assert.equal(tsl2.t("main.l2"), "v2fr");
assert.equal(tsl2.t("second.l1"), "v1sfr");
assert.equal(tsl2.t("second.l2"), "v2sfr");
assert.equal(tsl2.t("third.sub.l1"), "l1tfr");
});
QUnit.test("t with invalid parameters", function(assert) {
var tsl2 = new Translator();
tsl2.addTranslation('en', { "label1": "val1", "label2": "val2" });
tsl2.addTranslation('fr', { "label1": "val1fr", "label2": "val2fr" });
assert.equal(tsl2.t("unknown"), "Missing label");
});
/*
* MODIFIERS
*/
//setLocale
QUnit.test("SetLocale with valid parameters", function(assert) {
var tsl2 = new Translator();
tsl2.addTranslation('en', { "label1": "val1", "label2": "val2" });
tsl2.addTranslation('fr', { "label1": "val1fr", "label2": "val2fr" });
assert.equal(tsl2.t("label1"), "val1");
tsl2.setLocale('fr');
assert.equal(tsl2.currentLocale, "fr");
assert.equal(tsl2.t("label1"), "val1fr");
});
QUnit.test("SetLocale with language variant", function(assert) {
var tsl2 = new Translator();
tsl2.addTranslation('en', { "label1": "val1", "label2": "val2" });
tsl2.addTranslation('fr', { "label1": "val1fr", "label2": "val2fr" });
tsl2.addTranslation('fr-BE', { "label1": "val1frbe", "label2": "val2frbe" });
assert.equal(tsl2.t("label1"), "val1");
tsl2.setLocale('fr-BE');
assert.equal(tsl2.currentLocale, "fr-BE");
assert.equal(tsl2.t("label1"), "val1frbe");
});
QUnit.test("SetLocale with unknown language", function(assert) {
var tsl3 = new Translator();
tsl3.addTranslation('en', { "label1": "val1", "label2": "val2" });
tsl3.addTranslation('fr', { "label1": "val1fr", "label2": "val2fr" });
assert.equal(tsl3.t("label1"), "val1");
tsl3.setLocale('ukn');
assert.equal(tsl3.currentLocale, "en");
assert.equal(tsl3.t("label1"), "val1");
});
QUnit.test("SetLocale with unknown language variant", function(assert) {
var tsl3 = new Translator();
tsl3.addTranslation('en', { "label1": "val1", "label2": "val2" });
tsl3.addTranslation('fr', { "label1": "val1fr", "label2": "val2fr" });
assert.equal(tsl3.t("label1"), "val1");
tsl3.setLocale('fr-BE');
assert.equal(tsl3.currentLocale, "fr");
assert.equal(tsl3.t("label1"), "val1fr");
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment