From bac3fb49ccb4b7f98c5f18257645bed90b34067d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Wed, 6 Dec 2023 00:33:39 -0600 Subject: [PATCH] Append date range to feature labels --- data/core.yaml | 4 ++++ modules/util/util.js | 38 ++++++++++++++++++++++++++++++++++++-- test/spec/util/util.js | 10 ++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/data/core.yaml b/data/core.yaml index 1c29b694dc..1c9cc1a943 100644 --- a/data/core.yaml +++ b/data/core.yaml @@ -760,6 +760,10 @@ en: lock: suggestion: 'The "{label}" field is locked because there is a Wikidata tag. You can delete it or edit the tags in the "Tags" section.' 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}" diff --git a/modules/util/util.js b/modules/util/util.js index 2eba0e4084..13d30faf3c 100644 --- a/modules/util/util.js +++ b/modules/util/util.js @@ -180,9 +180,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; + } + } + + 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]}); + } + } + var localizedNameKey = 'name:' + localizer.languageCode().toLowerCase(); var name = entity.tags[localizedNameKey] || entity.tags.name || ''; - if (name) return name; + if (name) { + return dateRange ? t('inspector.display_name.dated', {dateRange: dateRange, name: name}) : name; + } var tags = { direction: entity.tags.direction, @@ -218,7 +252,7 @@ export function utilDisplayName(entity) { name = t('inspector.display_name.' + keyComponents.join('_'), tags); } - return name; + return name && dateRange ? t('inspector.display_name.dated', {dateRange: dateRange, name: name}) : name; } diff --git a/test/spec/util/util.js b/test/spec/util/util.js index 0d78639849..9cd906b699 100644 --- a/test/spec/util/util.js +++ b/test/spec/util/util.js @@ -283,6 +283,16 @@ describe('iD.util', function() { // BART Yellow Line: Antioch => Pittsburg/Bay Point => SFO Airport => Millbrae expect(iD.utilDisplayName({tags: {network: 'BART', ref: 'Yellow', from: 'Antioch', to: 'Millbrae', via: 'Pittsburg/Bay Point;San Francisco International Airport', route: 'subway'}})).to.eql('BART Yellow from Antioch to Millbrae via Pittsburg/Bay Point;San Francisco International Airport'); }); + 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é [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]'); + }); }); describe('utilOldestID', function() {