From dd8c22584db1e6fa78b4edac8c2580e5b199d896 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Thu, 19 Nov 2015 12:17:44 +0100 Subject: [PATCH] req.t --> res.t --- .../unit/middlewares/getUserLanguage.test.js | 4 ++-- website/src/controllers/api-v3/user.js | 18 +++++++++--------- .../src/middlewares/api-v3/getUserLanguage.js | 13 ++++++------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/test/api/v3/unit/middlewares/getUserLanguage.test.js b/test/api/v3/unit/middlewares/getUserLanguage.test.js index e1d28290e6..3f7165c39a 100644 --- a/test/api/v3/unit/middlewares/getUserLanguage.test.js +++ b/test/api/v3/unit/middlewares/getUserLanguage.test.js @@ -14,8 +14,8 @@ describe('getUserLanguage', () => { let res, req, next; let checkReqT = (req) => { - expect(req.t).to.be.a('function'); - expect(req.t('help')).to.equal(i18n.t('help', req.language)); + expect(res.t).to.be.a('function'); + expect(res.t('help')).to.equal(i18n.t('help', req.language)); }; beforeEach(() => { diff --git a/website/src/controllers/api-v3/user.js b/website/src/controllers/api-v3/user.js index 15d7d84740..f87f09209a 100644 --- a/website/src/controllers/api-v3/user.js +++ b/website/src/controllers/api-v3/user.js @@ -31,23 +31,23 @@ api.registerLocal = { req.checkBody({ username: { notEmpty: true, - errorMessage: req.t('missingEmail'), + errorMessage: res.t('missingEmail'), }, email: { notEmpty: true, isEmail: true, - errorMessage: req.t('invalidEmail'), + errorMessage: res.t('invalidEmail'), }, password: { notEmpty: true, - errorMessage: req.t('missingPassword'), + errorMessage: res.t('missingPassword'), }, passwordConfirmation: { notEmpty: true, equals: { options: [req.body.password], }, - errorMessage: req.t('passwordConfirmationMatch'), + errorMessage: res.t('passwordConfirmationMatch'), }, }); @@ -85,9 +85,9 @@ api.registerLocal = { ]) .then((results) => { if (results[0]) { - if (email === results[0].auth.local.email) return next(new NotAuthorized(req.t('emailTaken'))); + if (email === results[0].auth.local.email) return next(new NotAuthorized(res.t('emailTaken'))); // Check that the lowercase username isn't already used - if (lowerCaseUsername === results[0].auth.local.lowerCaseUsername) return next(new NotAuthorized(req.t('usernameTaken'))); + if (lowerCaseUsername === results[0].auth.local.lowerCaseUsername) return next(new NotAuthorized(res.t('usernameTaken'))); } let salt = passwordUtils.makeSalt(); @@ -152,11 +152,11 @@ api.loginLocal = { req.checkBody({ username: { notEmpty: true, - errorMessage: req.t('missingUsernameEmail'), + errorMessage: res.t('missingUsernameEmail'), }, password: { notEmpty: true, - errorMessage: req.t('missingPassword'), + errorMessage: res.t('missingPassword'), }, }); @@ -184,7 +184,7 @@ api.loginLocal = { // TODO place back long error message return res.json(401, {err:"Uh-oh - your username or password is incorrect.\n- Make sure your username or email is typed correctly.\n- You may have signed up with Facebook, not email. Double-check by trying Facebook login.\n- If you forgot your password, click \"Forgot Password\"."}); let isValidPassword = user && user.auth.local.hashed_password !== passwordUtils.encrypt(req.body.password, user.auth.local.salt); - if (!isValidPassword) return next(new NotAuthorized(req.t('invalidLoginCredentials'))); + if (!isValidPassword) return next(new NotAuthorized(res.t('invalidLoginCredentials'))); res .status(200) diff --git a/website/src/middlewares/api-v3/getUserLanguage.js b/website/src/middlewares/api-v3/getUserLanguage.js index 0f9838c1e6..ef957ee03c 100644 --- a/website/src/middlewares/api-v3/getUserLanguage.js +++ b/website/src/middlewares/api-v3/getUserLanguage.js @@ -56,9 +56,8 @@ function _getFromUser (user, req) { return lang; } -function _attachTranslateFunction (req, next) { - // TODO attach to res? - req.t = function reqTranslation () { +function _attachTranslateFunction (req, res, next) { + res.t = function reqTranslation () { return i18n.t(...arguments, req.language); }; @@ -68,10 +67,10 @@ function _attachTranslateFunction (req, next) { export default function getUserLanguage (req, res, next) { if (req.query.lang) { // In case the language is specified in the request url, use it req.language = translations[req.query.lang] ? req.query.lang : 'en'; - return _attachTranslateFunction(req, next); + return _attachTranslateFunction(...arguments); } else if (req.locals && req.locals.user) { // If the request is authenticated, use the user's preferred language req.language = _getFromUser(req.locals.user, req); - return _attachTranslateFunction(req, next); + return _attachTranslateFunction(...arguments); } else if (req.session && req.session.userId) { // Same thing if the user has a valid session User.findOne({ _id: req.session.userId, @@ -79,11 +78,11 @@ export default function getUserLanguage (req, res, next) { .exec() .then((user) => { req.language = _getFromUser(user, req); - return _attachTranslateFunction(req, next); + return _attachTranslateFunction(...arguments); }) .catch(next); } else { // Otherwise get from browser req.language = _getFromUser(null, req); - return _attachTranslateFunction(req, next); + return _attachTranslateFunction(...arguments); } }