Skip to content

Commit

Permalink
added express4-cookie example & test
Browse files Browse the repository at this point in the history
  • Loading branch information
mashpie committed Feb 6, 2016
1 parent 300917b commit 1b5258c
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 10 deletions.
38 changes: 38 additions & 0 deletions examples/express4-cookie/index.js
@@ -0,0 +1,38 @@
var express = require('express');
var cookieParser = require('cookie-parser');
var url = require('url');
var i18n = require('../../i18n');

i18n.configure({
locales: ['en', 'de', 'ar'],
cookie: 'yourcookiename',
directory: __dirname+'/locales'
});

var app = express();
app.use(cookieParser());
app.use(i18n.init);

app.get('/test', function (req, res) {
// delay a response to simulate a long running process,
// while another request comes in with altered language settings
setTimeout(function () {
res.send('<body>res: ' + res.__('Hello') + ' req: ' + req.__('Hello') + '</body>');
}, app.getDelay(req, res));
});

app.get('/testfail', function (req, res) {
// delay a response to simulate a long running process,
// while another request comes in with altered language settings
setTimeout(function () {
res.send('<body>' + i18n.__('Hello') + '</body>');
}, app.getDelay(req, res));
});

// simple param parsing
app.getDelay = function (req, res) {
return url.parse(req.url, true).query.delay || 0;
};

// startup
app.listen(3000);
3 changes: 3 additions & 0 deletions examples/express4-cookie/locales/ar.json
@@ -0,0 +1,3 @@
{
"Hello": "مرحبا"
}
4 changes: 4 additions & 0 deletions examples/express4-cookie/locales/de.json
@@ -0,0 +1,4 @@
{
"Hello World": "Hallo Welt",
"Hello": "Hallo"
}
4 changes: 4 additions & 0 deletions examples/express4-cookie/locales/en.json
@@ -0,0 +1,4 @@
{
"Hello World": "Hello World",
"Hello": "Hello"
}
42 changes: 42 additions & 0 deletions examples/express4-cookie/test.js
@@ -0,0 +1,42 @@
require('./index');

var Browser = require('zombie'),
visitLinks = require('../testlib/visitlinks'),
DE = new Browser(),
EN = new Browser();
AR = new Browser();

EN.setCookie({name: 'yourcookiename', domain: 'localhost', value: 'en'});
DE.setCookie({name: 'yourcookiename', domain: 'localhost', value: 'de'});
AR.setCookie({name: 'yourcookiename', domain: 'localhost', value: 'ar'});

describe('Using i18n in express 4.x with cookieParser', function () {
describe('res.__() is able to handle concurrent request correctly', function () {
describe('serial requests', function () {
visitLinks('series', 'test', EN, 'res: Hello req: Hello', DE, 'res: Hallo req: Hallo');
});

describe('parallel requests', function () {
visitLinks('parallel', 'test', EN, 'res: Hello req: Hello', DE, 'res: Hallo req: Hallo');
});

describe('serial requests AR', function () {
visitLinks('series', 'test', EN, 'res: Hello req: Hello', AR, 'res: مرحبا req: مرحبا');
});

describe('parallel requests AR', function () {
visitLinks('parallel', 'test', EN, 'res: Hello req: Hello', AR, 'res: مرحبا req: مرحبا');
});

});

describe('i18n.__() is NOT able to handle concurrent request correctly', function () {
describe('serial requests', function () {
visitLinks('series', 'testfail', EN, 'Hello', DE, 'Hello');
});

describe('parallel requests', function () {
visitLinks('parallel', 'testfail', EN, 'Hello', DE, 'Hello');
});
});
});
2 changes: 1 addition & 1 deletion i18n.js
Expand Up @@ -792,7 +792,7 @@ function getStorageFilePath(locale) {
return filepathJS;
}
} catch (e) {
logDebug('will write to ' + filepath);
logDebug('will use ' + filepath);
}
return filepath;
}
Expand Down
17 changes: 8 additions & 9 deletions package.json
Expand Up @@ -18,20 +18,19 @@
"lib": "."
},
"dependencies": {
"sprintf-js": ">=1.0.3",
"debug": "*",
"mustache": "*",
"debug": "*"
"sprintf-js": ">=1.0.3"
},
"devDependencies": {
"async": "*",
"cookie-parser": "^1.4.1",
"express": "^4.13.4",
"jshint": "*",
"mocha": "*",
"should": "*",
"zombie": "*",
"async": "*",
"hbs": "*",
"jade": "*",
"consolidate": "*",
"restify": "*",
"jshint": "*"
"url": "^0.11.0",
"zombie": "*"
},
"engines": {
"node": ">=0.10.0"
Expand Down

0 comments on commit 1b5258c

Please sign in to comment.