@@ -82,11 +82,13 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
82
82
#updating: false | Promise < void > = false
83
83
84
84
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
+ }
90
92
}
91
93
92
94
#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
120
122
//
121
123
// Returns a formatted time String.
122
124
#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 )
144
133
}
145
134
146
135
#resolveFormat( duration : Duration ) : ResolvedFormat {
@@ -185,19 +174,10 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
185
174
}
186
175
187
176
#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
+ } )
201
181
202
182
const tense = this . tense
203
183
if ( tense === 'future' && duration . sign !== 1 ) duration = emptyDuration
@@ -210,30 +190,16 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
210
190
}
211
191
212
192
#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
+ } )
237
203
return `${ this . prefix } ${ formatter . format ( date ) } ` . trim ( )
238
204
}
239
205
0 commit comments