mirror of
https://github.com/FoggedLens/iD.git
synced 2026-05-16 05:49:16 +02:00
fill in localizations using wikipedia
If a feature has a wikipedia tag, then all translated page titles are fetched. If a user starts adding a localized name for a language with an available translation, that translation gets filled in automatically.
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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 = [];
|
||||
|
||||
Reference in New Issue
Block a user