From 5c7da4b686ad4610a1582b964b0c9874b74a8403 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Tue, 24 Sep 2013 14:14:26 -0400 Subject: [PATCH] fixed serverside templating; footer, logout internationalization; updated more global lang keys --- app.js | 1 + public/language/en/footer.json | 12 ++++ public/language/en/global.json | 5 +- public/src/language.js | 12 ---- public/src/translator.js | 117 ++++++++++++++++----------------- public/templates/footer.tpl | 20 +++--- public/templates/logout.tpl | 8 +-- src/webserver.js | 19 ++++-- 8 files changed, 101 insertions(+), 93 deletions(-) create mode 100644 public/language/en/footer.json delete mode 100644 public/src/language.js diff --git a/app.js b/app.js index 1e46c3c667..7b9196361c 100644 --- a/app.js +++ b/app.js @@ -85,6 +85,7 @@ plugins = require('./src/plugins'); // Don't remove this - plugins initializes itself global.templates = {}; + global.translator = translator; translator.loadServer(); diff --git a/public/language/en/footer.json b/public/language/en/footer.json new file mode 100644 index 0000000000..2655ac2795 --- /dev/null +++ b/public/language/en/footer.json @@ -0,0 +1,12 @@ +{ + "disconnect.title": "Socket Disconnect", + "disconnect.message": "Looks like you disconnected, try reloading the page.", + "disconnect.reload_button": "Reload", + "chat.chatting_with": "Chat with ", + "chat.placeholder": "type chat message, here press enter to send", + "chat.send": "Send", + "stats.online": "Online", + "stats.users": "Users", + "stats.topics": "Topics", + "stats.posts": "Posts" +} \ No newline at end of file diff --git a/public/language/en/global.json b/public/language/en/global.json index bb73c3f6d0..804c3a2f21 100644 --- a/public/language/en/global.json +++ b/public/language/en/global.json @@ -4,5 +4,8 @@ "403.title": "Access Denied", "403.message": "You seem to have stumbled upon a page that you do not have access to. Perhaps you should try logging in?", "404.title": "Not Found", - "404.message": "You seem to have stumbled upon a page that does not exist. Return to the home page." + "404.message": "You seem to have stumbled upon a page that does not exist. Return to the home page.", + "logout": "Logout", + "logout.title": "You are now logged out.", + "logout.message": "You have successfully logged out of NodeBB" } \ No newline at end of file diff --git a/public/src/language.js b/public/src/language.js deleted file mode 100644 index 8a2c610169..0000000000 --- a/public/src/language.js +++ /dev/null @@ -1,12 +0,0 @@ -(function (module) { - - - - - - -})('undefined' === typeof module ? { - module: { - exports: {} - } -} : module); \ No newline at end of file diff --git a/public/src/translator.js b/public/src/translator.js index bd38862c31..8ee16f398a 100644 --- a/public/src/translator.js +++ b/public/src/translator.js @@ -17,69 +17,12 @@ files = { loaded: {}, loading: {}, - callbacks: {} + callbacks: {} // could be combined with "loading" in future. }, isServer = false; module.exports = translator; - translator.load = function (filename, callback) { - if (isServer === true) { - if (callback) { - callback(files.loaded[filename]); - } - - return files.loaded[filename]; - } - - if (files.loaded[filename] && !files.loading[filename]) { - if (callback) { - callback(files.loaded[filename]); - } - } else if (files.loading[filename]) { - if (callback) { - files.callbacks[filename] = files.callbacks[filename] || []; - files.callbacks[filename].push(callback); - } - } else { - var timestamp = new Date().getTime(); //debug - - files.loading[filename] = true; - - jQuery.getJSON(RELATIVE_PATH + '/language/en/' + filename + '.json?v=' + timestamp, function (language) { - files.loaded[filename] = language; - - if (callback) { - callback(language); - } - - while (files.callbacks[filename] && files.callbacks[filename].length) { - files.callbacks[filename].pop()(language); - } - - files.loading[filename] = false; - }); - } - }; - - translator.loadServer = function () { - isServer = true; - - 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)) { - files.loaded[path.basename(data[d]).replace('.json', '')] = require(data[d]); - } - } - }); - }; - /* * TODO: DRY, see translator.translate. The hard part is to make sure both work node.js / js side */ @@ -97,7 +40,6 @@ }); }; - /* * 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. @@ -142,6 +84,63 @@ }; + translator.load = function (filename, callback) { + if (isServer === true) { + if (callback) { + callback(files.loaded[filename]); + } + + return files.loaded[filename]; + } + + if (files.loaded[filename] && !files.loading[filename]) { + if (callback) { + callback(files.loaded[filename]); + } + } else if (files.loading[filename]) { + if (callback) { + files.callbacks[filename] = files.callbacks[filename] || []; + files.callbacks[filename].push(callback); + } + } else { + var timestamp = new Date().getTime(); //debug + + files.loading[filename] = true; + + jQuery.getJSON(RELATIVE_PATH + '/language/en/' + filename + '.json?v=' + timestamp, function (language) { + files.loaded[filename] = language; + + if (callback) { + callback(language); + } + + while (files.callbacks[filename] && files.callbacks[filename].length) { + files.callbacks[filename].pop()(language); + } + + files.loading[filename] = false; + }); + } + }; + + translator.loadServer = function () { + isServer = true; + + 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)) { + files.loaded[path.basename(data[d]).replace('.json', '')] = require(data[d]); + } + } + }); + }; + if ('undefined' !== typeof window) { window.translator = module.exports; } diff --git a/public/templates/footer.tpl b/public/templates/footer.tpl index 13fd820609..ea47c97d86 100644 --- a/public/templates/footer.tpl +++ b/public/templates/footer.tpl @@ -6,13 +6,13 @@ @@ -23,15 +23,15 @@ @@ -45,22 +45,22 @@ diff --git a/public/templates/logout.tpl b/public/templates/logout.tpl index 79391db4ce..1704a611e0 100644 --- a/public/templates/logout.tpl +++ b/public/templates/logout.tpl @@ -1,13 +1,13 @@
-

You are now logged out.

-

You have successfully logged out of NodeBB

+

[[global:logout.title]]

+

[[global:logout.message]]

diff --git a/src/webserver.js b/src/webserver.js index 6a85c3f71e..db1826da73 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -55,13 +55,9 @@ var express = require('express'), meta_tags: metaString }; - // meta.build_title(options.title, (options.req.user ? options.req.user.uid : 0), function(err, title) { - // if (!err) templateValues.browserTitle = title; - - // callback(null, templates['header'].parse(templateValues)); - // }); - - callback(null, templates['header'].parse(templateValues)); + translator.translate(templates['header'].parse(templateValues), function(template) { + callback(null, template); + }); }; // Middlewares @@ -117,6 +113,15 @@ var express = require('express'), module.exports.init = function() { templates = global.templates; + + // translate all static templates served by webserver here. ex. footer, logout + translator.translate(templates['footer'].toString(), function(parsedTemplate) { + templates['footer'] = parsedTemplate; + }); + translator.translate(templates['logout'].toString(), function(parsedTemplate) { + templates['logout'] = parsedTemplate; + }); + server.listen(nconf.get('PORT') || nconf.get('port')); }