Skip to main content

Intl.RelativeTimeFormat

A spec-compliant polyfill for Intl.RelativeTimeFormat fully tested by the official ECMAScript Conformance test suite

npm Version size

Installation#

npm i @formatjs/intl-relativetimeformat

Requirements#

This package requires the following capabilities:

Usage#

Via polyfill.io#

You can use polyfill.io URL Builder to create a polyfill script tag for Intl.RelativeTimeFormat. By default the created URL does not come with any locale data. In order to add locale data, append Intl.RelativeTimeFormat.~locale.<locale> to your list of features. For example:

<!-- Polyfill Intl.RelativeTimeFormat, its dependencies & `en` locale data -->
<script src="https://polyfill.io/v3/polyfill.min.js?features=Intl.RelativeTimeFormat,Intl.RelativeTimeFormat.~locale.en"></script>

Simple#

import '@formatjs/intl-relativetimeformat/polyfill'
import '@formatjs/intl-relativetimeformat/locale-data/en' // locale-data for en

Dynamic import + capability detection#

import {shouldPolyfill} from '@formatjs/intl-relativetimeformat/should-polyfill'
async function polyfill(locale: string) {
if (shouldPolyfill()) {
// Load the polyfill 1st BEFORE loading data
await import('@formatjs/intl-relativetimeformat/polyfill')
}
if (Intl.RelativeTimeFormat.polyfilled) {
switch (locale) {
default:
await import('@formatjs/intl-relativetimeformat/locale-data/en')
break
case 'fr':
await import('@formatjs/intl-relativetimeformat/locale-data/fr')
break
}
}
}

Tests#

This library is fully test262-compliant.