1- var field = require ( '../helper/fieldValue' ) ;
2- var logger = require ( 'pelias-logger' ) . get ( 'api' ) ;
1+ const field = require ( '../helper/fieldValue' ) ;
2+ const logger = require ( 'pelias-logger' ) . get ( 'api' ) ;
33const _ = require ( 'lodash' ) ;
4+ const Debug = require ( '../helper/debug' ) ;
5+ const debugLog = new Debug ( 'middleware:change_language' ) ;
46
57/**
68example response from language web service:
@@ -47,6 +49,12 @@ function setup(service, should_execute) {
4749 controller : 'language' , //technically middleware, but this is consistent with other log lines
4850 } ) ;
4951
52+ debugLog . push ( req , {
53+ language : req . clean . lang . iso6391 ,
54+ translations,
55+ duration : Date . now ( ) - start
56+ } ) ;
57+
5058 // otherwise, update all the docs with translations
5159 updateDocs ( req , res , _ . defaultTo ( translations , [ ] ) ) ;
5260 next ( ) ;
@@ -60,10 +68,10 @@ function setup(service, should_execute) {
6068// update documents using a translation map
6169function updateDocs ( req , res , translations ) {
6270 // this is the target language we will be translating to
63- var requestLanguage = req . clean . lang . iso6393 ;
71+ const requestLanguage = req . clean . lang . iso6393 ;
6472
6573 // iterate over response documents
66- res . data . forEach ( function ( doc , p ) {
74+ res . data . forEach ( doc => {
6775
6876 // update name.default to the request language (if available)
6977 if ( req . clean . lang . defaulted === false ) {
@@ -74,25 +82,25 @@ function updateDocs( req, res, translations ){
7482 if ( ! doc || ! doc . parent ) { return ; }
7583
7684 // iterate over doc.parent.* attributes
77- for ( var attr in doc . parent ) {
85+ for ( const attr in doc . parent ) {
7886
7987 // match only attributes ending with '_id'
80- var match = attr . match ( / ^ ( .* ) _ i d $ / ) ;
88+ const match = attr . match ( / ^ ( .* ) _ i d $ / ) ;
8189 if ( ! match ) { continue ; }
8290
8391 // adminKey is the property name without the '_id'
8492 // eg. for 'country_id', adminKey would be 'country'.
85- var adminKey = match [ 1 ] ;
86- var adminValues = doc . parent [ adminKey ] ;
93+ const adminKey = match [ 1 ] ;
94+ const adminValues = doc . parent [ adminKey ] ;
8795
8896 // skip invalid/empty arrays
8997 if ( ! Array . isArray ( adminValues ) || ! adminValues . length ) { continue ; }
9098
9199 // iterate over adminValues (it's an array and can have more than one value)
92- for ( var i in adminValues ) {
100+ for ( const i in adminValues ) {
93101
94102 // find the corresponding key from the '_id' Array
95- var id = doc . parent [ attr ] [ i ] ;
103+ const id = doc . parent [ attr ] [ i ] ;
96104 if ( ! id ) { continue ; }
97105
98106 // id not found in translation service response
@@ -120,17 +128,11 @@ function updateDocs( req, res, translations ){
120128 } ) ;
121129}
122130
123- // boolean function to check if changing the language is required
124- function isLanguageChangeRequired ( req , res ) {
125- return req && res && res . data && res . data . length &&
126- req . hasOwnProperty ( 'language' ) ;
127- }
128-
129131// update name.default with the corresponding translation if available
130132function translateNameDefault ( doc , lang ) {
131- if ( lang && _ . has ( doc , 'name.' + lang ) ) {
132- doc . name . default = field . getStringValue ( doc . name [ lang ] ) ;
133- }
133+ if ( lang && _ . has ( doc , 'name.' + lang ) ) {
134+ doc . name . default = field . getStringValue ( doc . name [ lang ] ) ;
135+ }
134136}
135137
136138module . exports = setup ;
0 commit comments