Skip to content

Commit 75c8f1d

Browse files
committed
Add globalization
1 parent de0f51b commit 75c8f1d

File tree

20 files changed

+306
-142
lines changed

20 files changed

+306
-142
lines changed

browser/current-context.js

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

6+
var g = require('strong-globalize')();
7+
68
module.exports = function(loopback) {
79
loopback.getCurrentContext = function() {
810
return null;
911
};
1012

1113
loopback.runInContext =
1214
loopback.createContext = function() {
13-
throw new Error('Current context is not supported in the browser.');
15+
throw new Error(g.f('Current context is not supported in the browser.'));
1416
};
1517
};

common/models/access-token.js

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

10+
var g = require('strong-globalize')();
11+
1012
var loopback = require('../../lib/loopback');
1113
var assert = require('assert');
1214
var uid = require('uid2');
@@ -111,7 +113,7 @@ module.exports = function(AccessToken) {
111113
} else if (isValid) {
112114
cb(null, token);
113115
} else {
114-
var e = new Error('Invalid Access Token');
116+
var e = new Error(g.f('Invalid Access Token'));
115117
e.status = e.statusCode = 401;
116118
e.code = 'INVALID_TOKEN';
117119
cb(e);

common/models/acl.js

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

39+
var g = require('strong-globalize')();
40+
3941
var loopback = require('../../lib/loopback');
4042
var async = require('async');
4143
var assert = require('assert');
@@ -536,7 +538,7 @@ module.exports = function(ACL) {
536538
break;
537539
default:
538540
process.nextTick(function() {
539-
var err = new Error('Invalid principal type: ' + type);
541+
var err = new Error(g.f('Invalid principal type: %s', type));
540542
err.statusCode = 400;
541543
cb(err);
542544
});

common/models/change.js

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

10+
var g = require('strong-globalize')();
11+
1012
var PersistedModel = require('../../lib/loopback').PersistedModel;
1113
var loopback = require('../../lib/loopback');
1214
var utils = require('../../lib/utils');
@@ -110,7 +112,7 @@ module.exports = function(Change) {
110112
})
111113
.join('\n');
112114

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

common/models/email.js

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

18+
var g = require('strong-globalize')();
19+
1820
module.exports = function(Email) {
1921
/**
2022
* Send an email with the given `options`.
@@ -43,13 +45,13 @@ module.exports = function(Email) {
4345
*/
4446

4547
Email.send = function() {
46-
throw new Error('You must connect the Email Model to a Mail connector');
48+
throw new Error(g.f('You must connect the {{Email}} Model to a {{Mail}} connector'));
4749
};
4850

4951
/**
5052
* A shortcut for Email.send(this).
5153
*/
5254
Email.prototype.send = function() {
53-
throw new Error('You must connect the Email Model to a Mail connector');
55+
throw new Error(g.f('You must connect the {{Email}} Model to a {{Mail}} connector'));
5456
};
5557
};

common/models/user.js

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

10+
var g = require('strong-globalize')();
11+
1012
var loopback = require('../../lib/loopback');
1113
var utils = require('../../lib/utils');
1214
var path = require('path');
@@ -203,22 +205,22 @@ module.exports = function(User) {
203205
realmDelimiter);
204206

205207
if (realmRequired && !query.realm) {
206-
var err1 = new Error('realm is required');
208+
var err1 = new Error(g.f('realm is required'));
207209
err1.statusCode = 400;
208210
err1.code = 'REALM_REQUIRED';
209211
fn(err1);
210212
return fn.promise;
211213
}
212214
if (!query.email && !query.username) {
213-
var err2 = new Error('username or email is required');
215+
var err2 = new Error(g.f('{{username}} or {{email}} is required'));
214216
err2.statusCode = 400;
215217
err2.code = 'USERNAME_EMAIL_REQUIRED';
216218
fn(err2);
217219
return fn.promise;
218220
}
219221

220222
self.findOne({ where: query }, function(err, user) {
221-
var defaultError = new Error('login failed');
223+
var defaultError = new Error(g.f('login failed'));
222224
defaultError.statusCode = 401;
223225
defaultError.code = 'LOGIN_FAILED';
224226

@@ -248,7 +250,7 @@ module.exports = function(User) {
248250
if (self.settings.emailVerificationRequired && !user.emailVerified) {
249251
// Fail to log in if email verification is not done yet
250252
debug('User email has not been verified');
251-
err = new Error('login failed as the email has not been verified');
253+
err = new Error(g.f('login failed as the email has not been verified'));
252254
err.statusCode = 401;
253255
err.code = 'LOGIN_FAILED_EMAIL_NOT_VERIFIED';
254256
fn(err);
@@ -295,7 +297,7 @@ module.exports = function(User) {
295297
} else if (accessToken) {
296298
accessToken.destroy(fn);
297299
} else {
298-
fn(new Error('could not find accessToken'));
300+
fn(new Error(g.f('could not find {{accessToken}}')));
299301
}
300302
});
301303
return fn.promise;
@@ -446,15 +448,21 @@ module.exports = function(User) {
446448

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

451+
options.text = g.f(options.text);
452+
449453
options.to = options.to || user.email;
450454

451455
options.subject = options.subject || 'Thanks for Registering';
452456

457+
options.subject = g.f(options.subject);
458+
453459
options.headers = options.headers || {};
454460

455461
var template = loopback.template(options.template);
456462
options.html = template(options);
457463

464+
options.html = g.f(options.html);
465+
458466
Email.send(options, function(err, email) {
459467
if (err) {
460468
fn(err);
@@ -510,11 +518,11 @@ module.exports = function(User) {
510518
});
511519
} else {
512520
if (user) {
513-
err = new Error('Invalid token: ' + token);
521+
err = new Error(g.f('Invalid token: %s', token));
514522
err.statusCode = 400;
515523
err.code = 'INVALID_TOKEN';
516524
} else {
517-
err = new Error('User not found: ' + uid);
525+
err = new Error(g.f('User not found: %s', uid));
518526
err.statusCode = 404;
519527
err.code = 'USER_NOT_FOUND';
520528
}
@@ -543,7 +551,7 @@ module.exports = function(User) {
543551

544552
options = options || {};
545553
if (typeof options.email !== 'string') {
546-
var err = new Error('Email is required');
554+
var err = new Error(g.f('Email is required'));
547555
err.statusCode = 400;
548556
err.code = 'EMAIL_REQUIRED';
549557
cb(err);
@@ -555,7 +563,7 @@ module.exports = function(User) {
555563
return cb(err);
556564
}
557565
if (!user) {
558-
err = new Error('Email not found');
566+
err = new Error(g.f('Email not found'));
559567
err.statusCode = 404;
560568
err.code = 'EMAIL_NOT_FOUND';
561569
return cb(err);
@@ -591,7 +599,7 @@ module.exports = function(User) {
591599
if (typeof plain === 'string' && plain) {
592600
return true;
593601
}
594-
var err = new Error('Invalid password: ' + plain);
602+
var err = new Error(g.f('Invalid password: %s', plain));
595603
err.statusCode = 422;
596604
throw err;
597605
};
@@ -650,20 +658,21 @@ module.exports = function(User) {
650658
UserModel.remoteMethod(
651659
'login',
652660
{
653-
description: 'Login a user with username/email and password.',
661+
description: g.f('Login a user with username/email and password.'),
654662
accepts: [
655663
{ arg: 'credentials', type: 'object', required: true, http: { source: 'body' }},
656664
{ arg: 'include', type: ['string'], http: { source: 'query' },
657-
description: 'Related objects to include in the response. ' +
658-
'See the description of return value for more details.' },
665+
description: g.f('Related objects to include in the response. ' +
666+
'See the description of return value for more details.') },
659667
],
660668
returns: {
661669
arg: 'accessToken', type: 'object', root: true,
662670
description:
663-
'The response body contains properties of the AccessToken created on login.\n' +
671+
g.f('The response body contains properties of the {{AccessToken}} created on login.\n' +
664672
'Depending on the value of `include` parameter, the body may contain ' +
665673
'additional properties:\n\n' +
666-
' - `user` - `{User}` - Data of the currently logged in user. (`include=user`)\n\n',
674+
' - `user` - `U+007BUserU+007D` - Data of the currently logged in user. ' +
675+
'{{(`include=user`)}}\n\n'),
667676
},
668677
http: { verb: 'post' },
669678
}
@@ -672,16 +681,16 @@ module.exports = function(User) {
672681
UserModel.remoteMethod(
673682
'logout',
674683
{
675-
description: 'Logout a user with access token.',
684+
description: g.f('Logout a user with access token.'),
676685
accepts: [
677686
{ arg: 'access_token', type: 'string', required: true, http: function(ctx) {
678687
var req = ctx && ctx.req;
679688
var accessToken = req && req.accessToken;
680689
var tokenID = accessToken && accessToken.id;
681690

682691
return tokenID;
683-
}, description: 'Do not supply this argument, it is automatically extracted ' +
684-
'from request headers.',
692+
}, description: g.f('Do not supply this argument, it is automatically extracted ' +
693+
'from request headers.'),
685694
},
686695
],
687696
http: { verb: 'all' },
@@ -691,7 +700,7 @@ module.exports = function(User) {
691700
UserModel.remoteMethod(
692701
'confirm',
693702
{
694-
description: 'Confirm a user registration with email verification token.',
703+
description: g.f('Confirm a user registration with email verification token.'),
695704
accepts: [
696705
{ arg: 'uid', type: 'string', required: true },
697706
{ arg: 'token', type: 'string', required: true },
@@ -704,7 +713,7 @@ module.exports = function(User) {
704713
UserModel.remoteMethod(
705714
'resetPassword',
706715
{
707-
description: 'Reset password for a user with email.',
716+
description: g.f('Reset password for a user with email.'),
708717
accepts: [
709718
{ arg: 'options', type: 'object', required: true, http: { source: 'body' }},
710719
],
@@ -715,7 +724,7 @@ module.exports = function(User) {
715724
UserModel.afterRemote('confirm', function(ctx, inst, next) {
716725
if (ctx.args.redirect !== undefined) {
717726
if (!ctx.res) {
718-
return next(new Error('The transport does not support HTTP redirects.'));
727+
return next(new Error(g.f('The transport does not support HTTP redirects.')));
719728
}
720729
ctx.res.location(ctx.args.redirect);
721730
ctx.res.status(302);
@@ -733,7 +742,7 @@ module.exports = function(User) {
733742
// email validation regex
734743
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,}))$/;
735744

736-
UserModel.validatesFormatOf('email', { with: re, message: 'Must provide a valid email' });
745+
UserModel.validatesFormatOf('email', { with: re, message: g.f('Must provide a valid email') });
737746

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

example/client-server/client.js

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

6+
var g = require('strong-globalize')();
7+
68
var loopback = require('../../');
79
var client = loopback();
810
var CartItem = require('./models').CartItem;
@@ -16,10 +18,10 @@ CartItem.attachTo(remote);
1618

1719
// call the remote method
1820
CartItem.sum(1, function(err, total) {
19-
console.log('result:', err || total);
21+
g.log('result:%s', err || total);
2022
});
2123

2224
// call a built in remote method
2325
CartItem.find(function(err, items) {
24-
console.log(items);
26+
g.log(items);
2527
});

example/colors/app.js

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

6+
var g = require('strong-globalize')();
7+
68
var loopback = require('../../');
79
var app = loopback();
810

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

2325
app.listen(3000);
2426

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

example/context/app.js

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

6+
var g = require('strong-globalize')();
7+
68
var loopback = require('../../');
79
var app = loopback();
810

@@ -22,13 +24,13 @@ var Color = loopback.createModel('color', { 'name': String });
2224
Color.beforeRemote('**', function(ctx, unused, next) {
2325
// Inside LoopBack code, you can read the property from the context
2426
var ns = loopback.getCurrentContext();
25-
console.log('Request to host', ns && ns.get('host'));
27+
g.log('Request to host %s', ns && ns.get('host'));
2628
next();
2729
});
2830

2931
app.dataSource('db', { connector: 'memory' });
3032
app.model(Color, { dataSource: 'db' });
3133

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

example/mobile-models/app.js

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

6+
var g = require('strong-globalize')();
7+
68
var models = require('../../lib/models');
79

810
var loopback = require('../../');
@@ -37,15 +39,15 @@ var data = { pushSettings: [
3739
] };
3840

3941
Application.create(data, function(err, data) {
40-
console.log('Created: ', data.toObject());
42+
g.log('Created: %s', data.toObject());
4143
});
4244

4345

44-
Application.register('rfeng', 'MyApp', { description: 'My first mobile application' },
46+
Application.register('rfeng', 'MyApp', { description: g.f('My first mobile application') },
4547
function(err, result) {
4648
console.log(result.toObject());
4749

4850
result.resetKeys(function(err, result) {
49-
console.log(result.toObject());
51+
g.log(result.toObject());
5052
});
5153
});

0 commit comments

Comments
 (0)