From 27e7df888d7bcaa67da6a589e79e26dc53a40887 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Thu, 7 Feb 2013 18:24:01 -0500 Subject: [PATCH] Support locales in taginfo. Fixes #678 --- js/id/ui/inspector.js | 109 ++++++++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 41 deletions(-) diff --git a/js/id/ui/inspector.js b/js/id/ui/inspector.js index 35e03d231..fd29e08a5 100644 --- a/js/id/ui/inspector.js +++ b/js/id/ui/inspector.js @@ -119,6 +119,72 @@ iD.ui.inspector = function() { removeBtn.append('span') .attr('class', 'icon delete'); + function findLocal(docs) { + var locale = iD.detect().locale.toLowerCase(), + localized; + + localized = _.find(docs, function(d) { + return d.lang.toLowerCase() === locale; + }); + if (localized) return localized; + + // try the non-regional version of a language, like + // 'en' if the language is 'en-US' + if (locale.indexOf('-') !== -1) { + var first = locale.split('-')[0]; + localized = _.find(docs, function(d) { + return d.lang.toLowerCase() === first; + }); + if (localized) return localized; + } + + // finally fall back to english + return _.find(docs, function(d) { + return d.lang.toLowerCase() === 'en'; + }); + } + + function keyValueReference(err, docs) { + var local; + if (!err && docs) { + local = findLocal(docs); + } + if (local) { + var types = []; + if (local.on_area) types.push('area'); + if (local.on_node) types.push('point'); + if (local.on_way) types.push('line'); + local.types = types; + iD.ui.modal(context.container()) + .select('.content') + .datum(local) + .call(iD.ui.tagReference); + } else { + iD.ui.flash(context.container()) + .select('.content') + .append('h3') + .text(t('inspector.no_documentation_combination')); + } + } + + function keyReference(err, values) { + if (!err && values.data.length) { + iD.ui.modal(context.container()) + .select('.content') + .datum({ + data: values.data, + title: 'Key:' + params.key, + geometry: params.geometry + }) + .call(iD.keyReference(context)); + } else { + iD.ui.flash(context.container()) + .select('.content') + .append('h3') + .text(t('inspector.no_documentation_key')); + } + } + var helpBtn = row.append('button') .attr('tabindex', -1) .attr('class', 'tag-help minor') @@ -127,48 +193,9 @@ iD.ui.inspector = function() { geometry: entity.geometry(context.graph()) }); if (d.key && d.value) { - taginfo.docs(params, function(err, docs) { - var en; - if (!err && docs) { - en = _.find(docs, function(d) { - return d.lang == 'en'; - }); - } - if (en) { - var types = []; - if (en.on_area) types.push('area'); - if (en.on_node) types.push('point'); - if (en.on_way) types.push('line'); - en.types = types; - iD.ui.modal(context.container()) - .select('.content') - .datum(en) - .call(iD.ui.tagReference); - } else { - iD.ui.flash(context.container()) - .select('.content') - .append('h3') - .text(t('inspector.no_documentation_combination')); - } - }); + taginfo.docs(params, keyValueReference); } else if (d.key) { - taginfo.values(params, function(err, values) { - if (!err && values.data.length) { - iD.ui.modal(context.container()) - .select('.content') - .datum({ - data: values.data, - title: 'Key:' + params.key, - geometry: params.geometry - }) - .call(iD.keyReference(context)); - } else { - iD.ui.flash(context.container()) - .select('.content') - .append('h3') - .text(t('inspector.no_documentation_key')); - } - }); + taginfo.values(params, keyReference); } });