Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions middleware/changeLanguage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
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');

// note: responses from the language service are (at time of writing)
// all from the 'whosonfirst' source.
const LANG_SERVICE_SOURCE = 'whosonfirst';

/**
example response from language web service:
{
Expand Down Expand Up @@ -112,7 +116,7 @@ function updateDocs( req, res, translations ){

// if the record is an admin record we also translate
// the 'name.default' property.
if( adminKey === doc.layer ){
if( adminKey === doc.layer && doc.source === LANG_SERVICE_SOURCE ){
doc.name.default = translations[id].names[ requestLanguage ][0];
}
}
Expand Down
31 changes: 31 additions & 0 deletions test/unit/middleware/changeLanguage.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ module.exports.tests.success_conditions = (test, common) => {
data: [
// doc with 2 layer names that will be changed
{
source: 'whosonfirst',
name: {
default: 'original name for 1st result'
},
Expand All @@ -161,6 +162,7 @@ module.exports.tests.success_conditions = (test, common) => {
{},
// doc with only 1 layer name that will be changed and no default name change
{
source: 'whosonfirst',
name: {
default: 'original name for 2nd result'
},
Expand All @@ -184,6 +186,7 @@ module.exports.tests.success_conditions = (test, common) => {
},
// doc with name that will be translated
{
source: 'whosonfirst',
name: {
default: 'original name for 4th result',
'requested language': 'translated name'
Expand All @@ -197,6 +200,7 @@ module.exports.tests.success_conditions = (test, common) => {
},
// doc with name that will be translated
{
source: 'whosonfirst',
name: {
default: 'original name for 5th result',
'random language': 'translated name'
Expand All @@ -207,6 +211,18 @@ module.exports.tests.success_conditions = (test, common) => {
layer1_id: ['1'],
layer1: ['original name for layer1']
}
},
// geonames record will have parents translated but not default name
{
source: 'geonames',
name: {
default: 'geonames name'
},
layer: 'locality',
parent: {
layer1_id: ['1'],
layer1: ['original name for layer1']
}
}
]
};
Expand All @@ -221,6 +237,7 @@ module.exports.tests.success_conditions = (test, common) => {
t.deepEquals(res, {
data: [
{
source: 'whosonfirst',
name: {
default: 'replacement name for layer1'
},
Expand All @@ -235,6 +252,7 @@ module.exports.tests.success_conditions = (test, common) => {
undefined,
{},
{
source: 'whosonfirst',
name: {
default: 'original name for 2nd result'
},
Expand All @@ -253,6 +271,7 @@ module.exports.tests.success_conditions = (test, common) => {
}
},
{
source: 'whosonfirst',
name: {
default: 'translated name',
'requested language': 'translated name'
Expand All @@ -264,6 +283,7 @@ module.exports.tests.success_conditions = (test, common) => {
}
},
{
source: 'whosonfirst',
name: {
default: 'original name for 5th result',
'random language': 'translated name'
Expand All @@ -273,6 +293,17 @@ module.exports.tests.success_conditions = (test, common) => {
layer1_id: ['1'],
layer1: ['replacement name for layer1']
}
},
{
source: 'geonames',
name: {
default: 'geonames name'
},
layer: 'locality',
parent: {
layer1_id: ['1'],
layer1: ['replacement name for layer1']
}
}
]
});
Expand Down