Skip to content

Commit

Permalink
Rename locals to locales
Browse files Browse the repository at this point in the history
  • Loading branch information
woodcockjosh committed Jul 28, 2020
1 parent fb9aa22 commit 31a048c
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 83 deletions.
26 changes: 13 additions & 13 deletions examples/express4-setLocale/index.js
Expand Up @@ -4,7 +4,7 @@ var i18n = require('../../i18n');

// another 'global' object that is bound to i18n additionaly
// DANGER! this `funkyObject` is NOT concurrency aware,
// while req, res and res.locals are and will always be
// while req, res and res.locales are and will always be
var funkyObject = {};

i18n.configure({
Expand All @@ -17,48 +17,48 @@ var app = express();
app.use(i18n.init);

// uses locale as guessed by accept-headers
// req: Hallo res: Hallo res.locals: Hallo funkyObject: Hallo
// req: Hallo res: Hallo res.locales: Hallo funkyObject: Hallo
app.get('/default/:lang', function(req, res) {
render(req, res);
});

// implicitly sets all
// req: مرحبا res: مرحبا res.locals: مرحبا funkyObject: مرحبا
// req: مرحبا res: مرحبا res.locales: مرحبا funkyObject: مرحبا
app.get('/onreq/:lang', function(req, res) {
i18n.setLocale(req, req.params.lang);
render(req, res);
});

// sets res, res.locals and funkyObject
// req: Hallo res: مرحبا res.locals: مرحبا funkyObject: مرحبا
// sets res, res.locales and funkyObject
// req: Hallo res: مرحبا res.locales: مرحبا funkyObject: مرحبا
app.get('/onres/:lang', function(req, res) {
i18n.setLocale(res, req.params.lang);
render(req, res);
});

// sets res.locals and funkyObject
// req: Hallo res: Hallo res.locals: مرحبا funkyObject: مرحبا
app.get('/onreslocals/:lang', function(req, res) {
i18n.setLocale(res.locals, req.params.lang);
// sets res.locales and funkyObject
// req: Hallo res: Hallo res.locales: مرحبا funkyObject: مرحبا
app.get('/onreslocales/:lang', function(req, res) {
i18n.setLocale(res.locales, req.params.lang);
render(req, res);
});

// sets funkyObject only
// req: Hallo res: Hallo res.locals: Hallo funkyObject: مرحبا
// req: Hallo res: Hallo res.locales: Hallo funkyObject: مرحبا
app.get('/onfunky/:lang', function(req, res) {
i18n.setLocale(funkyObject, req.params.lang);
render(req, res);
});

// sets req & funkyObject only
// req: مرحبا res: Hallo res.locals: Hallo funkyObject: مرحبا
// req: مرحبا res: Hallo res.locales: Hallo funkyObject: مرحبا
app.get('/onarray/:lang', function(req, res) {
i18n.setLocale([req, funkyObject], req.params.lang);
render(req, res);
});

// sets res & funkyObject only
// req: Hallo res: مرحبا res.locals: Hallo funkyObject: مرحبا
// req: Hallo res: مرحبا res.locales: Hallo funkyObject: مرحبا
app.get('/onresonly/:lang', function(req, res) {
i18n.setLocale(res, req.params.lang, true);
render(req, res);
Expand All @@ -68,7 +68,7 @@ var getBody = function(req, res){
var body = '';
body += ' req: ' + req.__('Hello');
body += ' res: ' + res.__('Hello');
body += ' res.locals: ' + res.locals.__('Hello');
body += ' res.locales: ' + res.locales.__('Hello');
body += ' funkyObject: ' + funkyObject.__('Hello');
return body;
};
Expand Down
24 changes: 12 additions & 12 deletions examples/express4-setLocale/test.js
Expand Up @@ -11,7 +11,7 @@ var Browser = require('zombie'),
describe('Using i18n in express 4.x with setLocale', function() {

describe('res.send() is able to handle concurrent request correctly', function() {
var expected = 'req: Hallo res: Hallo res.locals: Hallo funkyObject: Hallo';
var expected = 'req: Hallo res: Hallo res.locales: Hallo funkyObject: Hallo';
var url = 'default';
describe('serial requests', function() {
visitLinks('series', url + '/ar', DE, expected, DE, expected);
Expand All @@ -22,7 +22,7 @@ describe('Using i18n in express 4.x with setLocale', function() {
});

describe('i18n.setLocale(req, req.params.lang) is able to set locales correctly by param', function() {
var expected = 'req: مرحبا res: مرحبا res.locals: مرحبا funkyObject: مرحبا';
var expected = 'req: مرحبا res: مرحبا res.locales: مرحبا funkyObject: مرحبا';
var url = 'onreq';
describe('serial requests', function() {
visitLinks('series', url + '/ar', DE, expected, DE, expected);
Expand All @@ -33,7 +33,7 @@ describe('Using i18n in express 4.x with setLocale', function() {
});

describe('i18n.setLocale(res, req.params.lang) is able to set locales correctly by param', function() {
var expected = 'req: Hallo res: مرحبا res.locals: مرحبا funkyObject: مرحبا';
var expected = 'req: Hallo res: مرحبا res.locales: مرحبا funkyObject: مرحبا';
var url = 'onres';
describe('serial requests', function() {
visitLinks('series', url + '/ar', DE, expected, DE, expected);
Expand All @@ -43,9 +43,9 @@ describe('Using i18n in express 4.x with setLocale', function() {
});
});

describe('i18n.setLocale(res.locals, req.params.lang) is able to set locales correctly by param', function() {
var expected = 'req: Hallo res: Hallo res.locals: مرحبا funkyObject: مرحبا';
var url = 'onreslocals';
describe('i18n.setLocale(res.locales, req.params.lang) is able to set locales correctly by param', function() {
var expected = 'req: Hallo res: Hallo res.locales: مرحبا funkyObject: مرحبا';
var url = 'onreslocales';
describe('serial requests', function() {
visitLinks('series', url + '/ar', DE, expected, DE, expected);
});
Expand All @@ -54,8 +54,8 @@ describe('Using i18n in express 4.x with setLocale', function() {
});
});

describe('i18n.setLocale(res.locals, req.params.lang) is able to set locales correctly by param', function() {
var expected = 'req: Hallo res: Hallo res.locals: Hallo funkyObject: مرحبا';
describe('i18n.setLocale(res.locales, req.params.lang) is able to set locales correctly by param', function() {
var expected = 'req: Hallo res: Hallo res.locales: Hallo funkyObject: مرحبا';
var url = 'onfunky';
describe('serial requests', function() {
visitLinks('series', url + '/ar', DE, expected, DE, expected);
Expand All @@ -65,8 +65,8 @@ describe('Using i18n in express 4.x with setLocale', function() {
});
});

describe('i18n.setLocale(res.locals, req.params.lang) is able to set locales correctly by param', function() {
var expected = 'req: مرحبا res: Hallo res.locals: Hallo funkyObject: مرحبا';
describe('i18n.setLocale(res.locales, req.params.lang) is able to set locales correctly by param', function() {
var expected = 'req: مرحبا res: Hallo res.locales: Hallo funkyObject: مرحبا';
var url = 'onarray';
describe('serial requests', function() {
visitLinks('series', url + '/ar', DE, expected, DE, expected);
Expand All @@ -76,8 +76,8 @@ describe('Using i18n in express 4.x with setLocale', function() {
});
});

describe('i18n.setLocale(res.locals, req.params.lang) is able to set locales correctly by param', function() {
var expected = 'req: Hallo res: مرحبا res.locals: Hallo funkyObject: مرحبا';
describe('i18n.setLocale(res.locales, req.params.lang) is able to set locales correctly by param', function() {
var expected = 'req: Hallo res: مرحبا res.locales: Hallo funkyObject: مرحبا';
var url = 'onresonly';
describe('serial requests', function() {
visitLinks('series', url + '/ar', DE, expected, DE, expected);
Expand Down
22 changes: 11 additions & 11 deletions i18n.js
Expand Up @@ -441,25 +441,25 @@ module.exports = (function() {
// escape recursion
// @see - https://github.com/balderdashy/sails/pull/3631
// - https://github.com/mashpie/i18n-node/pull/218
if (targetObject.res.locals) {
if (targetObject.res.locales) {
i18n.setLocale(targetObject.res, targetObject.locale, true);
i18n.setLocale(targetObject.res.locals, targetObject.locale, true);
i18n.setLocale(targetObject.res.locales, targetObject.locale, true);
} else {
i18n.setLocale(targetObject.res, targetObject.locale);
}
}

// consider locals
if (targetObject.locals && !skipImplicitObjects) {
// consider locales
if (targetObject.locales && !skipImplicitObjects) {

// escape recursion
// @see - https://github.com/balderdashy/sails/pull/3631
// - https://github.com/mashpie/i18n-node/pull/218
if (targetObject.locals.res) {
i18n.setLocale(targetObject.locals, targetObject.locale, true);
i18n.setLocale(targetObject.locals.res, targetObject.locale, true);
if (targetObject.locales.res) {
i18n.setLocale(targetObject.locales, targetObject.locale, true);
i18n.setLocale(targetObject.locales.res, targetObject.locale, true);
} else {
i18n.setLocale(targetObject.locals, targetObject.locale);
i18n.setLocale(targetObject.locales, targetObject.locale);
}
}

Expand Down Expand Up @@ -622,9 +622,9 @@ module.exports = (function() {
applyAPItoObject(object.res);
}

// attach to locals if present (ie. in express)
if (object.locals) {
applyAPItoObject(object.locals);
// attach to locales if present (ie. in express)
if (object.locales) {
applyAPItoObject(object.locales);
}
};

Expand Down
6 changes: 3 additions & 3 deletions test/i18n.configureApi.js
Expand Up @@ -69,9 +69,9 @@ describe('configure api', function() {
should.equal(typeof customObject.__, 'undefined');
});

it('should escape res -> locals -> res recursion', function() {
it('should escape res -> locales -> res recursion', function() {
var customObject = {};
customObject.locals = { res: customObject };
customObject.locales = { res: customObject };
reconfigure({
locales: ['en', 'de'],
register: customObject,
Expand All @@ -80,6 +80,6 @@ describe('configure api', function() {
}
});
should.equal(typeof customObject.t, 'function');
should.equal(typeof customObject.locals.t, 'function');
should.equal(typeof customObject.locales.t, 'function');
});
});
6 changes: 3 additions & 3 deletions test/i18n.configureCookiename.js
Expand Up @@ -28,7 +28,7 @@ describe('Locale switching should work when set via cookie', function() {
};

res = {
locals: {}
locales: {}
};
});

Expand All @@ -40,10 +40,10 @@ describe('Locale switching should work when set via cookie', function() {

req.getLocale().should.equal('fr');
res.getLocale().should.equal('fr');
res.locals.getLocale().should.equal('fr');
res.locales.getLocale().should.equal('fr');

req.__('Hello').should.equal('Bonjour');
res.__('Hello').should.equal('Bonjour');
res.locals.__('Hello').should.equal('Bonjour');
res.locales.__('Hello').should.equal('Bonjour');
});
});
6 changes: 3 additions & 3 deletions test/i18n.configureQueryParameter.js
Expand Up @@ -29,7 +29,7 @@ describe('Locale switching should work queryParameter', function() {
};

res = {
locals: {}
locales: {}
};
});

Expand All @@ -41,11 +41,11 @@ describe('Locale switching should work queryParameter', function() {

req.getLocale().should.equal('fr');
res.getLocale().should.equal('fr');
res.locals.getLocale().should.equal('fr');
res.locales.getLocale().should.equal('fr');

req.__('Hello').should.equal('Bonjour');
res.__('Hello').should.equal('Bonjour');
res.locals.__('Hello').should.equal('Bonjour');
res.locales.__('Hello').should.equal('Bonjour');
});

it('should support WHATWG URL API', function() {
Expand Down

0 comments on commit 31a048c

Please sign in to comment.