Skip to content

Commit 3f7554a

Browse files
committed
refactor(debug): add translations to debug view
1 parent 972046a commit 3f7554a

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

middleware/changeLanguage.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const field = require('../helper/fieldValue');
22
const logger = require( 'pelias-logger' ).get( 'api' );
33
const _ = require('lodash');
4+
const Debug = require('../helper/debug');
5+
const debugLog = new Debug('middleware:change_language');
46

57
// note: responses from the language service are (at time of writing)
68
// all from the 'whosonfirst' source.
@@ -51,6 +53,12 @@ function setup(service, should_execute) {
5153
controller: 'language', //technically middleware, but this is consistent with other log lines
5254
});
5355

56+
debugLog.push(req, {
57+
language: req.clean.lang.iso6391,
58+
translations,
59+
duration: Date.now() - start
60+
});
61+
5462
// otherwise, update all the docs with translations
5563
updateDocs(req, res, _.defaultTo(translations, []));
5664
next();
@@ -64,10 +72,10 @@ function setup(service, should_execute) {
6472
// update documents using a translation map
6573
function updateDocs( req, res, translations ){
6674
// this is the target language we will be translating to
67-
var requestLanguage = req.clean.lang.iso6393;
75+
const requestLanguage = req.clean.lang.iso6393;
6876

6977
// iterate over response documents
70-
res.data.forEach( function( doc, p ){
78+
res.data.forEach(doc => {
7179

7280
// update name.default to the request language (if available)
7381
if (req.clean.lang.defaulted === false) {
@@ -78,25 +86,25 @@ function updateDocs( req, res, translations ){
7886
if( !doc || !doc.parent ){ return; }
7987

8088
// iterate over doc.parent.* attributes
81-
for( var attr in doc.parent ){
89+
for( const attr in doc.parent ){
8290

8391
// match only attributes ending with '_id'
84-
var match = attr.match(/^(.*)_id$/);
92+
const match = attr.match(/^(.*)_id$/);
8593
if( !match ){ continue; }
8694

8795
// adminKey is the property name without the '_id'
8896
// eg. for 'country_id', adminKey would be 'country'.
89-
var adminKey = match[1];
90-
var adminValues = doc.parent[adminKey];
97+
const adminKey = match[1];
98+
const adminValues = doc.parent[adminKey];
9199

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

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

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

102110
// id not found in translation service response
@@ -124,17 +132,11 @@ function updateDocs( req, res, translations ){
124132
});
125133
}
126134

127-
// boolean function to check if changing the language is required
128-
function isLanguageChangeRequired( req, res ){
129-
return req && res && res.data && res.data.length &&
130-
req.hasOwnProperty('language');
131-
}
132-
133135
// update name.default with the corresponding translation if available
134136
function translateNameDefault(doc, lang) {
135-
if (lang && _.has(doc, 'name.' + lang)) {
136-
doc.name.default = field.getStringValue(doc.name[lang]);
137-
}
137+
if (lang && _.has(doc, 'name.' + lang)) {
138+
doc.name.default = field.getStringValue(doc.name[lang]);
139+
}
138140
}
139141

140142
module.exports = setup;

0 commit comments

Comments
 (0)