|
|
@ -1,5 +1,6 @@ |
|
|
|
|
|
|
|
|
|
|
|
const _ = require('lodash'); |
|
|
|
const _ = require('lodash'); |
|
|
|
|
|
|
|
const logger = require( 'pelias-logger' ).get( 'api' ); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
this middleware is responsible for negotiating HTTP locales for incoming |
|
|
|
this middleware is responsible for negotiating HTTP locales for incoming |
|
|
@ -63,36 +64,35 @@ module.exports = function middleware( req, res, next ){ |
|
|
|
|
|
|
|
|
|
|
|
// set defaults
|
|
|
|
// set defaults
|
|
|
|
var lang = language.en; |
|
|
|
var lang = language.en; |
|
|
|
var isDefault = true; |
|
|
|
var locales, best, via = 'default'; |
|
|
|
var locales, best; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// input language via query param
|
|
|
|
// input language via query param
|
|
|
|
if( isDefault && req.query && req.query.lang ){ |
|
|
|
if( via === 'default' && req.query && req.query.lang ){ |
|
|
|
locales = new locale.Locales( req.query.lang ); |
|
|
|
locales = new locale.Locales( req.query.lang ); |
|
|
|
best = locales.best( allLocales ); |
|
|
|
best = locales.best( allLocales ); |
|
|
|
if( best.defaulted ){ |
|
|
|
if( best.defaulted ){ |
|
|
|
req.warnings.push( 'invalid language provided via querystring' ); |
|
|
|
req.warnings.push( 'invalid language provided via querystring' ); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
lang = language[ best.language ]; |
|
|
|
lang = language[ best.language ]; |
|
|
|
isDefault = false; |
|
|
|
via = 'querystring'; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// input language via request headers
|
|
|
|
// input language via request headers
|
|
|
|
if( isDefault && req.headers && req.headers['accept-language'] ){ |
|
|
|
if( via === 'default' && req.headers && req.headers['accept-language'] ){ |
|
|
|
locales = new locale.Locales( req.headers['accept-language'] ); |
|
|
|
locales = new locale.Locales( req.headers['accept-language'] ); |
|
|
|
best = locales.best( allLocales ); |
|
|
|
best = locales.best( allLocales ); |
|
|
|
if( best.defaulted ){ |
|
|
|
if( best.defaulted ){ |
|
|
|
req.warnings.push( 'invalid language provided via header' ); |
|
|
|
req.warnings.push( 'invalid language provided via header' ); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
lang = language[ best.language ]; |
|
|
|
lang = language[ best.language ]; |
|
|
|
isDefault = false; |
|
|
|
via = 'header'; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// set $req.language property
|
|
|
|
// set $req.language property
|
|
|
|
req.language = _.clone( lang ); |
|
|
|
req.language = _.clone( lang ); |
|
|
|
req.language.defaulted = isDefault; |
|
|
|
req.language.defaulted = ( via === 'default' ); |
|
|
|
|
|
|
|
|
|
|
|
// set $req.clean property in order to print language info in response header
|
|
|
|
// set $req.clean property in order to print language info in response header
|
|
|
|
req.clean.lang = { |
|
|
|
req.clean.lang = { |
|
|
@ -102,5 +102,8 @@ module.exports = function middleware( req, res, next ){ |
|
|
|
defaulted: req.language.defaulted |
|
|
|
defaulted: req.language.defaulted |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// logging
|
|
|
|
|
|
|
|
logger.info( '[lang] \'%s\' via \'%s\'', lang.iso6391, via ); |
|
|
|
|
|
|
|
|
|
|
|
next(); |
|
|
|
next(); |
|
|
|
}; |
|
|
|
}; |
|
|
|