From 3233750d2dfae7ca846598700c05339e98da99c2 Mon Sep 17 00:00:00 2001 From: Peter Johnson <738069+missinglink@users.noreply.github.com> Date: Wed, 3 Dec 2025 12:01:48 +0100 Subject: [PATCH] refactor(debug): add translations to debug view --- middleware/changeLanguage.js | 40 +++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/middleware/changeLanguage.js b/middleware/changeLanguage.js index 90d45b99e..c8eb51d3d 100644 --- a/middleware/changeLanguage.js +++ b/middleware/changeLanguage.js @@ -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: @@ -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(); @@ -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) { @@ -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 @@ -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;