From b5274a0d9117fb07bf69c5f91d3a2eef28650e08 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 20 Sep 2013 14:35:32 -0400 Subject: [PATCH] init --- public/language/TODO | 2 + public/language/en/global.json | 3 ++ public/language/en/login.json | 8 ++++ public/src/language.js | 12 ++++++ public/src/templates.js | 77 ++++++++++++++++++---------------- public/src/translator.js | 75 +++++++++++++++++++++++++++++++++ public/templates/header.tpl | 1 + public/templates/login.tpl | 20 ++++----- 8 files changed, 151 insertions(+), 47 deletions(-) create mode 100644 public/language/TODO create mode 100644 public/language/en/global.json create mode 100644 public/language/en/login.json create mode 100644 public/src/language.js create mode 100644 public/src/translator.js diff --git a/public/language/TODO b/public/language/TODO new file mode 100644 index 0000000000..2f9c2d6598 --- /dev/null +++ b/public/language/TODO @@ -0,0 +1,2 @@ +For now, language packs will be stored here. Eventually, will be moved to server side to allow for npm installability. +When that happens, server code will generate compressed JSON language files in this folder. \ No newline at end of file diff --git a/public/language/en/global.json b/public/language/en/global.json new file mode 100644 index 0000000000..618afaaf2d --- /dev/null +++ b/public/language/en/global.json @@ -0,0 +1,3 @@ +{ + "home": "Home" +} \ No newline at end of file diff --git a/public/language/en/login.json b/public/language/en/login.json new file mode 100644 index 0000000000..f63e708750 --- /dev/null +++ b/public/language/en/login.json @@ -0,0 +1,8 @@ +{ + "login": "Login", + "username": "Username", + "password": "Password", + "remember_me": "Remember Me?", + "forgot_password": "Forgot Password?", + "alternative_logins": "Alternative Logins" +} \ No newline at end of file diff --git a/public/src/language.js b/public/src/language.js new file mode 100644 index 0000000000..8a2c610169 --- /dev/null +++ b/public/src/language.js @@ -0,0 +1,12 @@ +(function (module) { + + + + + + +})('undefined' === typeof module ? { + module: { + exports: {} + } +} : module); \ No newline at end of file diff --git a/public/src/templates.js b/public/src/templates.js index af6918935e..0528ce67dd 100644 --- a/public/src/templates.js +++ b/public/src/templates.js @@ -1,4 +1,4 @@ -(function(module) { +(function (module) { var config = {}, templates, @@ -12,11 +12,11 @@ fs = require('fs'); } catch (e) {} - templates.force_refresh = function(tpl) { + templates.force_refresh = function (tpl) { return !!config.force_refresh[tpl]; } - templates.get_custom_map = function(tpl) { + templates.get_custom_map = function (tpl) { if (config['custom_mapping'] && tpl) { for (var pattern in config['custom_mapping']) { if (tpl.match(pattern)) { @@ -28,11 +28,11 @@ return false; } - templates.is_available = function(tpl) { + templates.is_available = function (tpl) { return jQuery.inArray(tpl, available_templates) !== -1; }; - templates.ready = function(callback) { + templates.ready = function (callback) { if (callback == null) { if (this.ready_callback) { this.ready_callback(); @@ -48,7 +48,7 @@ } }; - templates.prepare = function(raw_tpl, data) { + templates.prepare = function (raw_tpl, data) { var template = {}; template.html = raw_tpl; template.parse = parse; @@ -62,10 +62,10 @@ var loaded = templatesToLoad.length; for (var t in templatesToLoad) { - (function(file) { - fs.readFile(__dirname + '/../templates/' + file + '.tpl', function(err, html) { - var template = function() { - this.toString = function() { + (function (file) { + fs.readFile(__dirname + '/../templates/' + file + '.tpl', function (err, html) { + var template = function () { + this.toString = function () { return this.html; }; } @@ -84,7 +84,7 @@ } function loadClient() { - jQuery.when(jQuery.getJSON(RELATIVE_PATH + '/templates/config.json'), jQuery.getJSON(RELATIVE_PATH + '/api/get_templates_listing')).done(function(config_data, templates_data) { + jQuery.when(jQuery.getJSON(RELATIVE_PATH + '/templates/config.json'), jQuery.getJSON(RELATIVE_PATH + '/api/get_templates_listing')).done(function (config_data, templates_data) { config = config_data[0]; available_templates = templates_data[0]; templates.ready(); @@ -96,11 +96,11 @@ } - templates.init = function(templates_to_load) { + templates.init = function (templates_to_load) { loadTemplates(templates_to_load || []); } - templates.getTemplateNameFromUrl = function(url) { + templates.getTemplateNameFromUrl = function (url) { var parts = url.split('?')[0].split('/'); for (var i = 0; i < parts.length; ++i) { @@ -112,7 +112,7 @@ } - templates.load_template = function(callback, url, template) { + templates.load_template = function (callback, url, template) { var location = document.location || window.location, rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : ''); @@ -129,13 +129,13 @@ var template_data = null; - (function() { + (function () { var timestamp = new Date().getTime(); //debug if (!templates[tpl_url]) { - jQuery.get(RELATIVE_PATH + '/templates/' + tpl_url + '.tpl?v=' + timestamp, function(html) { - var template = function() { - this.toString = function() { + jQuery.get(RELATIVE_PATH + '/templates/' + tpl_url + '.tpl?v=' + timestamp, function (html) { + var template = function () { + this.toString = function () { return this.html; }; } @@ -154,9 +154,9 @@ }()); - (function() { + (function () { - jQuery.get(API_URL + api_url, function(data) { + jQuery.get(API_URL + api_url, function (data) { if (!data) { ajaxify.go('404'); @@ -165,7 +165,7 @@ template_data = data; parse_template(); - }).fail(function(data) { + }).fail(function (data) { template_data = {}; parse_template(); }); @@ -180,22 +180,25 @@ else template_data['relative_path'] = RELATIVE_PATH; - document.getElementById('content').innerHTML = templates[tpl_url].parse(template_data); - jQuery('#content [template-variable]').each(function(index, element) { + translator.translate(templates[tpl_url].parse(template_data), function (translatedTemplate) { + document.getElementById('content').innerHTML = translatedTemplate; + }); + + jQuery('#content [template-variable]').each(function (index, element) { var value = null; switch (element.getAttribute('template-type')) { - case 'boolean': - value = (element.value === 'true' || element.value === '1') ? true : false; - break; - case 'int': // Intentional fall-through - case 'integer': - value = parseInt(element.value); - break; - default: - value = element.value; - break; + case 'boolean': + value = (element.value === 'true' || element.value === '1') ? true : false; + break; + case 'int': // Intentional fall-through + case 'integer': + value = parseInt(element.value); + break; + default: + value = element.value; + break; } templates.set(element.getAttribute('template-variable'), value); @@ -208,20 +211,20 @@ } - templates.flush = function() { + templates.flush = function () { parsed_variables = {}; } - templates.get = function(key) { + templates.get = function (key) { return parsed_variables[key]; } - templates.set = function(key, value) { + templates.set = function (key, value) { parsed_variables[key] = value; } //modified from https://github.com/psychobunny/dcp.templates - var parse = function(data) { + var parse = function (data) { var self = this; function replace(key, value, template) { diff --git a/public/src/translator.js b/public/src/translator.js new file mode 100644 index 0000000000..b94b846c12 --- /dev/null +++ b/public/src/translator.js @@ -0,0 +1,75 @@ +(function (module) { + "use strict"; + /*global RELATIVE_PATH*/ + + var translator = {}, + loaded = {}; + + module.exports = translator; + + + translator.load = function (file, callback) { + if (loaded[file]) { + callback(loaded[file]); + } else { + var timestamp = new Date().getTime(); //debug + + jQuery.getJSON(RELATIVE_PATH + '/language/en/' + file + '.json?v=' + timestamp, function (language) { + loaded[file] = language; + callback(language); + }); + } + }; + + + + + translator.translate = function (data, callback) { + var keys = data.match(/\[\[.*?\]\]/g), + loading = 0; + + + for (var key in keys) { + if (keys.hasOwnProperty(key)) { + var parsedKey = keys[key].replace('[[', '').replace(']]', '').split(':'), + languageFile = parsedKey[0]; + + parsedKey = parsedKey[1]; + + if (loaded[languageFile]) { + data = data.replace(keys[key], loaded[file][parsedKey]); + } else { + loading++; + + (function (languageKey, parsedKey) { + translator.load(languageFile, function (languageData) { + data = data.replace(languageKey, languageData[parsedKey]); + loading--; + checkComplete(); + }); + }(keys[key], parsedKey)); + + } + } + + checkComplete(); + } + + function checkComplete() { + + if (loading === 0) { + callback(data); + } + } + + }; + + if ('undefined' !== typeof window) { + window.translator = module.exports; + } + +})('undefined' === typeof module ? { + module: { + exports: {} + } +} : module); \ No newline at end of file diff --git a/public/templates/header.tpl b/public/templates/header.tpl index 0efb66394d..a364ddcbd9 100644 --- a/public/templates/header.tpl +++ b/public/templates/header.tpl @@ -24,6 +24,7 @@ + diff --git a/public/templates/login.tpl b/public/templates/login.tpl index 4298194ee5..11d0b86566 100644 --- a/public/templates/login.tpl +++ b/public/templates/login.tpl @@ -1,9 +1,9 @@ @@ -13,18 +13,18 @@
- +
- +
- +
@@ -33,14 +33,14 @@
-   Forgot Password? +   [[login:forgot_password]]
@@ -48,10 +48,10 @@
- +
-

Alternative Logins

+

[[login:alternative_logins]]