Skip to content

Commit

Permalink
Add lang atttribute on multilingual names, wikidata/wikipedia fields (#…
Browse files Browse the repository at this point in the history
…10716)

This improves rendering of CJK names that contain unified ideographs that share a unicode codepoint, such as 化.
  • Loading branch information
mapmeld authored Jan 31, 2025
1 parent d39f61f commit 6dcc73c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ _Breaking developer changes, which may affect downstream projects or sites that
* Update the list of languages in the Wikipedia field ([#10489])
* Add Ladin (language code `lld`) as an available option for multilingual names
* Add 30 indigenous languages as dropdown options for multilingual names ([#10684], thanks [@k-yle])
* Add `lang`uage attributes to input fields for multilingual names, as well as wikidata and wikipedia fields ([#10716], thanks [@mapmeld])
#### :hourglass: Performance
#### :mortar_board: Walkthrough / Help
* Fix walkthrough from showing tooltips on wrong location under certain circumstances ([#10650], [#10624], [#10634])
Expand All @@ -83,6 +84,7 @@ _Breaking developer changes, which may affect downstream projects or sites that
[#10653]: https://github.com/openstreetmap/iD/issues/10653
[#10683]: https://github.com/openstreetmap/iD/issues/10683
[#10684]: https://github.com/openstreetmap/iD/pull/10684
[#10716]: https://github.com/openstreetmap/iD/pull/10716
[@winstonsung]: https://github.com/winstonsung/
[@Nekzuris]: https://github.com/Nekzuris
[@michaelabon]: https://github.com/michaelabon
Expand Down
3 changes: 3 additions & 0 deletions modules/ui/fields/localized.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,9 @@ export function uiFieldLocalized(field, context) {
.attr('placeholder', function(d) {
return Array.isArray(d.value) ? t('inspector.multiple_values') : t('translate.localized_translation_name');
})
.attr('lang', function (d) {
return d.lang;
})
.classed('mixed', function(d) {
return Array.isArray(d.value);
});
Expand Down
25 changes: 15 additions & 10 deletions modules/ui/fields/wikidata.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,17 @@ export function uiFieldWikidata(field, context) {
}

function setLabelForEntity() {
var label = '';
var label = {
value: ''
};
if (_wikidataEntity) {
label = entityPropertyForDisplay(_wikidataEntity, 'labels');
if (label.length === 0) {
label = _wikidataEntity.id.toString();
if (label.value.length === 0) {
label.value = _wikidataEntity.id.toString();
}
}
utilGetSetValue(_searchInput, label);
utilGetSetValue(_searchInput, label.value)
.attr('lang', label.language);
}


Expand Down Expand Up @@ -319,10 +322,11 @@ export function uiFieldWikidata(field, context) {

_selection.select('.preset-wikidata-description')
.style('display', function(){
return description.length > 0 ? 'flex' : 'none';
return description.value.length > 0 ? 'flex' : 'none';
})
.select('input')
.attr('value', description);
.attr('value', description.value)
.attr('lang', description.language);

_selection.select('.preset-wikidata-identifier')
.style('display', function(){
Expand Down Expand Up @@ -355,19 +359,20 @@ export function uiFieldWikidata(field, context) {
};

function entityPropertyForDisplay(wikidataEntity, propKey) {
if (!wikidataEntity[propKey]) return '';
var blankResponse = { value: '' };
if (!wikidataEntity[propKey]) return blankResponse;
var propObj = wikidataEntity[propKey];
var langKeys = Object.keys(propObj);
if (langKeys.length === 0) return '';
if (langKeys.length === 0) return blankResponse;
// sorted by priority, since we want to show the user's language first if possible
var langs = wikidata.languagesToQuery();
for (var i in langs) {
var lang = langs[i];
var valueObj = propObj[lang];
if (valueObj && valueObj.value && valueObj.value.length > 0) return valueObj.value;
if (valueObj && valueObj.value && valueObj.value.length > 0) return valueObj;
}
// default to any available value
return propObj[langKeys[0]].value;
return propObj[langKeys[0]];
}


Expand Down
4 changes: 3 additions & 1 deletion modules/ui/fields/wikipedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ export function uiFieldWikipedia(field, context) {
value += '#' + anchor.replace(/_/g, ' ');
}
value = value.slice(0, 1).toUpperCase() + value.slice(1);
utilGetSetValue(_langInput, nativeLangName);
utilGetSetValue(_langInput, nativeLangName)
.attr('lang', langInfo[2]);
utilGetSetValue(_titleInput, value);
}

Expand Down Expand Up @@ -281,6 +282,7 @@ export function uiFieldWikipedia(field, context) {
if (tagLangInfo) {
const nativeLangName = tagLangInfo[1];
utilGetSetValue(_langInput, nativeLangName);
_titleInput.attr('lang', tagLangInfo[2]); // for CJK and other display issues
utilGetSetValue(_titleInput, tagArticleTitle + (anchor ? ('#' + anchor) : ''));
_wikiURL = `${scheme}${tagLang}.${domain}/wiki/${wiki.encodePath(tagArticleTitle, anchor)}`;
} else {
Expand Down
10 changes: 10 additions & 0 deletions test/spec/ui/fields/localized.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,14 @@ describe('iD.uiFieldLocalized', function() {
done();
}, 20);
});

it('has a lang attribute on an existing multilingual name field', function(done) {
var localized = iD.uiFieldLocalized(field, context);
localized.tags({'name:de': 'Value'});
window.setTimeout(function() {
selection.call(localized);
expect(selection.selectAll('.localized-value').attr('lang')).to.eql('de');
done();
}, 20);
});
});

0 comments on commit 6dcc73c

Please sign in to comment.