Intl.RelativeTimeFormat
Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →
Un polyfill compatible con la especificación para Intl.RelativeTimeFormat, probado exhaustivamente por la suite oficial de pruebas de conformidad ECMAScript
Instalación
- npm
- yarn
npm i @formatjs/intl-relativetimeformat
yarn add @formatjs/intl-relativetimeformat
Requisitos
Este paquete requiere las siguientes capacidades:
-
Si necesitas
formatToPartsy debes dar soporte a IE11- o Node 10-, necesitarás implementar un polyfill usando@formatjs/intl-numberformat.
Uso
Mediante polyfill-fastly.io
Puedes usar el Constructor de URLs de polyfill-fastly.io para crear una etiqueta de script de polyfill para Intl.RelativeTimeFormat. Por defecto, la URL generada no incluye datos de localización. Para añadir datos de localización, agrega Intl.RelativeTimeFormat.~locale.<locale> a tu lista de características. Por ejemplo:
<!-- Polyfill Intl.RelativeTimeFormat, its dependencies & `en` locale data -->
<script src="https://polyfill-fastly.io/v3/polyfill.min.js?features=Intl.RelativeTimeFormat,Intl.RelativeTimeFormat.~locale.en"></script>
Sencillo
import '@formatjs/intl-relativetimeformat/polyfill'
import '@formatjs/intl-relativetimeformat/locale-data/en' // locale-data for en
Importación dinámica + detección de capacidades
import {shouldPolyfill} from '@formatjs/intl-relativetimeformat/should-polyfill'
async function polyfill(locale: string) {
const unsupportedLocale = shouldPolyfill(locale)
// This locale is supported
if (!unsupportedLocale) {
return
}
// Load the polyfill 1st BEFORE loading data
await import('@formatjs/intl-relativetimeformat/polyfill-force')
await import(
`@formatjs/intl-relativetimeformat/locale-data/${unsupportedLocale}`
)
}
Pruebas
Esta biblioteca cumple totalmente con los estándares test262.