handles post and update of specific language on page model
This commit is contained in:
@@ -8,7 +8,7 @@ import User from '../demo/User/User.model';
|
||||
import fileUpload from 'express-fileupload';
|
||||
import assetRoutes from './routes/uploads.routes'
|
||||
import config from '../demo/payload.config';
|
||||
import language from './middleware/locale';
|
||||
import locale from './middleware/locale';
|
||||
|
||||
module.exports = {
|
||||
init: options => {
|
||||
@@ -38,19 +38,18 @@ module.exports = {
|
||||
res.header('Access-Control-Allow-Headers',
|
||||
'Origin X-Requested-With, Content-Type, Accept, Authorization');
|
||||
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
|
||||
res.header('Content-Language', options.config.localization.language);
|
||||
res.header('Content-Language', options.config.localization.locale);
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
options.router.use('/upload', assetRoutes(options.config));
|
||||
|
||||
options.app.use(language(config.localization));
|
||||
|
||||
options.app.use(express.json());
|
||||
options.app.use(methodOverride('X-HTTP-Method-Override'));
|
||||
options.app.use(express.urlencoded({extended: true}));
|
||||
options.app.use(bodyParser.urlencoded({ extended: true }));
|
||||
options.app.use(locale(config.localization));
|
||||
options.app.use(options.router);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -8,15 +8,21 @@ import languageParser from 'accept-language-parser';
|
||||
*/
|
||||
export default function locale(localization) {
|
||||
return function (req, res, next) {
|
||||
let query;
|
||||
let setLocale;
|
||||
if (req.query.locale) {
|
||||
query = localization.languages.find(locale => locale === req.query.locale);
|
||||
if (query) {
|
||||
req.locale = query;
|
||||
setLocale = localization.languages.find(search => search === req.query.locale);
|
||||
if (setLocale) {
|
||||
req.locale = setLocale;
|
||||
}
|
||||
if (req.query.locale === '*' || req.query.locale === 'all')
|
||||
return next();
|
||||
}
|
||||
if (req.body.locale){
|
||||
setLocale = localization.languages.find(search => search === req.body.locale);
|
||||
if (setLocale) {
|
||||
req.locale = setLocale;
|
||||
}
|
||||
}
|
||||
if (!req.locale && req.headers['accept-language'])
|
||||
req.locale = languageParser.pick(localization.languages, req.headers['accept-language']);
|
||||
if (!req.locale)
|
||||
|
||||
@@ -48,7 +48,8 @@ describe('Payload Middleware', () => {
|
||||
beforeEach(() => {
|
||||
req = {
|
||||
query: {},
|
||||
headers: {}
|
||||
headers: {},
|
||||
body: {}
|
||||
};
|
||||
localization = {
|
||||
languages: ['en', 'es'],
|
||||
@@ -67,7 +68,7 @@ describe('Payload Middleware', () => {
|
||||
});
|
||||
|
||||
it('Supports query param fallback to default', () => {
|
||||
req.query.lang = 'pt';
|
||||
req.query.locale = 'pt';
|
||||
|
||||
locale(localization)(req, res, next);
|
||||
|
||||
@@ -118,5 +119,14 @@ describe('Payload Middleware', () => {
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
expect(req.locale).toBeUndefined();
|
||||
});
|
||||
|
||||
it('Supports locale in body on post', () => {
|
||||
req.body = {locale: 'es'};
|
||||
req.method = 'post';
|
||||
locale(localization)(req, res, next);
|
||||
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
expect(req.locale).toEqual('es');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user