diff --git a/src/relative-time.ts b/src/relative-time.ts index 39b02cf..bbc1c82 100644 --- a/src/relative-time.ts +++ b/src/relative-time.ts @@ -168,7 +168,7 @@ export default class RelativeTime { } formatDate(): string { - let format = isDayFirst() ? '%e %b' : '%b %e' + let format = isDayFirst(this.locale) ? '%e %b' : '%b %e' if (!isThisYear(this.date)) { format += isYearSeparator() ? ', %Y' : ' %Y' } diff --git a/src/utils.ts b/src/utils.ts index 5fa8345..14e7b80 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -91,14 +91,13 @@ export function strftime(time: Date, formatString: string): string { }) } -export function makeFormatter(options: Intl.DateTimeFormatOptions): () => Intl.DateTimeFormat | undefined { - let format: Intl.DateTimeFormat | null - return function (): Intl.DateTimeFormat | undefined { - if (format) return format +export function makeFormatter( + options: Intl.DateTimeFormatOptions +): (locale?: string) => Intl.DateTimeFormat | undefined { + return function (locale?: string): Intl.DateTimeFormat | undefined { if ('Intl' in window) { try { - format = new Intl.DateTimeFormat(undefined, options) - return format + return new Intl.DateTimeFormat(locale, options) } catch (e) { if (!(e instanceof RangeError)) { throw e @@ -108,7 +107,7 @@ export function makeFormatter(options: Intl.DateTimeFormatOptions): () => Intl.D } } -let dayFirst: boolean | null = null +let dayFirst: Record = {} const dayFirstFormatter = makeFormatter({day: 'numeric', month: 'short'}) // Private: Determine if the day should be formatted before the month name in @@ -116,16 +115,16 @@ const dayFirstFormatter = makeFormatter({day: 'numeric', month: 'short'}) // for en-US. // // Returns true if the day appears before the month. -export function isDayFirst(): boolean { - if (dayFirst !== null) { - return dayFirst +export function isDayFirst(locale: string = 'default'): boolean { + if (locale in dayFirst) { + return dayFirst[locale] } - const formatter = dayFirstFormatter() + const formatter = dayFirstFormatter(locale) if (formatter) { const output = formatter.format(new Date(0)) - dayFirst = !!output.match(/^\d/) - return dayFirst + dayFirst[locale] = !!output.match(/^\d/) + return dayFirst[locale] } else { return false } diff --git a/test/relative-time.js b/test/relative-time.js index be31a35..1b504c0 100644 --- a/test/relative-time.js +++ b/test/relative-time.js @@ -114,6 +114,7 @@ suite('relative-time', function () { test('switches to dates after 30 past days', function () { const now = new Date(Date.now() - 30 * 60 * 60 * 24 * 1000).toISOString() const time = document.createElement('relative-time') + time.setAttribute('lang', 'en-US') time.setAttribute('datetime', now) assert.match(time.textContent, /on [A-Z][a-z]{2} \d{1,2}/) }) @@ -121,6 +122,7 @@ suite('relative-time', function () { test('switches to dates after 30 future days', function () { const now = new Date(Date.now() + 30 * 60 * 60 * 24 * 1000).toISOString() const time = document.createElement('relative-time') + time.setAttribute('lang', 'en-US') time.setAttribute('datetime', now) assert.match(time.textContent, /on [A-Z][a-z]{2} \d{1,2}/) })