Skip to content

Commit

Permalink
fix: load intl polyfill locale data
Browse files Browse the repository at this point in the history
  • Loading branch information
limpbrains committed Dec 6, 2024
1 parent a195eb7 commit 04b58b7
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 6 deletions.
5 changes: 2 additions & 3 deletions shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@ require('./src/polyfills/textdecoder-polyfill');
// RN still doesn't support full spec of Intl API
// Don't remove -force from these because detection is VERY slow on low-end Android.
// https://github.com/formatjs/formatjs/issues/4463#issuecomment-2176070577
// we only load english locale data by default, other locales should be loaded after i18next initialized
if (!Intl.Locale) {
require('@formatjs/intl-locale/polyfill-force');
}
if (!NumberFormat.formatToParts) {
require('@formatjs/intl-numberformat/polyfill-force');
require('@formatjs/intl-numberformat/locale-data/en');
require('@formatjs/intl-numberformat/locale-data/ru');
NumberFormat.polyfilled = true;
}
if (!Intl.PluralRules) {
require('@formatjs/intl-pluralrules/polyfill-force');
require('@formatjs/intl-pluralrules/locale-data/en');
require('@formatjs/intl-pluralrules/locale-data/ru');
}
if (!Intl.RelativeTimeFormat) {
require('@formatjs/intl-relativetimeformat/polyfill-force');
require('@formatjs/intl-relativetimeformat/locale-data/en');
require('@formatjs/intl-relativetimeformat/locale-data/ru');
}

if (!Symbol.asyncIterator) {
Expand Down
30 changes: 27 additions & 3 deletions src/utils/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import ICU from 'i18next-icu';
import { initReactI18next } from 'react-i18next';
import * as RNLocalize from 'react-native-localize';

import { __ENABLE_I18NEXT_DEBUGGER__ } from '../../constants/env';
import locales from './locales';
import { dispatch } from '../../store/helpers';
import { updateUi } from '../../store/slices/ui';
import convert from './convert';
import locales, {
numberFormatPolyfills,
pluralRulesPolyfills,
relativeTimeFormatPolyfills,
} from './locales';

const getDeviceLanguage = (): string => {
const lang =
Expand Down Expand Up @@ -58,7 +62,27 @@ i18nICU
}
}

dispatch(updateUi({ timeZone, language: i18n.language }));
dispatch(updateUi({ timeZone, language: i18nICU.language }));
})
.then(async () => {
// we need to load language related polyfill data
const lang = i18nICU.language;
try {
// @ts-ignore
if (NumberFormat.polyfilled) {
await numberFormatPolyfills[lang]?.();
}
// @ts-ignore
if (Intl.PluralRules.polyfilled) {
await pluralRulesPolyfills[lang]?.();
}
// @ts-ignore
if (Intl.RelativeTimeFormat.polyfilled) {
await relativeTimeFormatPolyfills[lang]?.();
}
} catch (e) {
console.warn('Error loading polyfill for language: ', lang);
}
});

export default i18nICU;
Expand Down
82 changes: 82 additions & 0 deletions src/utils/i18n/locales/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,85 @@ export default {
uk,
yo,
} as const;

export const numberFormatPolyfills = {
arb: (): any => import('@formatjs/intl-numberformat/locale-data/ar'),
ca: (): any => import('@formatjs/intl-numberformat/locale-data/ca'),
cs: (): any => import('@formatjs/intl-numberformat/locale-data/cs'),
de: (): any => import('@formatjs/intl-numberformat/locale-data/de'),
el: (): any => import('@formatjs/intl-numberformat/locale-data/el'),
es: (): any => import('@formatjs/intl-numberformat/locale-data/es'),
'es-ES': (): any => import('@formatjs/intl-numberformat/locale-data/es'),
'es-419': (): any => import('@formatjs/intl-numberformat/locale-data/es-419'),
fa: (): any => import('@formatjs/intl-numberformat/locale-data/fa'),
fr: (): any => import('@formatjs/intl-numberformat/locale-data/fr'),
it: (): any => import('@formatjs/intl-numberformat/locale-data/it'),
ja: (): any => import('@formatjs/intl-numberformat/locale-data/ja'),
ko: (): any => import('@formatjs/intl-numberformat/locale-data/ko'),
nl: (): any => import('@formatjs/intl-numberformat/locale-data/nl'),
no: (): any => import('@formatjs/intl-numberformat/locale-data/no'),
pl: (): any => import('@formatjs/intl-numberformat/locale-data/pl'),
pt: (): any => import('@formatjs/intl-numberformat/locale-data/pt'),
'pt-BR': (): any => import('@formatjs/intl-numberformat/locale-data/pt'),
'pt-PT': (): any => import('@formatjs/intl-numberformat/locale-data/pt-PT'),
ro: (): any => import('@formatjs/intl-numberformat/locale-data/ro'),
ru: (): any => import('@formatjs/intl-numberformat/locale-data/ru'),
uk: (): any => import('@formatjs/intl-numberformat/locale-data/uk'),
yo: (): any => import('@formatjs/intl-numberformat/locale-data/yo'),
};

export const pluralRulesPolyfills = {
arb: (): any => import('@formatjs/intl-pluralrules/locale-data/ar'),
ca: (): any => import('@formatjs/intl-pluralrules/locale-data/ca'),
cs: (): any => import('@formatjs/intl-pluralrules/locale-data/cs'),
de: (): any => import('@formatjs/intl-pluralrules/locale-data/de'),
el: (): any => import('@formatjs/intl-pluralrules/locale-data/el'),
es: (): any => import('@formatjs/intl-pluralrules/locale-data/es'),
'es-ES': (): any => import('@formatjs/intl-pluralrules/locale-data/es'),
'es-419': (): any => import('@formatjs/intl-pluralrules/locale-data/es'),
fa: (): any => import('@formatjs/intl-pluralrules/locale-data/fa'),
fr: (): any => import('@formatjs/intl-pluralrules/locale-data/fr'),
it: (): any => import('@formatjs/intl-pluralrules/locale-data/it'),
ja: (): any => import('@formatjs/intl-pluralrules/locale-data/ja'),
ko: (): any => import('@formatjs/intl-pluralrules/locale-data/ko'),
nl: (): any => import('@formatjs/intl-pluralrules/locale-data/nl'),
no: (): any => import('@formatjs/intl-pluralrules/locale-data/no'),
pl: (): any => import('@formatjs/intl-pluralrules/locale-data/pl'),
pt: (): any => import('@formatjs/intl-pluralrules/locale-data/pt'),
'pt-BR': (): any => import('@formatjs/intl-pluralrules/locale-data/pt'),
'pt-PT': (): any => import('@formatjs/intl-pluralrules/locale-data/pt-PT'),
ro: (): any => import('@formatjs/intl-pluralrules/locale-data/ro'),
ru: (): any => import('@formatjs/intl-pluralrules/locale-data/ru'),
uk: (): any => import('@formatjs/intl-pluralrules/locale-data/uk'),
yo: (): any => import('@formatjs/intl-pluralrules/locale-data/yo'),
};

export const relativeTimeFormatPolyfills = {
arb: (): any => import('@formatjs/intl-relativetimeformat/locale-data/ar'),
ca: (): any => import('@formatjs/intl-relativetimeformat/locale-data/ca'),
cs: (): any => import('@formatjs/intl-relativetimeformat/locale-data/cs'),
de: (): any => import('@formatjs/intl-relativetimeformat/locale-data/de'),
el: (): any => import('@formatjs/intl-relativetimeformat/locale-data/el'),
es: (): any => import('@formatjs/intl-relativetimeformat/locale-data/es'),
'es-ES': (): any =>
import('@formatjs/intl-relativetimeformat/locale-data/es'),
'es-419': (): any =>
import('@formatjs/intl-relativetimeformat/locale-data/es-419'),
fa: (): any => import('@formatjs/intl-relativetimeformat/locale-data/fa'),
fr: (): any => import('@formatjs/intl-relativetimeformat/locale-data/fr'),
it: (): any => import('@formatjs/intl-relativetimeformat/locale-data/it'),
ja: (): any => import('@formatjs/intl-relativetimeformat/locale-data/ja'),
ko: (): any => import('@formatjs/intl-relativetimeformat/locale-data/ko'),
nl: (): any => import('@formatjs/intl-relativetimeformat/locale-data/nl'),
no: (): any => import('@formatjs/intl-relativetimeformat/locale-data/no'),
pl: (): any => import('@formatjs/intl-relativetimeformat/locale-data/pl'),
pt: (): any => import('@formatjs/intl-relativetimeformat/locale-data/pt'),
'pt-BR': (): any =>
import('@formatjs/intl-relativetimeformat/locale-data/pt'),
'pt-PT': (): any =>
import('@formatjs/intl-relativetimeformat/locale-data/pt-PT'),
ro: (): any => import('@formatjs/intl-relativetimeformat/locale-data/ro'),
ru: (): any => import('@formatjs/intl-relativetimeformat/locale-data/ru'),
uk: (): any => import('@formatjs/intl-relativetimeformat/locale-data/uk'),
yo: (): any => import('@formatjs/intl-relativetimeformat/locale-data/yo'),
};

0 comments on commit 04b58b7

Please sign in to comment.