1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | type IntlConstructor = {
|
8 | supportedLocalesOf(locales: string | string[], options?: any): string[];
|
9 | };
|
10 |
|
11 | export default function areIntlLocalesSupported(
|
12 | locales: string | string[],
|
13 | constructorsToCheck?: Array<IntlConstructor>
|
14 | ): boolean {
|
15 | if (typeof Intl === 'undefined') {
|
16 | return false;
|
17 | }
|
18 |
|
19 | if (!locales) {
|
20 | throw new Error('locales must be supplied.');
|
21 | }
|
22 |
|
23 | if (!Array.isArray(locales)) {
|
24 | locales = [locales];
|
25 | }
|
26 |
|
27 | let intlConstructors = constructorsToCheck || [
|
28 | Intl.DateTimeFormat,
|
29 | Intl.NumberFormat,
|
30 | Intl.PluralRules,
|
31 | ];
|
32 |
|
33 | intlConstructors = intlConstructors.filter(Boolean);
|
34 |
|
35 | if (
|
36 | intlConstructors.length === 0 ||
|
37 | (constructorsToCheck &&
|
38 | intlConstructors.length !== constructorsToCheck.length)
|
39 | ) {
|
40 | return false;
|
41 | }
|
42 |
|
43 | return intlConstructors.every(
|
44 | intlConstructor =>
|
45 | intlConstructor.supportedLocalesOf(locales).length === locales.length
|
46 | );
|
47 | }
|