From 1074eb8145d4790636a2bc2287e8aa5ffd5b4643 Mon Sep 17 00:00:00 2001 From: thomasbau Date: Sat, 4 May 2013 14:46:05 +0300 Subject: [PATCH 1/7] Create jq-localize initial commit --- src/jq-localize | 160 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 src/jq-localize diff --git a/src/jq-localize b/src/jq-localize new file mode 100644 index 0000000..0d61f5c --- /dev/null +++ b/src/jq-localize @@ -0,0 +1,160 @@ + + +(function() { + var $, normaliseLang; + $ = jq; + normaliseLang = function(lang) { + lang = lang.replace(/_/, '-').toLowerCase(); + if (lang.length > 3) { + lang = lang.substring(0, 3) + lang.substring(3).toUpperCase(); + } + return lang; + }; + $.defaultLanguage = normaliseLang(navigator.language || navigator.userLanguage); + + $.localize = function(pkg, options) { + var defaultCallback, + fileExtension, + intermediateLangData, + jsonCall, + lang, + loadLanguage, + notifyDelegateLanguageLoaded, + regexify, + valueForKey, + wrappedSet; + + if (options === null) { + options = {}; + } + + wrappedSet = this; + intermediateLangData = {}; + fileExtension = options.fileExtension || "json"; + + loadLanguage = function(pkg, lang, level) { + var file; + if (level === null) { + level = 1; + } + switch (level) { + case 1: + intermediateLangData = {}; + if (options.loadBase) { + file = pkg + ("." + fileExtension); + return jsonCall(file, pkg, lang, level); + } else { + return loadLanguage(pkg, lang, 2); + } + break; + case 2: + if (lang.length >= 2) { + file = "" + pkg + "-" + (lang.substring(0, 2)) + "." + fileExtension; + return jsonCall(file, pkg, lang, level); + } + break; + case 3: + if (lang.length >= 5) { + file = "" + pkg + "-" + (lang.substring(0, 5)) + "." + fileExtension; + return jsonCall(file, pkg, lang, level); + } + } + }; + + jsonCall = function(file, pkg, lang, level) { + var ajaxOptions, successFunc; + if (typeof options.pathPrefix !== 'undefined') { + file = "" + options.pathPrefix + "/" + file; + } + successFunc = function(d) { + notifyDelegateLanguageLoaded($.parseJSON(d)); + return loadLanguage(pkg, lang, level + 1); + }; + + ajaxOptions = { + url: file, + crossDomain: true, + type: "GET", + success: successFunc + }; + $.ajax(ajaxOptions); + }; + + + defaultCallback = function(data) { + $.localize.data[pkg] = data; + return wrappedSet.each(function() { + var elem, key, value; + elem = $(this); + key = elem.attr("localize-data").match(/localize\[(.*?)\]/)[1]; + value = valueForKey(key, data); + + var type = elem[0].nodeName; + if (type === 'INPUT') { + if (elem.attr("placeholder")) { + return elem.attr("placeholder", value); + } else { + return elem.val(value); + } + } else if (elem.is('img')) { + value = valueForKey("" + key + ".alt", data); + if (value != null) { + elem.attr("alt", value); + } + value = valueForKey("" + key + ".src", data); + if (value != null) { + return elem.attr("src", value); + } + } else { + return elem.html(value); + } + }); + }; + + notifyDelegateLanguageLoaded = function(data) { + if (options.callback != null) { + return options.callback(data, defaultCallback); + } else { + return defaultCallback(data); + } + }; + + valueForKey = function(key, data) { + var keys, value, _i, _len; + keys = key.split(/\./); + value = data; + for (_i = 0, _len = keys.length; _i < _len; _i++) { + key = keys[_i]; + value = value != null ? value[key] : null; + } + return value; + }; + + regexify = function(string_or_regex_or_array) { + var thing; + if (typeof string_or_regex_or_array === "string") { + return "^" + string_or_regex_or_array + "$"; + } else if (string_or_regex_or_array.length != null) { + return ((function() { + var _i, _len, _results; + _results = []; + for (_i = 0, _len = string_or_regex_or_array.length; _i < _len; _i++) { + thing = string_or_regex_or_array[_i]; + _results.push(regexify(thing)); + } + return _results; + })()).join("|"); + } else { + return string_or_regex_or_array; + } + }; + lang = normaliseLang(options.language ? options.language : $.defaultLanguage); + if (!(options.skipLanguage && lang.match(regexify(options.skipLanguage)))) { + loadLanguage(pkg, lang, 1); + } + return wrappedSet; + }; + + $.fn.localize = $.localize; + $.localize.data = {}; +}).call(this); From cf889aabadf24a329942d518929ad0647e9874c0 Mon Sep 17 00:00:00 2001 From: thomasbau Date: Sat, 4 May 2013 14:49:06 +0300 Subject: [PATCH 2/7] Update jq-localize --- src/jq-localize | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/jq-localize b/src/jq-localize index 0d61f5c..fddded0 100644 --- a/src/jq-localize +++ b/src/jq-localize @@ -1,3 +1,9 @@ +# Copyright (c) MobComp GmbH (http://www.mobcomp.ch), 2013. +# Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. +# Written by Thomas Baumann (tbaumann@mobcomp.ch) for use on mc-time web app. +# http://github.com/coderifous/jquery-localize +# Based off of Keith Wood's Localisation jQuery plugin. +# http://keith-wood.name/localisation.html (function() { From c6b97024622347946e4e9888ba97e1a333820e0d Mon Sep 17 00:00:00 2001 From: thomasbau Date: Sat, 4 May 2013 14:51:09 +0300 Subject: [PATCH 3/7] Update jq-localize --- src/jq-localize | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/jq-localize b/src/jq-localize index fddded0..56bc3f0 100644 --- a/src/jq-localize +++ b/src/jq-localize @@ -1,9 +1,10 @@ # Copyright (c) MobComp GmbH (http://www.mobcomp.ch), 2013. # Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. # Written by Thomas Baumann (tbaumann@mobcomp.ch) for use on mc-time web app. -# http://github.com/coderifous/jquery-localize +# http://github.com/thomasbau/jquery-localize/src/jq-localize # Based off of Keith Wood's Localisation jQuery plugin. # http://keith-wood.name/localisation.html +# and Jim Garvin (http://github.com/coderifous) (function() { From 30c52500b0c1abcc4e16b621f9062bf3c492c77f Mon Sep 17 00:00:00 2001 From: thomasbau Date: Sat, 4 May 2013 15:20:11 +0300 Subject: [PATCH 4/7] Update README.markdown --- README.markdown | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.markdown b/README.markdown index e31065c..d4c384a 100644 --- a/README.markdown +++ b/README.markdown @@ -1,3 +1,43 @@ +# jq-localize.js + +## jqMobi plugin enabling localization for mobile devices. + +## Usage: +Add to any html element you want to localize following attribute: + +localize-data="localize[json.value]" + +## Example: +given a json file named: + main_language-fr.json + +in your base directory + localize/ + +with following content: + { + "maintext":{ + "hello": "Bonjour" + } + } + +add following line between script tag to your html header: + $("[localize-data*=localize]").localize("localize/main_language", {skipLanguage: /^en/, loadBase: false}); + +any tag containing: + localize-data="localize[maintext.hello]" + +like: +

Hello

+ +will have his text changed to "Bonjour" if the browser language is discovered to "fr" + +## In use with Intel XDK & appMobi +jqMobi is using asynchronous Ajax method to load any file. Thus loading the json file before +AppMobi.device is being instantiated, will result in an empty result value and no localization will be possible. + +
+ # jquery.localize.js ## a jQuery plugin that makes it easy to i18n your static web site. From cd283ee0ffbed8ea2572c96f54679fdc1d45b7bc Mon Sep 17 00:00:00 2001 From: thomasbau Date: Sat, 4 May 2013 15:25:47 +0300 Subject: [PATCH 5/7] Update README.markdown --- README.markdown | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index d4c384a..b6daf58 100644 --- a/README.markdown +++ b/README.markdown @@ -22,15 +22,20 @@ with following content: } add following line between script tag to your html header: + $("[localize-data*=localize]").localize("localize/main_language", {skipLanguage: /^en/, loadBase: false}); any tag containing: + localize-data="localize[maintext.hello]" like: +

Hello

-will have his text changed to "Bonjour" if the browser language is discovered to "fr" +will have his text changed to "Bonjour" if the browser language is discovered to + + "fr" ## In use with Intel XDK & appMobi jqMobi is using asynchronous Ajax method to load any file. Thus loading the json file before From b339161ed38a825cd3e94a46b13f4bef2c63fd73 Mon Sep 17 00:00:00 2001 From: thomasbau Date: Sat, 4 May 2013 15:27:52 +0300 Subject: [PATCH 6/7] Update README.markdown --- README.markdown | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index b6daf58..a48ea2f 100644 --- a/README.markdown +++ b/README.markdown @@ -33,7 +33,7 @@ like:

Hello

-will have his text changed to "Bonjour" if the browser language is discovered to +will have it's text changed to "Bonjour" if the browser language is discovered to "fr" @@ -41,6 +41,9 @@ will have his text changed to "Bonjour" if the browser language is discovered to jqMobi is using asynchronous Ajax method to load any file. Thus loading the json file before AppMobi.device is being instantiated, will result in an empty result value and no localization will be possible. + + +
# jquery.localize.js From 7ca9501e274743f1e2a97830c8d1103d02289be9 Mon Sep 17 00:00:00 2001 From: thomasbau Date: Sat, 4 May 2013 15:37:51 +0300 Subject: [PATCH 7/7] Update README.markdown --- README.markdown | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/README.markdown b/README.markdown index a48ea2f..a1b0747 100644 --- a/README.markdown +++ b/README.markdown @@ -9,9 +9,11 @@ localize-data="localize[json.value]" ## Example: given a json file named: + main_language-fr.json -in your base directory +in your base directory named + localize/ with following content: @@ -22,7 +24,11 @@ with following content: } add following line between script tag to your html header: - + + /* + * Search in directory "localize" + * the file starting with "main_language" + * if the browser language is english, skip $("[localize-data*=localize]").localize("localize/main_language", {skipLanguage: /^en/, loadBase: false}); any tag containing: @@ -39,7 +45,21 @@ will have it's text changed to "Bonjour" if the browser language is discovered t ## In use with Intel XDK & appMobi jqMobi is using asynchronous Ajax method to load any file. Thus loading the json file before -AppMobi.device is being instantiated, will result in an empty result value and no localization will be possible. +AppMobi.device is being instantiated, will result in an empty value and no localization will be possible. + +Possible solution + + /* This code is used for appMobi native apps */ + var onDeviceReady = function() { + AppMobi.device.setRotateOrientation("portrait"); + AppMobi.device.setAutoRotate(false); + webRoot = AppMobi.webRoot + "/"; + + // Now the device should be ready + $("[localize-data*=localize]").localize("localize/main_language", {skipLanguage: /^en/, loadBase: false}); + + }; + document.addEventListener("appMobi.device.ready", onDeviceReady, false);