From 6f3d1e6f276a5f36e30c5c5c77b712b1b411708b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Tue, 12 Dec 2023 13:19:21 -0800 Subject: [PATCH] Fixed display names 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. --- data/core.yaml | 2 -- modules/util/util.js | 56 ++++++++++++++++++++++++------------------ test/spec/util/util.js | 12 ++++----- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/data/core.yaml b/data/core.yaml index 1c9cc1a943..2cf2078d03 100644 --- a/data/core.yaml +++ b/data/core.yaml @@ -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}" diff --git a/modules/util/util.js b/modules/util/util.js index 13d30faf3c..b56c967977 100644 --- a/modules/util/util.js +++ b/modules/util/util.js @@ -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(''); } 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 = { @@ -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; } diff --git a/test/spec/util/util.js b/test/spec/util/util.js index 9cd906b699..6b9befa64a 100644 --- a/test/spec/util/util.js +++ b/test/spec/util/util.js @@ -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é [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: '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]'); }); });