@@ -274,41 +274,44 @@ export function timestampToPlainDateTime(
274274 return instantToPlainDateTime ( timestampToInstant ( timestamp ) , timezone ) ;
275275}
276276
277+ export interface LocalizeRelativeTimeOptions {
278+ style ?: Intl . RelativeTimeFormatStyle ;
279+ /// We override the default to "auto"
280+ numeric ?: Intl . RelativeTimeFormatNumeric ;
281+ /// If true, capitalize if the script supports it.
282+ standalone ?: boolean ;
283+ }
284+
285+ // Creating Intl objects every time is slow, so cache them.
277286const relativeTimeFormatCache = new Map < string , Intl . RelativeTimeFormat > ( ) ;
278287
279288export function localizeRelativeTimeUnit (
280289 value : number ,
281290 unit : Intl . RelativeTimeFormatUnit ,
282291 locale : string ,
283- options ?: {
284- style ?: Intl . RelativeTimeFormatStyle ;
285- numeric ?: Intl . RelativeTimeFormatNumeric ;
286- } ,
292+ options ?: LocalizeRelativeTimeOptions ,
287293) : string {
288- const cacheKey = JSON . stringify ( { locale, ...options } ) ;
294+ const intlOptions = {
295+ style : options ?. style ?? "long" ,
296+ numeric : options ?. numeric ?? "auto" ,
297+ } ;
298+ const cacheKey = JSON . stringify ( { locale, ...intlOptions } ) ;
289299 let formatter = relativeTimeFormatCache . get ( cacheKey ) ;
290300 if ( ! formatter ) {
291- formatter = new Intl . RelativeTimeFormat ( locale , options ) ;
301+ formatter = new Intl . RelativeTimeFormat ( locale , intlOptions ) ;
292302 relativeTimeFormatCache . set ( cacheKey , formatter ) ;
293303 }
294- return formatter . format ( value , unit ) ;
295- }
296-
297- /// Localizes a number of days as a relative time string (e.g. "today", "tomorrow", "in 3 days").
298- export function localizeRelativeDays ( days : number , locale : string ) : string {
299- return new Intl . RelativeTimeFormat ( locale , { numeric : "auto" } ) . format (
300- days ,
301- "day" ,
302- ) ;
304+ let result = formatter . format ( value , unit ) ;
305+ if ( options ?. standalone === true )
306+ result = capitalizeFirstLetter ( result , locale ) ;
307+ return result ;
303308}
304309
305310/// Localizes a duration (positive or negative)'s largest unit (e.g. "4 days" ignoring minutes).
306311export function localizeDurationLargestUnit (
307312 delta : Temporal . Duration ,
308313 locale : string ,
309- options ?: {
310- style ?: Intl . RelativeTimeFormatStyle ;
311- numeric ?: Intl . RelativeTimeFormatNumeric ;
314+ options ?: LocalizeRelativeTimeOptions & {
312315 smallestUnit ?: Temporal . PluralizeUnit < Temporal . TimeUnit > ;
313316 t ?: TFunction < "global" , undefined > ;
314317 } ,
@@ -351,10 +354,8 @@ export function localizeDurationLargestUnit(
351354export function localizeRelativeInstant (
352355 instant : Temporal . Instant | Timestamp . AsObject ,
353356 locale : string ,
354- options ?: {
357+ options ?: LocalizeRelativeTimeOptions & {
355358 relativeTo ?: Temporal . Instant ;
356- style ?: Intl . RelativeTimeFormatStyle ;
357- numeric ?: Intl . RelativeTimeFormatNumeric ;
358359 smallestUnit ?: Temporal . PluralizeUnit < Temporal . TimeUnit > ;
359360 t ?: TFunction < "global" , undefined > ;
360361 } ,
0 commit comments