Skip to content

Commit

Permalink
Fixed display names
Browse files Browse the repository at this point in the history
Temporarily replaced the localizable format string for a dated display name with a hard-coded format string. Replaced the format string for a data range with more robust usage of Intl.DateTimeFormatter.
  • Loading branch information
1ec5 committed Dec 12, 2023
1 parent cb4bb64 commit 4c7bccc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
2 changes: 0 additions & 2 deletions data/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,6 @@ en:
display_name:
# format for the label of a feature that has a name and date range
dated: "{name} [{dateRange}]"
# compact format for expressing a range of years
date_range: "{start}–{end}"
direction: "{direction}"
network: "{network}"
from_to: "from {from} to {to}"
Expand Down
56 changes: 32 additions & 24 deletions modules/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,40 +182,43 @@ export function utilGetAllNodes(ids, graph) {
export function utilDisplayName(entity) {
let dateRange;
if (entity.tags.start_date || entity.tags.end_date) {
// Parse the start and end dates, discarding unnecessary precision, since the display name must be fairly succinct.
let start = entity.tags.start_date && utilNormalizeDateString(entity.tags.start_date);
if (start) {
delete start.localeOptions.month;
delete start.localeOptions.day;
}
let end = entity.tags.end_date && utilNormalizeDateString(entity.tags.end_date);
if (end) {
delete end.localeOptions.month;
delete end.localeOptions.day;

if (start) {
// If the start date has an explicit era, then so must the end date.
end.localeOptions.era = start.localeOptions.era;
}
// Keep the date range suffix succinct by only including the year and era.
let options = {timeZone: 'UTC'};
if (end) {
options.year = end.localeOptions.year;
options.era = end.localeOptions.era;
}
if (start) {
// Override any settings from the end of the range.
options.year = start.localeOptions.year;
options.era = start.localeOptions.era;
}

let dates = [
start ? new Intl.DateTimeFormat(localizer.languageCode(), start.localeOptions).format(start.date) : '',
end ? new Intl.DateTimeFormat(localizer.languageCode(), end.localeOptions).format(end.date) : '',
];

// Omit a redundant date.
if (dates[0] === dates[1]) {
dateRange = dates[0];
} else {
dateRange = t('inspector.display_name.date_range', {start: dates[0], end: dates[1]});
// Get the date range format in structured form, then filter out anything untagged.
let format = new Intl.DateTimeFormat(localizer.languageCode(), options);
let lateDate = new Date(Date.UTC(9999));
let parts = format.formatRangeToParts(start ? start.date : lateDate, end ? end.date : lateDate);
if (!start) {
parts = parts.filter(p => p.source !== 'startRange');
}
if (!end) {
parts = parts.filter(p => p.source !== 'endRange');
}
dateRange = parts.map(p => p.value).join('').trim();
}

var localizedNameKey = 'name:' + localizer.languageCode().toLowerCase();
var name = entity.tags[localizedNameKey] || entity.tags.name || '';
if (name) {
return dateRange ? t('inspector.display_name.dated', {dateRange: dateRange, name: name}) : name;
/* eslint-disable-next-line no-warning-comments */
// FIXME: Make this localizable again once we're set up for translation.
// https://github.com/OpenHistoricalMap/issues/issues/652
// https://github.com/OpenHistoricalMap/issues/issues/470
//return dateRange ? t('inspector.display_name.dated', {dateRange: dateRange, name: name}) : name;
return dateRange ? `${name} [${dateRange}]` : name;
}

var tags = {
Expand Down Expand Up @@ -252,7 +255,12 @@ export function utilDisplayName(entity) {
name = t('inspector.display_name.' + keyComponents.join('_'), tags);
}

return name && dateRange ? t('inspector.display_name.dated', {dateRange: dateRange, name: name}) : name;
/* eslint-disable-next-line no-warning-comments */
// FIXME: Make this localizable again once we're set up for translation.
// https://github.com/OpenHistoricalMap/issues/issues/652
// https://github.com/OpenHistoricalMap/issues/issues/470
//return name && dateRange ? t('inspector.display_name.dated', {dateRange: dateRange, name: name}) : name;
return dateRange ? `${name} [${dateRange}]` : name;
}


Expand Down
12 changes: 6 additions & 6 deletions test/spec/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,12 @@ describe('iD.util', function() {
});
it('appends a date range', function() {
expect(iD.utilDisplayName({tags: {name: 'Arbre Perdu', 'name:en': 'Lost Tree'}})).to.eql('Lost Tree');
expect(iD.utilDisplayName({tags: {name: 'Arbre du Ténéré', 'name:en': 'Tree of Ténéré', start_date: '1673', end_date: '1973'}})).to.eql('Tree of Ténéré [16731973]');
expect(iD.utilDisplayName({tags: {name: 'Charter Oak', start_date: '1614', end_date: '1856-08-21'}})).to.eql('Charter Oak [16141856]');
expect(iD.utilDisplayName({tags: {name: 'Prometheus', start_date: '-2899', end_date: '1964-08'}})).to.eql('Prometheus [2900 BC1964 AD]');
expect(iD.utilDisplayName({tags: {name: 'ජය ශ්‍රී මහා බෝධිය', 'name:en': 'Jaya Sri Maha Bodhi', start_date: '-287'}})).to.eql('Jaya Sri Maha Bodhi [288 BC–]');
expect(iD.utilDisplayName({tags: {name: 'Son of the Tree That Owns Itself', start_date: '1946-12-04'}})).to.eql('Son of the Tree That Owns Itself [1946–]');
expect(iD.utilDisplayName({tags: {name: 'Great Elm', end_date: '1876-02-15'}})).to.eql('Great Elm [–1876]');
expect(iD.utilDisplayName({tags: {name: 'Arbre du Ténéré', 'name:en': 'Tree of Ténéré', start_date: '1673', end_date: '1973'}})).to.eql('Tree of Ténéré [1673 – 1973]');
expect(iD.utilDisplayName({tags: {name: 'Charter Oak', start_date: '1614', end_date: '1856-08-21'}})).to.eql('Charter Oak [1614 – 1856]');
expect(iD.utilDisplayName({tags: {name: 'Prometheus', start_date: '-2899', end_date: '1964-08'}})).to.eql('Prometheus [2900 BC – 1964 AD]');
expect(iD.utilDisplayName({tags: {name: 'ජය ශ්‍රී මහා බෝධිය', 'name:en': 'Jaya Sri Maha Bodhi', start_date: '-287'}})).to.eql('Jaya Sri Maha Bodhi [288 BC–]');
expect(iD.utilDisplayName({tags: {name: 'Son of the Tree That Owns Itself', start_date: '1946-12-04'}})).to.eql('Son of the Tree That Owns Itself [1946–]');
expect(iD.utilDisplayName({tags: {name: 'Great Elm', end_date: '1876-02-15'}})).to.eql('Great Elm [–1876]');
expect(iD.utilDisplayName({tags: {name: 'Capitol Christmas Tree', start_date: '2021-11-19', end_date: '2021-12-25'}})).to.eql('Capitol Christmas Tree [2021]');
});
});
Expand Down

0 comments on commit 4c7bccc

Please sign in to comment.