summaryrefslogtreecommitdiff
path: root/js/codeq
diff options
context:
space:
mode:
authorAleš Smodiš <aless@guru.si>2015-09-30 17:37:31 +0200
committerAleš Smodiš <aless@guru.si>2015-09-30 17:37:31 +0200
commit0da1117cfc28688633be7b8382aa60435bf740eb (patch)
treeb464b92e673419db595c231ed1ba8e7c6cfd783b /js/codeq
parent8995341d43efe16f307b55f200821944cfecf4c8 (diff)
Implemented GUI translations via the data-tkey tag attribute and res/*.json files.
Diffstat (limited to 'js/codeq')
-rw-r--r--js/codeq/comms.js14
-rw-r--r--js/codeq/core.js63
-rw-r--r--js/codeq/translation.js10
3 files changed, 71 insertions, 16 deletions
diff --git a/js/codeq/comms.js b/js/codeq/comms.js
index ecca6ff..f416e88 100644
--- a/js/codeq/comms.js
+++ b/js/codeq/comms.js
@@ -165,7 +165,6 @@
// ================================================================================
var languageCache = {}, // language defs, keyed by language identifier
- problemCache = {}, // problem cache, 3-level, keyed by: language, problem group, and problem identifier
ajaxGet = function (url) {
return Q.Promise(function (resolve, reject, notify) {
$.ajax({
@@ -197,7 +196,7 @@
});
});
},
- ajaxPrefix;
+ ajaxPrefix, ajaxDataPrefix, ajaxResPrefix;
ajaxPrefix = location.pathname;
if (!ajaxPrefix) ajaxPrefix = '/';
@@ -206,7 +205,8 @@
ajaxPrefix[ajaxPrefix.length - 1] = '';
ajaxPrefix = ajaxPrefix.join('/');
}
- ajaxPrefix = ajaxPrefix + 'data/';
+ ajaxDataPrefix = ajaxPrefix + 'data/';
+ ajaxResPrefix = ajaxPrefix + 'res/';
// ================================================================================
// This module's API methods
@@ -399,7 +399,7 @@
return Q(x);
}
}
- x = ajaxGet(ajaxPrefix + identifier + '/language.json').then(
+ x = ajaxGet(ajaxDataPrefix + identifier + '/language.json').then(
function getLanguageDefSuccess(data) {
languageCache[identifier] = data;
return data; // proxy further
@@ -414,7 +414,11 @@
},
'getProblemDef': function (language, group, problem) {
- return ajaxGet(ajaxPrefix + language + '/' + group + '/' + problem + '/problem.json');
+ return ajaxGet(ajaxDataPrefix + language + '/' + group + '/' + problem + '/problem.json');
+ },
+
+ 'getGuiTranslation': function (lang) {
+ return ajaxGet(ajaxResPrefix + lang + '.json');
}
};
})();
diff --git a/js/codeq/core.js b/js/codeq/core.js
index 01382d1..29b497c 100644
--- a/js/codeq/core.js
+++ b/js/codeq/core.js
@@ -372,6 +372,49 @@
// The boot sequence
// ================================================================================
+ var loadGuiTranslations = function () {
+ var langs = codeq.availableLangs,
+ loaders = [],
+ loaderLangs = [],
+ i, lang;
+ for (i = langs.length - 1; i >= 0; i--) {
+ lang = langs[i];
+ loaders.push(codeq.comms.getGuiTranslation(lang));
+ loaderLangs.push(lang);
+ }
+ return Q.all(loaders)
+ .then(function (results) {
+ // convert translations into something that the translation module can use
+ var dictionary = {},
+ i, json, key, lang, translations;
+ for (i = results.length - 1; i >= 0; i--) {
+ json = results[i];
+ lang = loaderLangs[i];
+ for (key in json) {
+ if (!json.hasOwnProperty(key)) continue;
+ translations = dictionary[key];
+ if (translations) translations[lang] = json[key];
+ else {
+ translations = {};
+ dictionary[key] = translations;
+ translations[lang] = json[key];
+ }
+ }
+ }
+ // ensure each key contains all translations
+ for (key in dictionary) {
+ if (!dictionary.hasOwnProperty(key)) continue;
+ translations = dictionary[key];
+ for (i = langs.length - 1; i >= 0; i--) {
+ lang = langs[i];
+ if (!(lang in translations)) translations[lang] = "(Untranslated: " + lang + ")";
+ }
+ }
+ // register with the system
+ codeq.tr.registerDictionary('gui', dictionary);
+ });
+ };
+
$(document).ready(function () {
// set the language
var navigatorLang = navigator.language || navigator.browserLanguage, // language reported by browser
@@ -387,11 +430,17 @@
codeq.availableLangs.push(key);
}
- codeq.fire('init'); // tell any interested modules that we are not initialized, perhaps they want to initialize too
- codeq.setLang(lang || 'en'); // initial language setting
- // go to login
- codeq.globalStateMachine.transition('login');
-
- //For performance reasons, the Tooltip and Popover data-apis are opt-in, meaning you must initialize them yourself.
- $('[data-toggle="popover"]').popover()});
+ // the boot chain: must be a sequence of .then() terminated with .done()
+ loadGuiTranslations()
+ .then(function () {
+ codeq.fire('init'); // tell any interested modules that we are now initialized, perhaps they want to initialize too
+ codeq.setLang(lang || 'en'); // initial language setting, this also translates the GUI
+ // go to login
+ codeq.globalStateMachine.transition('login');
+
+ //For performance reasons, the Tooltip and Popover data-apis are opt-in, meaning you must initialize them yourself.
+ $('[data-toggle="popover"]').popover()
+ })
+ .done();
+ });
})();
diff --git a/js/codeq/translation.js b/js/codeq/translation.js
index f33199d..67f49eb 100644
--- a/js/codeq/translation.js
+++ b/js/codeq/translation.js
@@ -2,7 +2,7 @@
var dicts = {},
translateElement = function (jqElt, lang) {
- var dictionaryKey = jqElt.data('dict'),
+ var dictionaryKey = jqElt.data('dict') || 'gui',
translationKey = jqElt.data('tkey'),
dict = dicts[dictionaryKey],
translations, html, key;
@@ -43,7 +43,7 @@
jqElt.html(html);
},
translateDocument = function (lang) {
- $('.translatable').each(function () {
+ $('[data-tkey]').each(function () {
translateElement($(this), lang);
});
};
@@ -70,8 +70,10 @@
'translateDom': function (jqTopElt) {
var lang = codeq.getLang();
- if (jqTopElt.hasClass('translatable')) translateElement(jqTopElt, lang);
- jqTopElt.find('.translatable').each(function () {
+ jqTopElt.filter('[data-tkey]').each(function () {
+ translateElement(jqTopElt, lang)
+ });
+ jqTopElt.find('[data-tkey]').each(function () {
translateElement($(this), lang);
});
}