mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-09 15:35:47 +01:00
finished initial client side & server side language parsing methods; integrated preloading into ajaxify and server app.js
This commit is contained in:
@@ -2,6 +2,7 @@ var ajaxify = {};
|
|||||||
|
|
||||||
|
|
||||||
(function ($) {
|
(function ($) {
|
||||||
|
/*global app, templates, utils*/
|
||||||
|
|
||||||
var location = document.location || window.location,
|
var location = document.location || window.location,
|
||||||
rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : ''),
|
rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : ''),
|
||||||
@@ -66,6 +67,8 @@ var ajaxify = {};
|
|||||||
}, url, RELATIVE_PATH + "/" + url);
|
}, url, RELATIVE_PATH + "/" + url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
translator.load(tpl_url);
|
||||||
|
|
||||||
jQuery('#footer, #content').fadeOut(100);
|
jQuery('#footer, #content').fadeOut(100);
|
||||||
|
|
||||||
templates.flush();
|
templates.flush();
|
||||||
@@ -93,7 +96,7 @@ var ajaxify = {};
|
|||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
};
|
||||||
|
|
||||||
$('document').ready(function () {
|
$('document').ready(function () {
|
||||||
if (!window.history || !window.history.pushState) return; // no ajaxification for old browsers
|
if (!window.history || !window.history.pushState) return; // no ajaxification for old browsers
|
||||||
@@ -127,7 +130,7 @@ var ajaxify = {};
|
|||||||
|
|
||||||
function nodeName(elem, name) {
|
function nodeName(elem, name) {
|
||||||
return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
|
return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
|
||||||
};
|
}
|
||||||
|
|
||||||
function evalScript(elem) {
|
function evalScript(elem) {
|
||||||
var data = (elem.text || elem.textContent || elem.innerHTML || ""),
|
var data = (elem.text || elem.textContent || elem.innerHTML || ""),
|
||||||
@@ -149,7 +152,7 @@ var ajaxify = {};
|
|||||||
head.insertBefore(script, head.firstChild);
|
head.insertBefore(script, head.firstChild);
|
||||||
//TODO: remove from head before inserting?, doing this breaks scripts in safari so commented out for now
|
//TODO: remove from head before inserting?, doing this breaks scripts in safari so commented out for now
|
||||||
//head.removeChild(script);
|
//head.removeChild(script);
|
||||||
};
|
}
|
||||||
|
|
||||||
var scripts = [],
|
var scripts = [],
|
||||||
script,
|
script,
|
||||||
@@ -172,6 +175,6 @@ var ajaxify = {};
|
|||||||
}
|
}
|
||||||
evalScript(scripts[i]);
|
evalScript(scripts[i]);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
}(jQuery));
|
}(jQuery));
|
||||||
@@ -427,6 +427,8 @@ var socket,
|
|||||||
}
|
}
|
||||||
|
|
||||||
jQuery('document').ready(function () {
|
jQuery('document').ready(function () {
|
||||||
|
translator.load('global');
|
||||||
|
|
||||||
$('#search-form').on('submit', function () {
|
$('#search-form').on('submit', function () {
|
||||||
var input = $(this).find('input');
|
var input = $(this).find('input');
|
||||||
ajaxify.go("search/" + input.val(), null, "search");
|
ajaxify.go("search/" + input.val(), null, "search");
|
||||||
|
|||||||
@@ -2,33 +2,96 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
/*global RELATIVE_PATH*/
|
/*global RELATIVE_PATH*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TODO: language en is hardcoded while system is developed.
|
||||||
|
*/
|
||||||
|
|
||||||
var translator = {},
|
var translator = {},
|
||||||
loaded = {};
|
files = {
|
||||||
|
loaded: {},
|
||||||
|
loading: {},
|
||||||
|
callbacks: {}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = translator;
|
module.exports = translator;
|
||||||
|
|
||||||
|
translator.load = function (filename, callback) {
|
||||||
translator.load = function (file, callback) {
|
if (files.loaded[filename] && !files.loading[filename]) {
|
||||||
if (loaded[file]) {
|
if (callback) {
|
||||||
callback(loaded[file]);
|
callback(files.loaded[filename]);
|
||||||
|
}
|
||||||
|
} else if (files.loading[filename]) {
|
||||||
|
if (callback) {
|
||||||
|
files.callbacks[filename] = files.callbacks[filename] || [];
|
||||||
|
files.callbacks[filename].push(callback);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
var timestamp = new Date().getTime(); //debug
|
var timestamp = new Date().getTime(); //debug
|
||||||
|
|
||||||
jQuery.getJSON(RELATIVE_PATH + '/language/en/' + file + '.json?v=' + timestamp, function (language) {
|
files.loading[filename] = true;
|
||||||
loaded[file] = language;
|
|
||||||
|
jQuery.getJSON(RELATIVE_PATH + '/language/en/' + filename + '.json?v=' + timestamp, function (language) {
|
||||||
|
files.loaded[filename] = language;
|
||||||
|
|
||||||
|
if (callback) {
|
||||||
callback(language);
|
callback(language);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (files.callbacks[filename] && files.callbacks[filename].length) {
|
||||||
|
files.callbacks[filename].pop()(language);
|
||||||
|
}
|
||||||
|
|
||||||
|
files.loading[filename] = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
translator.loadAll = function (callback) {
|
||||||
|
var utils = require('./utils.js'),
|
||||||
|
path = require('path'),
|
||||||
|
fs = require('fs');
|
||||||
|
|
||||||
|
utils.walk(path.join(__dirname, '../../', 'public/language/en'), function (err, data) {
|
||||||
|
var loaded = data.length;
|
||||||
|
|
||||||
|
for (var d in data) {
|
||||||
|
if (data.hasOwnProperty(d)) {
|
||||||
|
(function (file) {
|
||||||
|
fs.readFile(file, function (err, json) {
|
||||||
|
files.loaded[path.basename(file).replace('json', '')] = json;
|
||||||
|
|
||||||
|
loaded--;
|
||||||
|
if (loaded === 0) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}(data[d]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TODO: DRY, see translator.translate. The hard part is to make sure both work node.js / js side
|
||||||
|
*/
|
||||||
|
translator.get = function (key) {
|
||||||
|
var parsedKey = key.split(':'),
|
||||||
|
languageFile = parsedKey[0];
|
||||||
|
|
||||||
|
parsedKey = parsedKey[1];
|
||||||
|
|
||||||
|
return files.loaded[languageFile][parsedKey];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TODO: Not fully converted to server side yet, ideally server should be able to parse whole templates on demand if necessary
|
||||||
|
* fix: translator.load should determine if server side and immediately return appropriate language file.
|
||||||
|
*/
|
||||||
translator.translate = function (data, callback) {
|
translator.translate = function (data, callback) {
|
||||||
var keys = data.match(/\[\[.*?\]\]/g),
|
var keys = data.match(/\[\[.*?\]\]/g),
|
||||||
loading = 0;
|
loading = 0;
|
||||||
|
|
||||||
|
|
||||||
for (var key in keys) {
|
for (var key in keys) {
|
||||||
if (keys.hasOwnProperty(key)) {
|
if (keys.hasOwnProperty(key)) {
|
||||||
var parsedKey = keys[key].replace('[[', '').replace(']]', '').split(':'),
|
var parsedKey = keys[key].replace('[[', '').replace(']]', '').split(':'),
|
||||||
@@ -36,8 +99,8 @@
|
|||||||
|
|
||||||
parsedKey = parsedKey[1];
|
parsedKey = parsedKey[1];
|
||||||
|
|
||||||
if (loaded[languageFile]) {
|
if (files.loaded[languageFile]) {
|
||||||
data = data.replace(keys[key], loaded[file][parsedKey]);
|
data = data.replace(keys[key], files.loaded[languageFile][parsedKey]);
|
||||||
} else {
|
} else {
|
||||||
loading++;
|
loading++;
|
||||||
|
|
||||||
@@ -51,12 +114,11 @@
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
checkComplete();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkComplete() {
|
checkComplete();
|
||||||
|
|
||||||
|
function checkComplete() {
|
||||||
if (loading === 0) {
|
if (loading === 0) {
|
||||||
callback(data);
|
callback(data);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user