Skip to content

Commit 0a42e68

Browse files
committed
fix: add try/catch to lang getter
1 parent 682264c commit 0a42e68

File tree

1 file changed

+29
-63
lines changed

1 file changed

+29
-63
lines changed

src/relative-time-element.ts

Lines changed: 29 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
8282
#updating: false | Promise<void> = false
8383

8484
get #lang() {
85-
return (
86-
this.closest('[lang]')?.getAttribute('lang') ||
87-
this.ownerDocument.documentElement.getAttribute('lang') ||
88-
'default'
89-
)
85+
const lang = this.closest('[lang]')?.getAttribute('lang') ||
86+
this.ownerDocument.documentElement.getAttribute('lang')
87+
try {
88+
return new Intl.Locale(lang ?? '').toString()
89+
} catch {
90+
return 'default'
91+
}
9092
}
9193

9294
#renderRoot: Node = this.shadowRoot ? this.shadowRoot : this.attachShadow ? this.attachShadow({mode: 'open'}) : this
@@ -120,27 +122,14 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
120122
//
121123
// Returns a formatted time String.
122124
#getFormattedTitle(date: Date): string | undefined {
123-
let dateTimeFormat
124-
try {
125-
dateTimeFormat = new Intl.DateTimeFormat(this.#lang, {
126-
day: 'numeric',
127-
month: 'short',
128-
year: 'numeric',
129-
hour: 'numeric',
130-
minute: '2-digit',
131-
timeZoneName: 'short',
132-
})
133-
} catch (_e) {
134-
dateTimeFormat = new Intl.DateTimeFormat('default', {
135-
day: 'numeric',
136-
month: 'short',
137-
year: 'numeric',
138-
hour: 'numeric',
139-
minute: '2-digit',
140-
timeZoneName: 'short',
141-
})
142-
}
143-
return dateTimeFormat.format(date)
125+
return new Intl.DateTimeFormat(this.#lang, {
126+
day: 'numeric',
127+
month: 'short',
128+
year: 'numeric',
129+
hour: 'numeric',
130+
minute: '2-digit',
131+
timeZoneName: 'short',
132+
}).format(date)
144133
}
145134

146135
#resolveFormat(duration: Duration): ResolvedFormat {
@@ -185,19 +174,10 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
185174
}
186175

187176
#getRelativeFormat(duration: Duration): string {
188-
let relativeFormat
189-
190-
try {
191-
relativeFormat = new Intl.RelativeTimeFormat(this.#lang, {
192-
numeric: 'auto',
193-
style: this.formatStyle,
194-
})
195-
} catch (_e) {
196-
relativeFormat = new Intl.RelativeTimeFormat('default', {
197-
numeric: 'auto',
198-
style: this.formatStyle,
199-
})
200-
}
177+
const relativeFormat = new Intl.RelativeTimeFormat(this.#lang, {
178+
numeric: 'auto',
179+
style: this.formatStyle,
180+
})
201181

202182
const tense = this.tense
203183
if (tense === 'future' && duration.sign !== 1) duration = emptyDuration
@@ -210,30 +190,16 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
210190
}
211191

212192
#getDateTimeFormat(date: Date): string {
213-
let formatter
214-
try {
215-
formatter = new Intl.DateTimeFormat(this.#lang, {
216-
second: this.second,
217-
minute: this.minute,
218-
hour: this.hour,
219-
weekday: this.weekday,
220-
day: this.day,
221-
month: this.month,
222-
year: this.year,
223-
timeZoneName: this.timeZoneName,
224-
})
225-
} catch (_e) {
226-
formatter = new Intl.DateTimeFormat('default', {
227-
second: this.second,
228-
minute: this.minute,
229-
hour: this.hour,
230-
weekday: this.weekday,
231-
day: this.day,
232-
month: this.month,
233-
year: this.year,
234-
timeZoneName: this.timeZoneName,
235-
})
236-
}
193+
const formatter = new Intl.DateTimeFormat(this.#lang, {
194+
second: this.second,
195+
minute: this.minute,
196+
hour: this.hour,
197+
weekday: this.weekday,
198+
day: this.day,
199+
month: this.month,
200+
year: this.year,
201+
timeZoneName: this.timeZoneName,
202+
})
237203
return `${this.prefix} ${formatter.format(date)}`.trim()
238204
}
239205

0 commit comments

Comments
 (0)