// Due to some limitations of Date Internationalization API, this hacks are applied import PowerduckState from '../../app/powerduck-state'; import { capitalize } from '../extensions/string-extensions'; export default class DateLocalizationUtils { /** * The patterns below, per language, each cut out of an ICU rendering the first time a date needs it. Every rendering * builds a date formatter (milliseconds each on a phone), so loading this module formats nothing, and a page in one * language never reads a pattern cut in another. */ private static readonly patterns = new Map(); private static get PATTERN_WITHOUT_YEAR(): string { return this.getPattern('withoutYear', (language) => { // eslint-disable-next-line no-restricted-syntax const dummyDate = new Date(Date.UTC( 2024, 9, 30, )); try { const formatted = dummyDate.toLocaleDateString(language, { day: '2-digit', month: 'long', timeZone: 'UTC', }); const monthName = formatted.split('30').join('').split('.').join('').trim(); return formatted.replace(monthName, '{{month}}').replace('30', '{{day}}'); } catch (error) { return '{{day}}. {{month}}'; } }); } private static get PATTERN_WITH_YEAR(): string { return this.getPattern('withYear', (language) => { // eslint-disable-next-line no-restricted-syntax const dummyDate = new Date(Date.UTC( 2024, 9, 30, )); try { const monthName = dummyDate.toLocaleDateString(language, { day: '2-digit', month: 'long', timeZone: 'UTC', }).split('30').join('').split('.').join('').trim(); const formatted = dummyDate.toLocaleDateString(language, { day: '2-digit', year: 'numeric', month: 'long', timeZone: 'UTC', }); return formatted.replace(monthName, '{{month}}').replace('30', '{{day}}').replace('2024', '{{year}}'); } catch (error) { return '{{day}}. {{month}}'; } }); } private static get PATTERN_PLACEHOLDERED(): string { return this.getPattern('placeholdered', (language) => { // eslint-disable-next-line no-restricted-syntax const dummyDate = new Date(Date.UTC( 2024, 9, 30, )); try { const monthName = dummyDate.toLocaleDateString(language, { month: 'long', timeZone: 'UTC', }); const formatted = dummyDate.toLocaleDateString(language, { year: 'numeric', month: 'long', timeZone: 'UTC', }); return formatted.replace(monthName, '{{date}}').replace('30', '{{day}}').replace('2024', '{{year}}'); } catch (error) { return '{{day}}. {{month}}'; } }); } private static getPattern(name: string, cut: (language: string) => string): string { const language = PowerduckState.getCurrentLanguage(); const key = `${language}:${name}`; let pattern = this.patterns.get(key); if (pattern == null) { pattern = cut(language); this.patterns.set(key, pattern); } return pattern; } static formatRange = (from: Temporal.PlainDateTime | Temporal.PlainDate, to: Temporal.PlainDateTime | Temporal.PlainDate): string => { if (from.year == to.year && from.month == to.month && from.day == to.day) { return this.getFormatted( from, this.PATTERN_WITH_YEAR, 'numeric', 'long', 'numeric', ); } else if (from.month == to.month) { // eslint-disable-next-line no-restricted-syntax const convertDate = new Date(Date.UTC( from.year, from.month - 1, from.day, )); convertDate.setDate(28); const monthName = convertDate.toLocaleDateString(PowerduckState.getCurrentLanguage(), { day: '2-digit', month: 'long', timeZone: 'UTC', }).split('28').join('').split('.').join('').trim(); return this.PATTERN_WITH_YEAR .replace('{{day}}', `${from.day} - ${to.day}`) .replace('{{month}}', monthName) .replace('{{year}}', from.year.toString()); } else if (from.year == to.year) { const fromDate = this.getFormatted( from, this.PATTERN_WITHOUT_YEAR, 'numeric', 'short', ); const toDate = this.getFormatted( to, this.PATTERN_WITHOUT_YEAR, 'numeric', 'short', ); const rangeVal = `${fromDate} - ${toDate}`; return this.PATTERN_PLACEHOLDERED.replace('{{date}}', rangeVal).replace('{{year}}', to.year.toString()); } else { const fromVal = this.getFormatted( from, this.PATTERN_WITH_YEAR, 'numeric', 'short', 'numeric', ); const toVal = this.getFormatted( to, this.PATTERN_WITH_YEAR, 'numeric', 'short', 'numeric', ); return `${fromVal} - ${toVal}`; } }; static getFormatedDateWithoutTime = ( dte: Temporal.PlainDateTime, showYear: boolean, day: 'numeric' | '2-digit' | undefined, month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow' | undefined, year?: 'numeric' | '2-digit' | undefined, ) => { if (showYear) { return this.getFormatted( dte, this.PATTERN_WITH_YEAR, day, month, year, ); } else { return this.getFormatted( dte, this.PATTERN_WITHOUT_YEAR, day, month, ); } }; private static getFormatted = ( dte: Temporal.PlainDateTime | Temporal.PlainDate, pattern: string, day: 'numeric' | '2-digit' | undefined, month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow' | undefined, year?: 'numeric' | '2-digit' | undefined, ): string => { let monthVal: string; try { monthVal = this.getMonthText( dte, pattern, month, ); } catch (error) { monthVal = dte.month.toString(); } let dayVal: string; if (day == '2-digit') { dayVal = dte.day.toString(); if (dayVal.length == 1) { dayVal = `0${dayVal}`; } } else { dayVal = dte.day.toString(); } if (year == null) { return pattern.replace('{{day}}', dayVal).replace('{{month}}', monthVal); } else { return pattern.replace('{{day}}', dayVal).replace('{{month}}', monthVal).replace('{{year}}', dte.year.toString()); } }; /** * The month as it reads inside `pattern`. A stand-alone month name is the nominative ("október"), * which is what a heading wants; a spelled-out month in a date with a day is declined in Slovak, Czech * and Polish ("10. októbra", "10. října", "10 października") and written in lower case in Hungarian * ("2027. október 10."), and ICU hands that form out only for a day + month rendering — the one the * patterns above were cut from. So when the pattern has a day and the month is spelled out, the name * is read off that rendering and left as ICU cased it (lower case where the language writes months so, * as the same-month range already does), wherever the language puts the day. A SHORT name ("okt", * "říj", "paź", "okt.", "Okt", "Oct") is ICU's own abbreviation as each language writes it: inside a * date it is never capitalised ("13. okt 2026", not "13. Okt 2026"). Every other combination — a narrow * or numeric month — keeps the capitalised stand-alone name it always had. */ private static getMonthText = ( dte: Temporal.PlainDateTime | Temporal.PlainDate, pattern: string, month: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow' | undefined, ): string => { const language = PowerduckState.getCurrentLanguage(); if (month === 'long' && pattern.includes('{{day}}')) { const declined = dte.toLocaleString(language, { day: 'numeric', month, timeZone: 'UTC', }).replace(/[\p{Nd}.,]/gu, '').trim(); if (declined.length > 0) { return declined; } } const name = dte.toLocaleString(language, { month, timeZone: 'UTC', }); return month === 'short' ? name : name[capitalize](); }; }