Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions middleware/changeLanguage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var field = require('../helper/fieldValue');
var logger = require( 'pelias-logger' ).get( 'api' );
const field = require('../helper/fieldValue');
const logger = require( 'pelias-logger' ).get( 'api' );
const _ = require('lodash');
const Debug = require('../helper/debug');
const debugLog = new Debug('middleware:change_language');

/**
example response from language web service:
Expand Down Expand Up @@ -47,6 +49,12 @@ function setup(service, should_execute) {
controller: 'language', //technically middleware, but this is consistent with other log lines
});

debugLog.push(req, {
language: req.clean.lang.iso6391,
translations,
duration: Date.now() - start
});

// otherwise, update all the docs with translations
updateDocs(req, res, _.defaultTo(translations, []));
next();
Expand All @@ -60,10 +68,10 @@ function setup(service, should_execute) {
// update documents using a translation map
function updateDocs( req, res, translations ){
// this is the target language we will be translating to
var requestLanguage = req.clean.lang.iso6393;
const requestLanguage = req.clean.lang.iso6393;

// iterate over response documents
res.data.forEach( function( doc, p ){
res.data.forEach(doc => {

// update name.default to the request language (if available)
if (req.clean.lang.defaulted === false) {
Expand All @@ -74,25 +82,25 @@ function updateDocs( req, res, translations ){
if( !doc || !doc.parent ){ return; }

// iterate over doc.parent.* attributes
for( var attr in doc.parent ){
for( const attr in doc.parent ){

// match only attributes ending with '_id'
var match = attr.match(/^(.*)_id$/);
const match = attr.match(/^(.*)_id$/);
if( !match ){ continue; }

// adminKey is the property name without the '_id'
// eg. for 'country_id', adminKey would be 'country'.
var adminKey = match[1];
var adminValues = doc.parent[adminKey];
const adminKey = match[1];
const adminValues = doc.parent[adminKey];

// skip invalid/empty arrays
if( !Array.isArray( adminValues ) || !adminValues.length ){ continue; }

// iterate over adminValues (it's an array and can have more than one value)
for( var i in adminValues ){
for( const i in adminValues ){

// find the corresponding key from the '_id' Array
var id = doc.parent[attr][i];
const id = doc.parent[attr][i];
if( !id ){ continue; }

// id not found in translation service response
Expand Down Expand Up @@ -120,17 +128,11 @@ function updateDocs( req, res, translations ){
});
}

// boolean function to check if changing the language is required
function isLanguageChangeRequired( req, res ){
return req && res && res.data && res.data.length &&
req.hasOwnProperty('language');
}

// update name.default with the corresponding translation if available
function translateNameDefault(doc, lang) {
if (lang && _.has(doc, 'name.' + lang)) {
doc.name.default = field.getStringValue(doc.name[lang]);
}
if (lang && _.has(doc, 'name.' + lang)) {
doc.name.default = field.getStringValue(doc.name[lang]);
}
}

module.exports = setup;