Skip to content

Commit d9c538b

Browse files
committed
Add globalization
1 parent ed45358 commit d9c538b

File tree

34 files changed

+4766
-150
lines changed

34 files changed

+4766
-150
lines changed

browser/current-context.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

6+
var path = require('path');
7+
var SG = require('strong-globalize');
8+
var g = SG();
9+
610
module.exports = function(loopback) {
711
loopback.getCurrentContext = function() {
812
return null;
913
};
1014

1115
loopback.runInContext =
1216
loopback.createContext = function() {
13-
throw new Error('Current context is not supported in the browser.');
17+
throw new Error(g.f('Current context is not supported in the browser.'));
1418
};
1519
};

common/models/access-token.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
* Module Dependencies.
88
*/
99

10+
var path = require('path');
11+
var SG = require('strong-globalize');
12+
var g = SG();
13+
1014
var loopback = require('../../lib/loopback');
1115
var assert = require('assert');
1216
var uid = require('uid2');
@@ -111,7 +115,7 @@ module.exports = function(AccessToken) {
111115
} else if (isValid) {
112116
cb(null, token);
113117
} else {
114-
var e = new Error('Invalid Access Token');
118+
var e = new Error(g.f('Invalid Access Token'));
115119
e.status = e.statusCode = 401;
116120
e.code = 'INVALID_TOKEN';
117121
cb(e);

common/models/acl.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
3737
*/
3838

39+
var path = require('path');
40+
var SG = require('strong-globalize');
41+
var g = SG();
42+
3943
var loopback = require('../../lib/loopback');
4044
var async = require('async');
4145
var assert = require('assert');
@@ -536,7 +540,7 @@ module.exports = function(ACL) {
536540
break;
537541
default:
538542
process.nextTick(function() {
539-
var err = new Error('Invalid principal type: ' + type);
543+
var err = new Error(g.f('Invalid principal type: ', type));
540544
err.statusCode = 400;
541545
cb(err);
542546
});

common/models/change.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
* Module Dependencies.
88
*/
99

10+
var path = require('path');
11+
var SG = require('strong-globalize');
12+
var g = SG();
13+
1014
var PersistedModel = require('../../lib/loopback').PersistedModel;
1115
var loopback = require('../../lib/loopback');
1216
var utils = require('../../lib/utils');
@@ -110,7 +114,7 @@ module.exports = function(Change) {
110114
})
111115
.join('\n');
112116

113-
var msg = 'Cannot rectify ' + modelName + ' changes:\n' + desc;
117+
var msg = g.f('Cannot rectify %s changes:\n%s', modelName, desc);
114118
err = new Error(msg);
115119
err.details = { errors: errors };
116120
return callback(err);

common/models/email.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
* @inherits {Model}
1616
*/
1717

18+
var path = require('path');
19+
var SG = require('strong-globalize');
20+
var g = SG();
21+
1822
module.exports = function(Email) {
1923
/**
2024
* Send an email with the given `options`.
@@ -43,13 +47,13 @@ module.exports = function(Email) {
4347
*/
4448

4549
Email.send = function() {
46-
throw new Error('You must connect the Email Model to a Mail connector');
50+
throw new Error(g.f('You must connect the Email Model to a Mail connector'));
4751
};
4852

4953
/**
5054
* A shortcut for Email.send(this).
5155
*/
5256
Email.prototype.send = function() {
53-
throw new Error('You must connect the Email Model to a Mail connector');
57+
throw new Error(g.f('You must connect the Email Model to a Mail connector'));
5458
};
5559
};

common/models/user.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
* Module Dependencies.
88
*/
99

10+
var path = require('path');
11+
var SG = require('strong-globalize');
12+
var g = SG();
13+
1014
var loopback = require('../../lib/loopback');
1115
var utils = require('../../lib/utils');
12-
var path = require('path');
1316
var SALT_WORK_FACTOR = 10;
1417
var crypto = require('crypto');
1518

@@ -203,22 +206,22 @@ module.exports = function(User) {
203206
realmDelimiter);
204207

205208
if (realmRequired && !query.realm) {
206-
var err1 = new Error('realm is required');
209+
var err1 = new Error(g.f('realm is required'));
207210
err1.statusCode = 400;
208211
err1.code = 'REALM_REQUIRED';
209212
fn(err1);
210213
return fn.promise;
211214
}
212215
if (!query.email && !query.username) {
213-
var err2 = new Error('username or email is required');
216+
var err2 = new Error(g.f('username or email is required'));
214217
err2.statusCode = 400;
215218
err2.code = 'USERNAME_EMAIL_REQUIRED';
216219
fn(err2);
217220
return fn.promise;
218221
}
219222

220223
self.findOne({ where: query }, function(err, user) {
221-
var defaultError = new Error('login failed');
224+
var defaultError = new Error(g.f('login failed'));
222225
defaultError.statusCode = 401;
223226
defaultError.code = 'LOGIN_FAILED';
224227

@@ -248,7 +251,7 @@ module.exports = function(User) {
248251
if (self.settings.emailVerificationRequired && !user.emailVerified) {
249252
// Fail to log in if email verification is not done yet
250253
debug('User email has not been verified');
251-
err = new Error('login failed as the email has not been verified');
254+
err = new Error(g.f('login failed as the email has not been verified'));
252255
err.statusCode = 401;
253256
err.code = 'LOGIN_FAILED_EMAIL_NOT_VERIFIED';
254257
fn(err);
@@ -295,7 +298,7 @@ module.exports = function(User) {
295298
} else if (accessToken) {
296299
accessToken.destroy(fn);
297300
} else {
298-
fn(new Error('could not find accessToken'));
301+
fn(new Error(g.f('could not find accessToken')));
299302
}
300303
});
301304
return fn.promise;
@@ -432,10 +435,14 @@ module.exports = function(User) {
432435

433436
options.text = options.text.replace(/\{href\}/g, options.verifyHref);
434437

438+
options.text = g.f(options.text);
439+
435440
options.to = options.to || user.email;
436441

437442
options.subject = options.subject || 'Thanks for Registering';
438443

444+
options.subject = g.f(options.subject);
445+
439446
options.headers = options.headers || {};
440447

441448
var template = loopback.template(options.template);
@@ -496,11 +503,11 @@ module.exports = function(User) {
496503
});
497504
} else {
498505
if (user) {
499-
err = new Error('Invalid token: ' + token);
506+
err = new Error(g.f('Invalid token: %s', token));
500507
err.statusCode = 400;
501508
err.code = 'INVALID_TOKEN';
502509
} else {
503-
err = new Error('User not found: ' + uid);
510+
err = new Error(g.f('User not found: %s', uid));
504511
err.statusCode = 404;
505512
err.code = 'USER_NOT_FOUND';
506513
}
@@ -529,7 +536,7 @@ module.exports = function(User) {
529536

530537
options = options || {};
531538
if (typeof options.email !== 'string') {
532-
var err = new Error('Email is required');
539+
var err = new Error(g.f('Email is required'));
533540
err.statusCode = 400;
534541
err.code = 'EMAIL_REQUIRED';
535542
cb(err);
@@ -541,7 +548,7 @@ module.exports = function(User) {
541548
return cb(err);
542549
}
543550
if (!user) {
544-
err = new Error('Email not found');
551+
err = new Error(g.f('Email not found'));
545552
err.statusCode = 404;
546553
err.code = 'EMAIL_NOT_FOUND';
547554
return cb(err);
@@ -577,7 +584,7 @@ module.exports = function(User) {
577584
if (typeof plain === 'string' && plain) {
578585
return true;
579586
}
580-
var err = new Error('Invalid password: ' + plain);
587+
var err = new Error(g.f('Invalid password: %s', plain));
581588
err.statusCode = 422;
582589
throw err;
583590
};
@@ -636,20 +643,21 @@ module.exports = function(User) {
636643
UserModel.remoteMethod(
637644
'login',
638645
{
639-
description: 'Login a user with username/email and password.',
646+
description: g.f('Login a user with username/email and password.'),
640647
accepts: [
641648
{ arg: 'credentials', type: 'object', required: true, http: { source: 'body' }},
642649
{ arg: 'include', type: ['string'], http: { source: 'query' },
643-
description: 'Related objects to include in the response. ' +
644-
'See the description of return value for more details.' },
650+
description: g.f('Related objects to include in the response. ' +
651+
'See the description of return value for more details.') },
645652
],
646653
returns: {
647654
arg: 'accessToken', type: 'object', root: true,
648655
description:
649-
'The response body contains properties of the AccessToken created on login.\n' +
656+
g.f('The response body contains properties of the AccessToken created on login.\n' +
650657
'Depending on the value of `include` parameter, the body may contain ' +
651658
'additional properties:\n\n' +
652-
' - `user` - `{User}` - Data of the currently logged in user. (`include=user`)\n\n',
659+
' - `user` - `U+007BUserU+007D` - Data of the currently logged in user. ' +
660+
'(`include=user`)\n\n'),
653661
},
654662
http: { verb: 'post' },
655663
}
@@ -658,16 +666,16 @@ module.exports = function(User) {
658666
UserModel.remoteMethod(
659667
'logout',
660668
{
661-
description: 'Logout a user with access token.',
669+
description: g.f('Logout a user with access token.'),
662670
accepts: [
663671
{ arg: 'access_token', type: 'string', required: true, http: function(ctx) {
664672
var req = ctx && ctx.req;
665673
var accessToken = req && req.accessToken;
666674
var tokenID = accessToken && accessToken.id;
667675

668676
return tokenID;
669-
}, description: 'Do not supply this argument, it is automatically extracted ' +
670-
'from request headers.',
677+
}, description: g.f('Do not supply this argument, it is automatically extracted ' +
678+
'from request headers.'),
671679
},
672680
],
673681
http: { verb: 'all' },
@@ -677,7 +685,7 @@ module.exports = function(User) {
677685
UserModel.remoteMethod(
678686
'confirm',
679687
{
680-
description: 'Confirm a user registration with email verification token.',
688+
description: g.f('Confirm a user registration with email verification token.'),
681689
accepts: [
682690
{ arg: 'uid', type: 'string', required: true },
683691
{ arg: 'token', type: 'string', required: true },
@@ -690,7 +698,7 @@ module.exports = function(User) {
690698
UserModel.remoteMethod(
691699
'resetPassword',
692700
{
693-
description: 'Reset password for a user with email.',
701+
description: g.f('Reset password for a user with email.'),
694702
accepts: [
695703
{ arg: 'options', type: 'object', required: true, http: { source: 'body' }},
696704
],
@@ -701,7 +709,7 @@ module.exports = function(User) {
701709
UserModel.afterRemote('confirm', function(ctx, inst, next) {
702710
if (ctx.args.redirect !== undefined) {
703711
if (!ctx.res) {
704-
return next(new Error('The transport does not support HTTP redirects.'));
712+
return next(new Error(g.f('The transport does not support HTTP redirects.')));
705713
}
706714
ctx.res.location(ctx.args.redirect);
707715
ctx.res.status(302);
@@ -719,7 +727,7 @@ module.exports = function(User) {
719727
// email validation regex
720728
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
721729

722-
UserModel.validatesFormatOf('email', { with: re, message: 'Must provide a valid email' });
730+
UserModel.validatesFormatOf('email', { with: re, message: g.f('Must provide a valid email') });
723731

724732
// FIXME: We need to add support for uniqueness of composite keys in juggler
725733
if (!(UserModel.settings.realmRequired || UserModel.settings.realmDelimiter)) {

example/client-server/client.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

6+
var path = require('path');
7+
var SG = require('strong-globalize');
8+
var g = SG();
9+
610
var loopback = require('../../');
711
var client = loopback();
812
var CartItem = require('./models').CartItem;
@@ -16,10 +20,10 @@ CartItem.attachTo(remote);
1620

1721
// call the remote method
1822
CartItem.sum(1, function(err, total) {
19-
console.log('result:', err || total);
23+
g.log('result:', err || total);
2024
});
2125

2226
// call a built in remote method
2327
CartItem.find(function(err, items) {
24-
console.log(items);
28+
g.log(items);
2529
});

example/colors/app.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

6+
var path = require('path');
7+
var SG = require('strong-globalize');
8+
var g = SG();
9+
610
var loopback = require('../../');
711
var app = loopback();
812

@@ -22,4 +26,4 @@ Color.create({ name: 'blue' });
2226

2327
app.listen(3000);
2428

25-
console.log('a list of colors is available at http://localhost:3000/colors');
29+
g.log('a list of colors is available at http://localhost:3000/colors');

example/context/app.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

6+
var path = require('path');
7+
var SG = require('strong-globalize');
8+
var g = SG();
9+
610
var loopback = require('../../');
711
var app = loopback();
812

@@ -22,13 +26,13 @@ var Color = loopback.createModel('color', { 'name': String });
2226
Color.beforeRemote('**', function(ctx, unused, next) {
2327
// Inside LoopBack code, you can read the property from the context
2428
var ns = loopback.getCurrentContext();
25-
console.log('Request to host', ns && ns.get('host'));
29+
g.log('Request to host', ns && ns.get('host'));
2630
next();
2731
});
2832

2933
app.dataSource('db', { connector: 'memory' });
3034
app.model(Color, { dataSource: 'db' });
3135

3236
app.listen(3000, function() {
33-
console.log('A list of colors is available at http://localhost:3000/colors');
37+
g.log('A list of colors is available at http://localhost:3000/colors');
3438
});

0 commit comments

Comments
 (0)