v1.18.x
barisusakli 11 years ago
parent 2a88e08977
commit 7825b8b951

@ -6,6 +6,12 @@
var translator = {}, var translator = {},
languages = {}; languages = {};
var regexes = {
match: /\[\[.*?\]\]/g,
split: /[,][\s]*/,
replace: /\]+$/
};
module.exports = translator; module.exports = translator;
translator.addTranslation = function(language, filename, translations) { translator.addTranslation = function(language, filename, translations) {
@ -69,7 +75,7 @@
} }
}; };
translator.translate = function (data, language, callback) { translator.translate = function (text, language, callback) {
if (typeof language === 'function') { if (typeof language === 'function') {
callback = language; callback = language;
if ('undefined' !== typeof window && config) { if ('undefined' !== typeof window && config) {
@ -80,69 +86,70 @@
} }
} }
if (!data) { if (!text) {
return callback(data); return callback(text);
} }
function insertLanguage(text, key, value, variables) { var keys = text.match(regexes.match);
if (value) {
for (var i = 1, ii = variables.length; i < ii; i++) {
var variable = variables[i].replace(']]', '');
value = value.replace('%' + i, variable);
}
text = text.replace(key, value); if (!keys) {
} else { return callback(text);
var string = key.split(':');
text = text.replace(key, string[string.length-1].replace(/\]+$/, ''));
} }
return text; translateKeys(keys, text, language, callback);
} };
var keys = data.match(/\[\[.*?\]\]/g), function translateKeys(keys, text, language, callback) {
loading = 0;
if (!keys) { var count = keys.length;
return callback(data); if (!count) {
return callback(text);
} }
for (var i=0; i<keys.length; ++i) { var data = {text: text};
var key = keys[i]; keys.forEach(function(key) {
translateKey(key, data, language, function(translated) {
--count;
if (count <= 0) {
callback(translated.text);
}
});
});
}
function translateKey(key, data, language, callback) {
key = '' + key; key = '' + key;
var variables = key.split(/[,][\s]*/); var variables = key.split(regexes.split);
var parsedKey = key.replace('[[', '').replace(']]', '').split(':'); var parsedKey = key.replace('[[', '').replace(']]', '').split(':');
if (!(parsedKey[0] && parsedKey[1])) { if (!(parsedKey[0] && parsedKey[1])) {
continue; return;
} }
var languageFile = parsedKey[0]; var languageFile = parsedKey[0];
parsedKey = ('' + parsedKey[1]).split(',')[0]; parsedKey = ('' + parsedKey[1]).split(',')[0];
if (isLanguageFileLoaded(language, languageFile)) { translator.load(language, languageFile, function(languageData) {
data = insertLanguage(data, key, languages[language].loaded[languageFile][parsedKey], variables); data.text = insertLanguage(data.text, key, languageData[parsedKey], variables);
} else { callback(data);
loading++;
(function (languageKey, parsedKey, languageFile, variables) {
translator.load(language, languageFile, function (languageData) {
data = insertLanguage(data, languageKey, languageData[parsedKey], variables);
loading--;
checkComplete();
}); });
}(key, parsedKey, languageFile, variables));
}
} }
checkComplete(); function insertLanguage(text, key, value, variables) {
if (value) {
for (var i = 1, ii = variables.length; i < ii; i++) {
var variable = variables[i].replace(']]', '');
value = value.replace('%' + i, variable);
}
function checkComplete() { text = text.replace(key, value);
if (loading === 0) { } else {
callback(data); var string = key.split(':');
text = text.replace(key, string[string.length-1].replace(regexes.replace, ''));
} }
return text;
} }
};
translator.compile = function() { translator.compile = function() {
var args = Array.prototype.slice.call(arguments, 0); var args = Array.prototype.slice.call(arguments, 0);

Loading…
Cancel
Save