Skip to content

Commit 3bdd2b4

Browse files
authored
fix(use-i18n)!: move date-fns dynamic import outside of library scope (#145)
This was causing a SIGSEGV for some reason on our tests BREAKING CHANGE: We now expose loadDateLocale props in the Provider instead of bundling it
1 parent 216ad9e commit 3bdd2b4

File tree

5 files changed

+37
-275
lines changed

5 files changed

+37
-275
lines changed

packages/use-i18n/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ Use of local `variables` and `namespace` to dynamically load locales.
2727
┃ ┗ 📜common.json
2828
```
2929

30-
your loader will be:
30+
your loaders will be:
3131

3232
```js
33-
const load = ({ locale, namespace }) =>
34-
import(`./locales/${locale}/${namespace}`)
33+
const load = ({ locale, namespace }) => import(`./locales/${locale}/${namespace}`)
34+
const loadDateLocale = (locale) => import(`date-fns/locale/${locale}/index`)
3535
```
3636

3737
Inside your app you will need to use useTranslation to load namespace locales.
@@ -44,6 +44,7 @@ import defaultTranslations from './locales/en/common'
4444

4545
const App = () => (
4646
<I18n
47+
loadDateLocale={loadDateLocale}
4748
defaultLocale="en"
4849
supportedLocales={['en']}
4950
defaultTranslations={defaultTranslations}

packages/use-i18n/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"prop-types": "^15.7.2"
3434
},
3535
"peerDependencies": {
36+
"date-fns": "2.x",
3637
"react": "17.x"
3738
},
3839
"devDependencies": {

packages/use-i18n/src/__tests__/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import fr from './locales/fr'
1010
const LOCALE_ITEM_STORAGE = 'locales'
1111

1212
const wrapper = ({
13+
loadDateLocale = async locale => import(`date-fns/locale/${locale}/index`),
1314
defaultLoad = async ({ locale }) => import(`./locales/${locale}`),
1415
defaultLocale = 'en',
1516
defaultTranslations = {},
@@ -19,6 +20,7 @@ const wrapper = ({
1920
supportedLocales = ['en', 'fr', 'es'],
2021
} = {}) => ({ children }) => (
2122
<I18n
23+
loadDateLocale={loadDateLocale}
2224
defaultLoad={defaultLoad}
2325
defaultLocale={defaultLocale}
2426
defaultTranslations={defaultTranslations}

packages/use-i18n/src/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,10 @@ const getNumberFormat = memoizeIntlConstructor(Intl.NumberFormat)
5555
const getDateTimeFormat = memoizeIntlConstructor(Intl.DateTimeFormat)
5656
const getListFormat = memoizeIntlConstructor(Intl.ListFormat)
5757

58-
const loadDateFnsLocale = async locale =>
59-
import(`date-fns/locale/${locale}/index`)
60-
6158
const I18nContextProvider = ({
6259
children,
6360
defaultLoad,
61+
loadDateLocale,
6462
defaultLocale,
6563
defaultTranslations,
6664
enableDefaultLocale,
@@ -204,12 +202,12 @@ const I18nContextProvider = ({
204202
)
205203

206204
useEffect(() => {
207-
loadDateFnsLocale(currentLocale === 'en' ? 'en-GB' : currentLocale)
205+
loadDateLocale(currentLocale === 'en' ? 'en-GB' : currentLocale)
208206
.then(setDateFnsLocale)
209-
.catch(() => loadDateFnsLocale('en-GB').then(setDateFnsLocale))
207+
.catch(() => loadDateLocale('en-GB').then(setDateFnsLocale))
210208

211209
setCurrentLocale(getCurrentLocale())
212-
}, [currentLocale, getCurrentLocale])
210+
}, [loadDateLocale, currentLocale, getCurrentLocale])
213211

214212
const value = useMemo(
215213
() => ({
@@ -261,6 +259,7 @@ I18nContextProvider.defaultProps = {
261259
I18nContextProvider.propTypes = {
262260
children: PropTypes.node.isRequired,
263261
defaultLoad: PropTypes.func.isRequired,
262+
loadDateLocale: PropTypes.func.isRequired,
264263
defaultLocale: PropTypes.string.isRequired,
265264
defaultTranslations: PropTypes.shape({}),
266265
enableDebugKey: PropTypes.bool,

0 commit comments

Comments
 (0)