From b5274a0d9117fb07bf69c5f91d3a2eef28650e08 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 20 Sep 2013 14:35:32 -0400 Subject: [PATCH 01/18] 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]]

- +
From 35b40ef650c64e935944f46ba5d453f501c4e370 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 20 Sep 2013 17:03:30 -0400 Subject: [PATCH 07/18] internationalization: unread.tpl --- public/language/en/unread.json | 5 +++++ public/templates/unread.tpl | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 public/language/en/unread.json diff --git a/public/language/en/unread.json b/public/language/en/unread.json new file mode 100644 index 0000000000..f8d5ccfbc3 --- /dev/null +++ b/public/language/en/unread.json @@ -0,0 +1,5 @@ +{ + "no_unread_topics": "There are no unread topics.", + "mark_all_read": "Mark all as Read", + "load_more": "Load More" +} \ No newline at end of file diff --git a/public/templates/unread.tpl b/public/templates/unread.tpl index e44f479982..4131be1d1f 100644 --- a/public/templates/unread.tpl +++ b/public/templates/unread.tpl @@ -1,6 +1,6 @@
@@ -10,10 +10,10 @@
- There are no unread topics. + [[unread:no_unread_topics]]
- +
@@ -45,7 +45,7 @@ - +
From 5e89caf358cac0f83ee65645e62987c61fd238d7 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 20 Sep 2013 17:16:01 -0400 Subject: [PATCH 08/18] internationalization: topic.json --- public/language/en/global.json | 3 +- public/language/en/topic.json | 17 +++++++++++ public/src/translator.js | 4 +++ public/templates/topic.tpl | 54 +++++++++++++++++----------------- 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/public/language/en/global.json b/public/language/en/global.json index 618afaaf2d..6acd249cc6 100644 --- a/public/language/en/global.json +++ b/public/language/en/global.json @@ -1,3 +1,4 @@ { - "home": "Home" + "home": "Home", + "buttons.close": "Close" } \ No newline at end of file diff --git a/public/language/en/topic.json b/public/language/en/topic.json index d83049ef5c..a00c1e6f60 100644 --- a/public/language/en/topic.json +++ b/public/language/en/topic.json @@ -1,4 +1,21 @@ { + "profile": "Profile", + "chat": "Chat", + "notify_me": "Be notified of new replies in this topic", + "favourite": "Favourite", + "quote": "Quote", + "reply": "Reply", + "edit": "Edit", + "delete": "Delete", + "banned": "banned", + "link": "Link", + "thread_tools.title": "Thread Tools", + "thread_tools.pin": "Pin Thread", + "thread_tools.lock": "Lock Thread", + "thread_tools.move": "Move Thread", + "thread_tools.delete": "Delete Thread", + "load_categories": "Loading Categories", + "confirm_move": "Move", "favourites.not_logged_in.title": "Not Logged In", "favourites.not_logged_in.message": "Please log in in order to favourite this post" } \ No newline at end of file diff --git a/public/src/translator.js b/public/src/translator.js index 17d0cb030d..4e8bec7fdd 100644 --- a/public/src/translator.js +++ b/public/src/translator.js @@ -6,7 +6,11 @@ * TODO: * * 1. language en is hardcoded while system is developed. + * b. need to write fallback system to default language * 2. recursion needed when parsing language keys (ex. topics:modal.delete.title), right now json is all one level deep + * 3. server side settings for default language + * 4. user side settings for preferred language + * */ var translator = {}, diff --git a/public/templates/topic.tpl b/public/templates/topic.tpl index 2a5b329056..307e8fe11e 100644 --- a/public/templates/topic.tpl +++ b/public/templates/topic.tpl @@ -1,7 +1,7 @@
- - + +
- - - + + +
@@ -82,7 +82,7 @@ - banned + [[topic:banned]]
@@ -94,26 +94,26 @@
- - + +
- - - + + +
@@ -142,16 +142,16 @@
@@ -175,7 +175,7 @@

Move Thread

From 5a10f7fcfe262ea31fdabbbc4472655ff588024a Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 20 Sep 2013 17:31:53 -0400 Subject: [PATCH 09/18] init variables in translator --- public/src/translator.js | 2 ++ public/templates/topic.tpl | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/public/src/translator.js b/public/src/translator.js index 4e8bec7fdd..bd38862c31 100644 --- a/public/src/translator.js +++ b/public/src/translator.js @@ -108,6 +108,8 @@ for (var key in keys) { if (keys.hasOwnProperty(key)) { + //check for additional variables then keys[key].split(/[,][?\s+]/); + var parsedKey = keys[key].replace('[[', '').replace(']]', '').split(':'), languageFile = parsedKey[0]; diff --git a/public/templates/topic.tpl b/public/templates/topic.tpl index 307e8fe11e..e1a13d95e6 100644 --- a/public/templates/topic.tpl +++ b/public/templates/topic.tpl @@ -27,7 +27,7 @@
- From c11920ac088afaf06f62befbc7345e872af29d5e Mon Sep 17 00:00:00 2001 From: psychobunny Date: Tue, 24 Sep 2013 13:22:10 -0400 Subject: [PATCH 10/18] internationalization: 404 and 403 --- public/language/en/global.json | 6 +++++- public/templates/403.tpl | 4 ++-- public/templates/404.tpl | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/public/language/en/global.json b/public/language/en/global.json index 6acd249cc6..bb73c3f6d0 100644 --- a/public/language/en/global.json +++ b/public/language/en/global.json @@ -1,4 +1,8 @@ { "home": "Home", - "buttons.close": "Close" + "buttons.close": "Close", + "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." } \ No newline at end of file diff --git a/public/templates/403.tpl b/public/templates/403.tpl index 62b460deb3..46478a224c 100644 --- a/public/templates/403.tpl +++ b/public/templates/403.tpl @@ -1,4 +1,4 @@
- Access Denied -

You seem to have stumbled upon a page that you do not have access to. Perhaps you should try logging in?

+ [[global:403.title]] +

[[global:403.message]]

\ No newline at end of file diff --git a/public/templates/404.tpl b/public/templates/404.tpl index 1825280f6f..95cd1b9383 100644 --- a/public/templates/404.tpl +++ b/public/templates/404.tpl @@ -1,4 +1,4 @@
- Not found -

You seem to have stumbled upon a page that does not exist. Return to the home page

+ [[global:404.title]] +

[[global:404.message]]

\ No newline at end of file From 2d8e6bd98019246f450157739e4da275ecb75b99 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Tue, 24 Sep 2013 13:31:44 -0400 Subject: [PATCH 11/18] category language --- public/language/en/category.json | 7 +++++++ public/templates/category.tpl | 13 ++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 public/language/en/category.json diff --git a/public/language/en/category.json b/public/language/en/category.json new file mode 100644 index 0000000000..73d3bef853 --- /dev/null +++ b/public/language/en/category.json @@ -0,0 +1,7 @@ +{ + "new_topic_button": "New Topic", + "no_topics": "There are no topics in this category.
Why don't you try posting one?", + "sidebar.recent_replies": "Recent Replies", + "sidebar.active_participants": "Active Participants", + "sidebar.moderators": "Moderators" +} \ No newline at end of file diff --git a/public/templates/category.tpl b/public/templates/category.tpl index 0a6a5f34f8..bbfd941de8 100644 --- a/public/templates/category.tpl +++ b/public/templates/category.tpl @@ -1,6 +1,6 @@
- +
  @@ -21,8 +21,7 @@
- There are no topics in this category.
- Why don't you try posting one? + [[category:no_topics]]
@@ -59,7 +58,7 @@