Skip to content

Commit 3ed95fe

Browse files
committed
fix: check PluralRules & RelativeTimeFormat lazily instead
fixes #1487
1 parent 1fdb186 commit 3ed95fe

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
lines changed

examples/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'core-js/stable';
22
import '@formatjs/intl-pluralrules/polyfill';
3-
import '@formatjs/intl-relativetimeformat/polyfill';
43
import * as React from 'react';
54
import * as ReactDOM from 'react-dom';
65
import Timezone from './TimeZone';

src/components/relative.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyrights licensed under the New BSD License.
44
* See the accompanying LICENSE file for terms.
55
*/
6-
6+
import '@formatjs/intl-relativetimeformat/polyfill';
77
import * as React from 'react';
88
import {Context} from './injectIntl';
99
import {FormatRelativeTimeOptions} from '../types';

src/formatters/plural.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ export function formatPlural(
1212
value: Parameters<IntlFormatters['formatPlural']>[0],
1313
options: Parameters<IntlFormatters['formatPlural']>[1] = {}
1414
) {
15+
if (!Intl.PluralRules) {
16+
onError(
17+
createError(`Intl.PluralRules is not available in this environment.
18+
Try polyfilling it using "@formatjs/intl-pluralrules"
19+
`)
20+
);
21+
}
1522
let filteredOptions = filterProps(options, PLURAL_FORMAT_OPTIONS);
1623

1724
try {

src/formatters/relativeTime.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ export function formatRelativeTime(
3939
if (!unit) {
4040
unit = 'second';
4141
}
42+
const RelativeTimeFormat = (Intl as any).RelativeTimeFormat;
43+
if (!RelativeTimeFormat) {
44+
config.onError(
45+
createError(`Intl.RelativeTimeFormat is not available in this environment.
46+
Try polyfilling it using "@formatjs/intl-relativetimeformat"
47+
`)
48+
);
49+
}
4250
try {
4351
return getFormatter(config, getRelativeTimeFormat, options).format(
4452
value,

src/utils.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,6 @@ export function createIntlCache(): IntlCache {
112112
*/
113113
export function createFormatters(cache: IntlCache = createIntlCache()) {
114114
const RelativeTimeFormat = (Intl as any).RelativeTimeFormat;
115-
if (!RelativeTimeFormat) {
116-
throw new Error(`Intl.RelativeTimeFormat is not available in this environment.
117-
Try polyfilling it using "@formatjs/intl-relativetimeformat"
118-
`);
119-
}
120-
if (!Intl.PluralRules) {
121-
throw new Error(`Intl.PluralRules is not available in this environment.
122-
Try polyfilling it using "@formatjs/intl-pluralrules"
123-
`);
124-
}
125115
return {
126116
getDateTimeFormat: memoizeIntlConstructor(
127117
Intl.DateTimeFormat,

0 commit comments

Comments
 (0)