Skip to content

Commit

Permalink
feat(translation) Translate name to request language when available
Browse files Browse the repository at this point in the history
  • Loading branch information
mihneadb authored and orangejulius committed May 28, 2019
1 parent f209100 commit eaf1f4e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
13 changes: 13 additions & 0 deletions middleware/changeLanguage.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var field = require('../helper/fieldValue');
var logger = require( 'pelias-logger' ).get( 'api' );
const _ = require('lodash');

Expand Down Expand Up @@ -64,6 +65,11 @@ function updateDocs( req, res, translations ){
// iterate over response documents
res.data.forEach( function( doc, p ){

// update name.default to the request language (if available)
if (req.clean.lang.defaulted === false) {
translateNameDefault(doc, req.clean.lang.iso6391);
}

// skip invalid records
if( !doc || !doc.parent ){ return; }

Expand Down Expand Up @@ -120,4 +126,11 @@ function isLanguageChangeRequired( req, res ){
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]);
}
}

module.exports = setup;
52 changes: 51 additions & 1 deletion test/unit/middleware/changeLanguage.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ module.exports.tests.success_conditions = (test, common) => {
const req = {
clean: {
lang: {
iso6393: 'requested language'
iso6393: 'requested language',
iso6391: 'requested language',
defaulted: false
}
}
};
Expand Down Expand Up @@ -179,6 +181,32 @@ module.exports.tests.success_conditions = (test, common) => {
layer13_id: [undefined],
layer13: ['original name for layer13']
}
},
// doc with name that will be translated
{
name: {
default: 'original name for 4th result',
'requested language': 'translated name'
},
// note that this is address!
layer: 'address',
parent: {
layer1_id: ['1'],
layer1: ['original name for layer1']
}
},
// doc with name that will be translated
{
name: {
default: 'original name for 5th result',
'random language': 'translated name'
},
// note that this is address!
layer: 'address',
parent: {
layer1_id: ['1'],
layer1: ['original name for layer1']
}
}
]
};
Expand Down Expand Up @@ -223,6 +251,28 @@ module.exports.tests.success_conditions = (test, common) => {
layer13_id: [undefined],
layer13: ['original name for layer13']
}
},
{
name: {
default: 'translated name',
'requested language': 'translated name'
},
layer: 'address',
parent: {
layer1_id: ['1'],
layer1: ['replacement name for layer1']
}
},
{
name: {
default: 'original name for 5th result',
'random language': 'translated name'
},
layer: 'address',
parent: {
layer1_id: ['1'],
layer1: ['replacement name for layer1']
}
}
]
});
Expand Down

0 comments on commit eaf1f4e

Please sign in to comment.