Permalink
Browse files

updated cookie support in client

  • Loading branch information...
1 parent 95fadb0 commit 669c7c2a33b81adb133fdc4cb1e07c759bc943e4 @jamuhl jamuhl committed Feb 2, 2012
@@ -1,2 +0,0 @@
-
-(function(){function j(a,c){typeof a=="function"&&(c=a,a={}),a=a||{},e.extend(d,a),typeof d.ns=="string"&&(d.ns={namespaces:[d.ns],defaultNs:d.ns}),d.lng||(d.lng=e.detectLanguage()),i=[];if(d.lng.indexOf("-")===2&&d.lng.length===5){var h=d.lng.split("-");d.lng=d.lowerCaseLng?h[0].toLowerCase()+"-"+h[1].toLowerCase():h[0].toLowerCase()+"-"+h[1].toUpperCase(),i.push(d.lng),i.push(d.lng.substr(0,2))}else i.push(d.lng);i.indexOf(d.fallbackLng)===-1&&i.push(d.fallbackLng),g=d.lng;if(d.resStore){f=d.resStore,b&&d.setJqueryExt&&l(),c&&c(q);return}t.load(i,d.ns,d.useLocalStorage,d.dynamicLoad,function(a,e){f=e,b&&d.setJqueryExt&&l(),c&&c(q)})}function k(a,b){j({lng:a},b)}function l(){function a(a,c){if(c.length===0)return;var d="text";if(c.indexOf("[")===0){var e=c.split("]");c=e[1],d=e[0].substr(1,e[0].length-1)}c.indexOf(";")===c.length-1&&(c=c.substr(0,c.length-2)),d==="text"?a.text(b.t(c,{defaultValue:a.text()})):a.attr(d,b.t(c,{defaultValue:a.attr(d)}))}b.t=b.t||q,b.fn.i18n=function(c){return this.each(function(){var c=b(this).find("[data-i18n]");c.each(function(){var c=b(this),d=c.attr("data-i18n");if(d.indexOf(";")<=d.length-1){var e=d.split(";");b.each(e,function(b,d){a(c,d)})}else a(c,d)})})}}function m(a,b){return e.each(b,function(b,c){a=a.replace([d.interpolationPrefix,b,d.interpolationSuffix].join(""),c)}),a}function n(a,b){while(a.indexOf(d.reusePrefix)!=-1){h++;if(h>d.maxRecursion)break;var c=a.indexOf(d.reusePrefix),e=a.indexOf(d.reuseSuffix,c)+d.reuseSuffix.length,f=a.substring(c,e),g=f.replace(d.reusePrefix,"").replace(d.reuseSuffix,""),i=r(g,b);a=a.replace(f,i)}return a}function o(a){return a.context&&typeof a.context=="string"}function p(a){return a.count!==undefined&&typeof a.count!="string"&&a.count!==1}function q(a,b){return h=0,r(a,b)}function r(a,b){b=b||{};var c,h,j=b.defaultValue||a;if(!f)return j;var k=d.ns.defaultNs;if(a.indexOf(d.nsseparator)>-1){var l=a.split(d.nsseparator);k=l[0],a=l[1]}if(o(b)){c=e.extend({},b),delete c.context,c.defaultValue=d.contextNotFound;var r=a+"_"+b.context;h=q(r,c);if(h!=d.contextNotFound)return m(h,{context:b.context})}if(p(b)){c=e.extend({},b),delete c.count,c.defaultValue=d.pluralNotFound;var s=a+d.pluralSuffix,v=u.get(g,b.count);v!=="other"&&(s=s+"_"+v),h=q(s,c);if(h!=d.pluralNotFound)return m(h,{count:b.count})}var w;for(var x=0,y=i.length;x<y;x++){if(w)break;var z=i[x],A=a.split(d.keyseparator),B=0,C=f[z][k];while(A[B])C=C&&C[A[B]],B++;C&&(C=m(C,b),C=n(C,b),w=C)}return!w&&d.sendMissing&&t.postMissing(k,a,j),w?w:j}function s(){var a,b=[];(function(){var a=window.location.search.substring(1),c=a.split("&");for(var d=0;d<c.length;d++){var e=c[d].indexOf("=");if(e>0){var f=c[d].substring(0,e),g=c[d].substring(e+1);b[f]=g}}})(),b.setLng&&(a=b.setLng,w.read("i18next")||w.create("i18next",a));if(!a){var c=w.read("i18next");c&&(a=c)}return!a&&navigator?a=navigator.language?navigator.language:navigator.userLanguage:a=d.fallbackLng,a}function v(){return g}var a=this,b=a.jQuery,c={};typeof module!="undefined"&&module.exports?module.exports=c:b?b.i18n=b.i18n||c:a.i18n=a.i18n||c;var d={lng:!1,lowerCaseLng:!1,fallbackLng:"dev",ns:"translation",nsseparator:":",keyseparator:".",resGetPath:"locales/__lng__/__ns__.json",resPostPath:"locales/add/__lng__/__ns__",resStore:!1,useLocalStorage:!0,dynamicLoad:!1,sendMissing:!1,interpolationPrefix:"__",interpolationSuffix:"__",reusePrefix:"$t(",reuseSuffix:")",pluralSuffix:"_plural",pluralNotFound:["plural_not_found",Math.random()].join(""),pcontextNotFound:["context_not_found",Math.random()].join(""),setJqueryExt:!0},e={extend:b?b.extend:undefined,each:b?b.each:undefined,ajax:b?b.ajax:undefined,detectLanguage:s},f=!1,g=!1,h=0,i=[],t={load:function(a,b,c,d,f){c?t._loadLocal(a,function(c,g){var h=[];for(var i=0,j=a.length;i<j;i++)g[a[i]]||h.push(a[i]);h.length>0?t._fetch(h,b,d,function(a,b){e.extend(g,b),t._storeLocal(b),f(null,g)}):f(null,g)}):t._fetch(a,b,d,function(a,b){f(null,b)})},_loadLocal:function(a,b){var c={};if(window.localStorage){var d=a.length;e.each(a,function(a,e){var f=window.localStorage.getItem("res_"+e);f&&(c[e]=JSON.parse(f)),d--,d===0&&b(null,c)})}},_storeLocal:function(a){if(window.localStorage)for(var b in a)window.localStorage.setItem("res_"+b,JSON.stringify(a[b]));return},_fetch:function(a,b,c,f){var g={};if(!c){var h=b.namespaces.length*a.length;e.each(b.namespaces,function(b,c){e.each(a,function(a,b){t._fetchOne(b,c,function(a,d){g[b]=g[b]||{},g[b][c]=d,h--,h===0&&f(null,g)})})})}else e.ajax({url:m(d.resGetPath,{lng:a.join("+"),ns:b.namespaces.join("+")}),success:function(a,b,c){f(null,a)},error:function(a,b,c){f("failed loading resource.json error: "+c)},dataType:"json"})},_fetchOne:function(a,b,c){e.ajax({url:m(d.resGetPath,{lng:a,ns:b}),success:function(a,b,d){c(null,a)},error:function(a,b,d){c(d,{})},dataType:"json"})},postMissing:function(a,b,c){var g={};g[b]=c,e.ajax({url:m(d.resPostPath,{lng:d.fallbackLng,ns:a}),type:"POST",data:g,success:function(e,g,h){f[d.fallbackLng][a][b]=c},error:function(a,b,c){},dataType:"json"})}},u={rules:{sl:function(a){return a%100===1?"one":a%100===2?"two":a%100===3||a%100===4?"few":"other"},ar:function(a){return a===0?"zero":a===1?"one":a===2?"two":a%100>=3&&a%100<=10?"few":a%100>=11&&a%100<=99?"many":"other"}},addRule:function(a,b){u.rules[a]=b},get:function(a,b){function d(a,b){return u.rules[a]?u.rules[a](b):b===1?"one":"other"}var c=a.split("-");return c.length===2?d(c[0],b):d(a,b)}},w={create:function(a,b,c){var d;if(c){var e=new Date;e.setTime(e.getTime()+c*60*1e3),d="; expires="+e.toGMTString()}else d="";document.cookie=a+"="+b+d+"; path=/"},read:function(a){var b=a+"=",c=document.cookie.split(";");for(var d=0;d<c.length;d++){var e=c[d];while(e.charAt(0)==" ")e=e.substring(1,e.length);if(e.indexOf(b)===0)return e.substring(b.length,e.length)}return null},remove:function(a){this.create(a,"",-1)}};c.init=j,c.setLng=k,c.t=q,c.translate=q,c.detectLanguage=e.detectLanguage,c.pluralExtensions=u,c.sync=t,c.functions=e,c.lng=v,c.options=d})()
@@ -51,7 +51,34 @@
extend: $ ? $.extend : undefined,
each: $ ? $.each : undefined,
ajax: $ ? $.ajax : undefined,
- detectLanguage: detectLanguage
+ detectLanguage: detectLanguage,
+ cookie: {
+ create: function(name,value,minutes) {
+ var expires;
+ if (minutes) {
+ var date = new Date();
+ date.setTime(date.getTime()+(minutes*60*1000));
+ expires = "; expires="+date.toGMTString();
+ }
+ else expires = "";
+ document.cookie = name+"="+value+expires+"; path=/";
+ },
+
+ read: function(name) {
+ var nameEQ = name + "=";
+ var ca = document.cookie.split(';');
+ for(var i=0;i < ca.length;i++) {
+ var c = ca[i];
+ while (c.charAt(0)==' ') c = c.substring(1,c.length);
+ if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length,c.length);
+ }
+ return null;
+ },
+
+ remove: function(name) {
+ this.create(name,"",-1);
+ }
+ }
};
var resStore = false
@@ -74,7 +101,12 @@
o.ns = { namespaces: [o.ns], defaultNs: o.ns};
}
- if(!o.lng) { o.lng = f.detectLanguage(); }
+ if (!o.lng) {
+ o.lng = f.detectLanguage();
+ } else {
+ // set cookie with lng set (detectLanguage will set cookie on need)
+ f.cookie.create('i18next', o.lng);
+ }
languages = [];
if (o.lng.indexOf('-') === 2 && o.lng.length === 5) {
@@ -288,19 +320,20 @@
if (qsParm.setLng) {
detectedLng = qsParm.setLng;
- if (!cookie.read('i18next')) {
- cookie.create('i18next', detectedLng);
- }
+ // set cookie
+ f.cookie.create('i18next', detectedLng);
}
if (!detectedLng) {
- var c = cookie.read('i18next');
+ var c = f.cookie.read('i18next');
if (c) detectedLng = c;
}
if (!detectedLng && navigator) {
detectedLng = (navigator.language) ? navigator.language : navigator.userLanguage;
- } else {
+ }
+
+ if (!detectedLng) {
detectedLng = o.fallbackLng;
}
@@ -472,35 +505,6 @@
return currentLng;
}
- var cookie = {
-
- create: function(name,value,minutes) {
- var expires;
- if (minutes) {
- var date = new Date();
- date.setTime(date.getTime()+(minutes*60*1000));
- expires = "; expires="+date.toGMTString();
- }
- else expires = "";
- document.cookie = name+"="+value+expires+"; path=/";
- },
-
- read: function(name) {
- var nameEQ = name + "=";
- var ca = document.cookie.split(';');
- for(var i=0;i < ca.length;i++) {
- var c = ca[i];
- while (c.charAt(0)==' ') c = c.substring(1,c.length);
- if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length,c.length);
- }
- return null;
- },
-
- remove: function(name) {
- this.create(name,"",-1);
- }
- };
-
// extend main object with main api interface
i18n.init = init;
i18n.setLng = setLng;
@@ -0,0 +1,2 @@
+
+(function(){function j(a,c){typeof a=="function"&&(c=a,a={}),a=a||{},e.extend(d,a),typeof d.ns=="string"&&(d.ns={namespaces:[d.ns],defaultNs:d.ns}),d.lng?e.cookie.create("i18next",d.lng):d.lng=e.detectLanguage(),i=[];if(d.lng.indexOf("-")===2&&d.lng.length===5){var h=d.lng.split("-");d.lng=d.lowerCaseLng?h[0].toLowerCase()+"-"+h[1].toLowerCase():h[0].toLowerCase()+"-"+h[1].toUpperCase(),i.push(d.lng),i.push(d.lng.substr(0,2))}else i.push(d.lng);i.indexOf(d.fallbackLng)===-1&&i.push(d.fallbackLng),g=d.lng;if(d.resStore){f=d.resStore,b&&d.setJqueryExt&&l(),c&&c(q);return}t.load(i,d.ns,d.useLocalStorage,d.dynamicLoad,function(a,e){f=e,b&&d.setJqueryExt&&l(),c&&c(q)})}function k(a,b){j({lng:a},b)}function l(){function a(a,c){if(c.length===0)return;var d="text";if(c.indexOf("[")===0){var e=c.split("]");c=e[1],d=e[0].substr(1,e[0].length-1)}c.indexOf(";")===c.length-1&&(c=c.substr(0,c.length-2)),d==="text"?a.text(b.t(c,{defaultValue:a.text()})):a.attr(d,b.t(c,{defaultValue:a.attr(d)}))}b.t=b.t||q,b.fn.i18n=function(c){return this.each(function(){var c=b(this).find("[data-i18n]");c.each(function(){var c=b(this),d=c.attr("data-i18n");if(d.indexOf(";")<=d.length-1){var e=d.split(";");b.each(e,function(b,d){a(c,d)})}else a(c,d)})})}}function m(a,b){return e.each(b,function(b,c){a=a.replace([d.interpolationPrefix,b,d.interpolationSuffix].join(""),c)}),a}function n(a,b){while(a.indexOf(d.reusePrefix)!=-1){h++;if(h>d.maxRecursion)break;var c=a.indexOf(d.reusePrefix),e=a.indexOf(d.reuseSuffix,c)+d.reuseSuffix.length,f=a.substring(c,e),g=f.replace(d.reusePrefix,"").replace(d.reuseSuffix,""),i=r(g,b);a=a.replace(f,i)}return a}function o(a){return a.context&&typeof a.context=="string"}function p(a){return a.count!==undefined&&typeof a.count!="string"&&a.count!==1}function q(a,b){return h=0,r(a,b)}function r(a,b){b=b||{};var c,h,j=b.defaultValue||a;if(!f)return j;var k=d.ns.defaultNs;if(a.indexOf(d.nsseparator)>-1){var l=a.split(d.nsseparator);k=l[0],a=l[1]}if(o(b)){c=e.extend({},b),delete c.context,c.defaultValue=d.contextNotFound;var r=a+"_"+b.context;h=q(r,c);if(h!=d.contextNotFound)return m(h,{context:b.context})}if(p(b)){c=e.extend({},b),delete c.count,c.defaultValue=d.pluralNotFound;var s=a+d.pluralSuffix,v=u.get(g,b.count);v!=="other"&&(s=s+"_"+v),h=q(s,c);if(h!=d.pluralNotFound)return m(h,{count:b.count})}var w;for(var x=0,y=i.length;x<y;x++){if(w)break;var z=i[x],A=a.split(d.keyseparator),B=0,C=f[z][k];while(A[B])C=C&&C[A[B]],B++;C&&(C=m(C,b),C=n(C,b),w=C)}return!w&&d.sendMissing&&t.postMissing(k,a,j),w?w:j}function s(){var a,b=[];(function(){var a=window.location.search.substring(1),c=a.split("&");for(var d=0;d<c.length;d++){var e=c[d].indexOf("=");if(e>0){var f=c[d].substring(0,e),g=c[d].substring(e+1);b[f]=g}}})(),b.setLng&&(a=b.setLng,e.cookie.create("i18next",a));if(!a){var c=e.cookie.read("i18next");c&&(a=c)}return!a&&navigator&&(a=navigator.language?navigator.language:navigator.userLanguage),a||(a=d.fallbackLng),a}function v(){return g}var a=this,b=a.jQuery,c={};typeof module!="undefined"&&module.exports?module.exports=c:b?b.i18n=b.i18n||c:a.i18n=a.i18n||c;var d={lng:!1,lowerCaseLng:!1,fallbackLng:"dev",ns:"translation",nsseparator:":",keyseparator:".",resGetPath:"locales/__lng__/__ns__.json",resPostPath:"locales/add/__lng__/__ns__",resStore:!1,useLocalStorage:!0,dynamicLoad:!1,sendMissing:!1,interpolationPrefix:"__",interpolationSuffix:"__",reusePrefix:"$t(",reuseSuffix:")",pluralSuffix:"_plural",pluralNotFound:["plural_not_found",Math.random()].join(""),pcontextNotFound:["context_not_found",Math.random()].join(""),setJqueryExt:!0},e={extend:b?b.extend:undefined,each:b?b.each:undefined,ajax:b?b.ajax:undefined,detectLanguage:s,cookie:{create:function(a,b,c){var d;if(c){var e=new Date;e.setTime(e.getTime()+c*60*1e3),d="; expires="+e.toGMTString()}else d="";document.cookie=a+"="+b+d+"; path=/"},read:function(a){var b=a+"=",c=document.cookie.split(";");for(var d=0;d<c.length;d++){var e=c[d];while(e.charAt(0)==" ")e=e.substring(1,e.length);if(e.indexOf(b)===0)return e.substring(b.length,e.length)}return null},remove:function(a){this.create(a,"",-1)}}},f=!1,g=!1,h=0,i=[],t={load:function(a,b,c,d,f){c?t._loadLocal(a,function(c,g){var h=[];for(var i=0,j=a.length;i<j;i++)g[a[i]]||h.push(a[i]);h.length>0?t._fetch(h,b,d,function(a,b){e.extend(g,b),t._storeLocal(b),f(null,g)}):f(null,g)}):t._fetch(a,b,d,function(a,b){f(null,b)})},_loadLocal:function(a,b){var c={};if(window.localStorage){var d=a.length;e.each(a,function(a,e){var f=window.localStorage.getItem("res_"+e);f&&(c[e]=JSON.parse(f)),d--,d===0&&b(null,c)})}},_storeLocal:function(a){if(window.localStorage)for(var b in a)window.localStorage.setItem("res_"+b,JSON.stringify(a[b]));return},_fetch:function(a,b,c,f){var g={};if(!c){var h=b.namespaces.length*a.length;e.each(b.namespaces,function(b,c){e.each(a,function(a,b){t._fetchOne(b,c,function(a,d){g[b]=g[b]||{},g[b][c]=d,h--,h===0&&f(null,g)})})})}else e.ajax({url:m(d.resGetPath,{lng:a.join("+"),ns:b.namespaces.join("+")}),success:function(a,b,c){f(null,a)},error:function(a,b,c){f("failed loading resource.json error: "+c)},dataType:"json"})},_fetchOne:function(a,b,c){e.ajax({url:m(d.resGetPath,{lng:a,ns:b}),success:function(a,b,d){c(null,a)},error:function(a,b,d){c(d,{})},dataType:"json"})},postMissing:function(a,b,c){var g={};g[b]=c,e.ajax({url:m(d.resPostPath,{lng:d.fallbackLng,ns:a}),type:"POST",data:g,success:function(e,g,h){f[d.fallbackLng][a][b]=c},error:function(a,b,c){},dataType:"json"})}},u={rules:{sl:function(a){return a%100===1?"one":a%100===2?"two":a%100===3||a%100===4?"few":"other"},ar:function(a){return a===0?"zero":a===1?"one":a===2?"two":a%100>=3&&a%100<=10?"few":a%100>=11&&a%100<=99?"many":"other"}},addRule:function(a,b){u.rules[a]=b},get:function(a,b){function d(a,b){return u.rules[a]?u.rules[a](b):b===1?"one":"other"}var c=a.split("-");return c.length===2?d(c[0],b):d(a,b)}};c.init=j,c.setLng=k,c.t=q,c.translate=q,c.detectLanguage=e.detectLanguage,c.pluralExtensions=u,c.sync=t,c.functions=e,c.lng=v,c.options=d})()
View
@@ -63,7 +63,7 @@
i18n.serveClientScript = function(app) {
app.get('/i18next/i18next.js', function(req, res) {
- var filename = (process.env.NODE_ENV !== 'production' || process.env['DEBUG']) ? __dirname + '/dep/i18next-1.2.1.js' : __dirname + '/dep/i18next-1.2.1.min.js';
+ var filename = (process.env.NODE_ENV !== 'production' || process.env['DEBUG']) ? __dirname + '/dep/i18next-' + i18n.Version + '.js' : __dirname + '/dep/i18next-' + i18n.Version + '.min.js';
fs.readFile(filename, function(err, data) {
if (err) console.log(err);
View
@@ -1,10 +1,15 @@
(function() {
- var i18n = require('./dep/i18next-1.2.1')
+ // when updating i18next dep update version here
+ var i18nVersion = '1.2.3';
+
+ var i18n = require('./dep/i18next-1.2.3')
, url = require('url')
, Cookies = require('cookies')
, wrapper = {};
+ wrapper.Version = i18nVersion;
+
if (typeof module !== 'undefined' && module.exports) {
module.exports = wrapper;
}
@@ -136,7 +141,13 @@
ajax: function() { return null; },
- detectLanguage: detectLanguage
+ detectLanguage: detectLanguage,
+
+ cookie: {
+ create: function() {},
+ read: function() {},
+ remove: function() {}
+ }
};
f.extend(i18n.functions, f);
View
@@ -11,8 +11,9 @@ i18n.init({
// Configuration
app.configure(function() {
app.use(express.bodyParser());
- app.use(i18n.handle);
app.use(app.router);
+ app.use(i18n.handle);
+
app.set('view engine', 'jade');
app.set('views', __dirname);
View
@@ -1 +0,0 @@
---globals i,len

0 comments on commit 669c7c2

Please sign in to comment.