diff --git a/js/id/services/wikipedia.js b/js/id/services/wikipedia.js index 2d2a27a32..48c26d11c 100644 --- a/js/id/services/wikipedia.js +++ b/js/id/services/wikipedia.js @@ -14,7 +14,7 @@ iD.wikipedia = function() { callback: '{callback}', srsearch: query }), function(data) { - if (!data.query) return console.log("resp", data); + if (!data.query) return callback(query, data.query.search.map(function(d) { return d.title; })); @@ -35,5 +35,27 @@ iD.wikipedia = function() { callback(d[0], d[1]); }); }; + + wiki.translations = function(lang, title, callback) { + d3.jsonp(endpoint.replace('en', lang) + + iD.util.qsString({ + action: 'query', + prop: 'langlinks', + format: 'json', + callback: '{callback}', + lllimit: 500, + titles: title + }), function(d) { + var list = d.query.pages[Object.keys(d.query.pages)[0]], + translations = {}; + if (list) { + list.langlinks.forEach(function(d) { + translations[d.lang] = d['*']; + }); + callback(translations); + } + }); + }; + return wiki; }; diff --git a/js/id/ui/preset/localized.js b/js/id/ui/preset/localized.js index c89618978..a09cb81ce 100644 --- a/js/id/ui/preset/localized.js +++ b/js/id/ui/preset/localized.js @@ -1,7 +1,8 @@ iD.ui.preset.localized = function(field, context) { var event = d3.dispatch('change', 'close'), - input, localizedInputs; + wikipedia = iD.wikipedia(), + input, localizedInputs, wikiTitles; function i(selection) { @@ -22,6 +23,7 @@ iD.ui.preset.localized = function(field, context) { localizedInputs = selection.append('div') .attr('class', 'localized-wrap'); + } function addBlank() { @@ -49,7 +51,13 @@ iD.ui.preset.localized = function(field, context) { if (language) value = language[2]; t[key(d.lang)] = ''; - if (d.value) t[key(value)] = d.value; + + if (d.value) { + t[key(value)] = d.value; + } else if (wikiTitles && wikiTitles[d.lang]) { + t[key(value)] = wikiTitles[d.lang]; + } + event.change(t); d.lang = value; @@ -126,6 +134,18 @@ iD.ui.preset.localized = function(field, context) { } i.tags = function(tags) { + + // Fetch translations from wikipedia + if (tags.wikipedia && !wikiTitles) { + wikiTitles = {}; + var wm = tags.wikipedia.match(/([^:]+):(.+)/); + if (wm && wm[0] && wm[1]) { + wikipedia.translations(wm[1], wm[2], function(d) { + wikiTitles = d; + }); + } + } + input.property('value', tags[field.key] || ''); var postfixed = [];