From 7e96c3609c63c291a022013b84cee7fbb85e1914 Mon Sep 17 00:00:00 2001 From: Joxit Date: Thu, 1 Sep 2022 10:58:46 +0200 Subject: [PATCH] feat(pip_verbose): add support for `lang` query parameter --- module/pip/StatementPointInPolygonVerbose.js | 10 ++++++++++ server/routes/pip_verbose.js | 14 ++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/module/pip/StatementPointInPolygonVerbose.js b/module/pip/StatementPointInPolygonVerbose.js index 62cec2d..795101c 100644 --- a/module/pip/StatementPointInPolygonVerbose.js +++ b/module/pip/StatementPointInPolygonVerbose.js @@ -27,6 +27,16 @@ class StatementPeliasView extends SqliteStatement { AND abbr = 0 LIMIT 1 ) AS name, + ( + SELECT name + FROM name + WHERE source = place.source + AND id = place.id + AND lang = @lang + AND tag IN ('default', 'preferred') + AND abbr = 0 + LIMIT 1 + ) AS name_localized, ( SELECT GROUP_CONCAT(name, CHAR(30)) FROM ( diff --git a/server/routes/pip_verbose.js b/server/routes/pip_verbose.js index 373ff4b..7a044a2 100644 --- a/server/routes/pip_verbose.js +++ b/server/routes/pip_verbose.js @@ -1,4 +1,7 @@ const util = require('./util') +const iso6393 = require('iso-639-3') +const language = {} +iso6393.filter(i => !!i.iso6391 && !!i.iso6393).forEach(i => { language[i.iso6391] = i.iso6393 }) /** * a verbose view of the PIP data @@ -9,13 +12,15 @@ const util = require('./util') module.exports = function (req, res) { var service = req.app.locals.service + const lang = util.flatten(req.query.lang) // inputs let query = { lon: parseFloat(util.flatten(req.query.lon)), lat: parseFloat(util.flatten(req.query.lat)), limit: 1000, aliaslimit: parseInt(util.flatten(req.query.aliaslimit), 10) || 0, - wofonly: util.flatten(req.query.wofonly) ? 1 : 0 + wofonly: util.flatten(req.query.wofonly) ? 1 : 0, + lang: language[lang] || lang || 'und' } // perform query @@ -27,20 +32,21 @@ module.exports = function (req, res) { let resp = {} rows.forEach(row => { let centroid = row.centroid.split(',').map(util.floatPrecision7) + let name = row.name_localized || row.name || undefined let nameAlias = [] if (query.aliaslimit > 0) { nameAlias = (row.names || '').split(String.fromCharCode(30)) } - nameAlias = (nameAlias.length > 1) ? nameAlias.filter(n => n !== row.name).slice(0, query.aliaslimit) : undefined + nameAlias = (nameAlias.length > 1) ? nameAlias.filter(n => n !== name).slice(0, query.aliaslimit) : undefined let abbrAlias = [] if (query.aliaslimit > 0) { abbrAlias = (row.abbrs || '').split(String.fromCharCode(30)) } - abbrAlias = (abbrAlias.length > 1) ? abbrAlias.filter(n => n !== row.name).slice(0, query.aliaslimit) : undefined + abbrAlias = (abbrAlias.length > 1) ? abbrAlias.filter(n => n !== name).slice(0, query.aliaslimit) : undefined if (!Array.isArray(resp[row.type])) { resp[row.type] = [] } resp[row.type].push({ id: row.id, source: row.source, - name: row.name || undefined, + name: name, name_alias: nameAlias, abbr: row.abbr || undefined, abbr_alias: abbrAlias,