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 <span id='chat-with-name'></span>", + "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 <a href='/login'>try logging in</a>?", "404.title": "Not Found", - "404.message": "You seem to have stumbled upon a page that does not exist. Return to the <a href='/''>home page</a>." + "404.message": "You seem to have stumbled upon a page that does not exist. Return to the <a href='/''>home page</a>.", + "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 @@ <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> - <h3 id="myModalLabel">Socket Disconnect</h3> + <h3 id="myModalLabel">[[footer:disconnect.title]]</h3> </div> <div class="modal-body"> - <span id="disconnect-text">Looks like you disconnected, try reloading the page.</span> + <span id="disconnect-text">[[footer:disconnect.message]]</span> </div> <div class="modal-footer"> - <a id="reload-button" href="/" class="btn btn-primary">Reload</a> + <a id="reload-button" href="/" class="btn btn-primary">[[footer:disconnect.reload_button]]</a> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> @@ -23,15 +23,15 @@ <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> - <h3 id="myModalLabel">Chat with <span id="chat-with-name"></span></h3> + <h3 id="myModalLabel">[[footer:chat.chatting_with]]</h3> </div> <div class="modal-body"> <textarea class="form-control" id="chat-content" cols="40" rows="10" readonly></textarea><br/> - <input id="chat-message-input" type="text" class="form-control" name="chat-message" placeholder="type chat message, here press enter to send"/> + <input id="chat-message-input" type="text" class="form-control" name="chat-message" placeholder="[[footer:chat.placeholder]]"/> </div> <div class="modal-footer"> <button type="button" id="chat-message-send-btn" href="#" class="btn btn-primary btn-lg btn-block - ">Send</button> + ">[[footer:chat.send]]</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> @@ -45,22 +45,22 @@ <div class="row footer-stats"> <div class="col-md-3 col-xs-6"> <div class="stats-card well"> - <h2><span id="stats_online"></span><br /><small>Online</small></h2> + <h2><span id="stats_online"></span><br /><small>[[footer:stats.online]]</small></h2> </div> </div> <div class="col-md-3 col-xs-6"> <div class="stats-card well"> - <h2><span id="stats_users"></span><br /><small>Users</small></h2> + <h2><span id="stats_users"></span><br /><small>[[footer:stats.users]]</small></h2> </div> </div> <div class="col-md-3 col-xs-6"> <div class="stats-card well"> - <h2><span id="stats_topics"></span><br /><small>Topics</small></h2> + <h2><span id="stats_topics"></span><br /><small>[[footer:stats.topics]]</small></h2> </div> </div> <div class="col-md-3 col-xs-6"> <div class="stats-card well"> - <h2><span id="stats_posts"></span><br /><small>Posts</small></h2> + <h2><span id="stats_posts"></span><br /><small>[[footer:stats.posts]]</small></h2> </div> </div> </div> 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 @@ <ol class="breadcrumb"> <li itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"> - <a href="/" itemprop="url"><span itemprop="title">Home</span></a> + <a href="/" itemprop="url"><span itemprop="title">[[global:home]]</span></a> </li> <li class="active" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"> - <span itemprop="title">Logout</span> + <span itemprop="title">[[global:logout]]</span> </li> </ol> <div class="alert alert-success" id="message"> - <h4>You are now logged out.</h4> - <p>You have successfully logged out of NodeBB</p> + <h4>[[global:logout.title]]</h4> + <p>[[global:logout.message]]</p> </div> 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')); }