From 266730409d519020891498f6cb4805cf32301238 Mon Sep 17 00:00:00 2001 From: Emmanuel Chambon Date: Thu, 16 Dec 2021 19:36:38 +0100 Subject: [PATCH 1/3] feat(use-i18n): allow default date-fns locale instead of lazy loading --- packages/use-i18n/src/index.ts | 4 ++-- packages/use-i18n/src/usei18n.tsx | 26 +++++++++++--------------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/packages/use-i18n/src/index.ts b/packages/use-i18n/src/index.ts index 274eb1be0..1a2e86589 100644 --- a/packages/use-i18n/src/index.ts +++ b/packages/use-i18n/src/index.ts @@ -1,5 +1,5 @@ -import usei18n from './usei18n' +import I18nContextProvider from './usei18n' export * from './usei18n' -export default usei18n +export default I18nContextProvider diff --git a/packages/use-i18n/src/usei18n.tsx b/packages/use-i18n/src/usei18n.tsx index e7d45c883..54a925711 100644 --- a/packages/use-i18n/src/usei18n.tsx +++ b/packages/use-i18n/src/usei18n.tsx @@ -120,16 +120,18 @@ const I18nContextProvider = ({ children, defaultLoad, loadDateLocale, + defaultDateLocale, defaultLocale, - defaultTranslations, - enableDefaultLocale, - enableDebugKey, - localeItemStorage, + defaultTranslations = {}, + enableDefaultLocale = false, + enableDebugKey = false, + localeItemStorage = LOCALE_ITEM_STORAGE, supportedLocales, }: { children: ReactNode, defaultLoad: LoadTranslationsFn, - loadDateLocale: LoadLocaleFn, + loadDateLocale?: LoadLocaleFn, + defaultDateLocale?: Locale, defaultLocale: string, defaultTranslations: TranslationsByLocales, enableDefaultLocale: boolean, @@ -142,10 +144,10 @@ const I18nContextProvider = ({ ) const [translations, setTranslations] = useState(defaultTranslations) const [namespaces, setNamespaces] = useState([]) - const [dateFnsLocale, setDateFnsLocale] = useState() + const [dateFnsLocale, setDateFnsLocale] = useState(defaultDateLocale ?? {}) useEffect(() => { - loadDateLocale(currentLocale === 'en' ? 'en-GB' : currentLocale) + loadDateLocale?.(currentLocale === 'en' ? 'en-GB' : currentLocale) .then(setDateFnsLocale) .catch(() => loadDateLocale('en-GB').then(setDateFnsLocale)) }, [loadDateLocale, currentLocale]) @@ -340,13 +342,6 @@ const I18nContextProvider = ({ return {children} } -I18nContextProvider.defaultProps = { - defaultTranslations: {}, - enableDebugKey: false, - enableDefaultLocale: false, - localeItemStorage: LOCALE_ITEM_STORAGE, -} - I18nContextProvider.propTypes = { children: PropTypes.node.isRequired, defaultLoad: PropTypes.func.isRequired, @@ -354,7 +349,8 @@ I18nContextProvider.propTypes = { defaultTranslations: PropTypes.shape({}), enableDebugKey: PropTypes.bool, enableDefaultLocale: PropTypes.bool, - loadDateLocale: PropTypes.func.isRequired, + loadDateLocale: PropTypes.func, + defaultDateLocale: PropTypes.shape({}), localeItemStorage: PropTypes.string, supportedLocales: PropTypes.arrayOf(PropTypes.string).isRequired, } From 2d7de8e47d24a9413189d2cebefaa949a2c02cd6 Mon Sep 17 00:00:00 2001 From: Emmanuel Chambon Date: Thu, 16 Dec 2021 19:40:19 +0100 Subject: [PATCH 2/3] fix: sort --- packages/use-i18n/src/usei18n.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/use-i18n/src/usei18n.tsx b/packages/use-i18n/src/usei18n.tsx index 54a925711..986a7bfa6 100644 --- a/packages/use-i18n/src/usei18n.tsx +++ b/packages/use-i18n/src/usei18n.tsx @@ -344,13 +344,13 @@ const I18nContextProvider = ({ I18nContextProvider.propTypes = { children: PropTypes.node.isRequired, + defaultDateLocale: PropTypes.shape({}), defaultLoad: PropTypes.func.isRequired, defaultLocale: PropTypes.string.isRequired, defaultTranslations: PropTypes.shape({}), enableDebugKey: PropTypes.bool, enableDefaultLocale: PropTypes.bool, loadDateLocale: PropTypes.func, - defaultDateLocale: PropTypes.shape({}), localeItemStorage: PropTypes.string, supportedLocales: PropTypes.arrayOf(PropTypes.string).isRequired, } From e5cad9bf87f21c3d36722712acf3278154db0336 Mon Sep 17 00:00:00 2001 From: Emmanuel Chambon Date: Mon, 20 Dec 2021 12:45:59 +0100 Subject: [PATCH 3/3] fix: correct tests --- packages/use-i18n/src/__tests__/usei18n.tsx | 6 +++--- packages/use-i18n/src/usei18n.tsx | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/use-i18n/src/__tests__/usei18n.tsx b/packages/use-i18n/src/__tests__/usei18n.tsx index 8ffb5cc31..c2c874f33 100644 --- a/packages/use-i18n/src/__tests__/usei18n.tsx +++ b/packages/use-i18n/src/__tests__/usei18n.tsx @@ -186,7 +186,7 @@ describe('i18n hook', () => { jest.spyOn(window, 'navigator', 'get').mockImplementation(() => ({ language: 'en-US', languages: ['en-US', 'en'], - })) + }) as unknown as Navigator) const { result, waitForNextUpdate } = renderHook(() => useI18n(), { wrapper: wrapper({ defaultLocale: 'fr', @@ -201,7 +201,7 @@ describe('i18n hook', () => { jest.spyOn(window, 'navigator', 'get').mockImplementation(() => ({ language: 'en', languages: undefined, - })) + }) as unknown as Navigator) const { result, waitForNextUpdate } = renderHook(() => useI18n(), { wrapper: wrapper({ defaultLocale: 'fr', @@ -278,7 +278,7 @@ describe('i18n hook', () => { }), }) await waitForNextUpdate() - const identiqueTranslate = result.current.namespaceTranslation() + const identiqueTranslate = result.current.namespaceTranslation('') expect(identiqueTranslate('title')).toEqual(result.current.t('title')) const translate = result.current.namespaceTranslation('tests.test') diff --git a/packages/use-i18n/src/usei18n.tsx b/packages/use-i18n/src/usei18n.tsx index 986a7bfa6..b6759cd64 100644 --- a/packages/use-i18n/src/usei18n.tsx +++ b/packages/use-i18n/src/usei18n.tsx @@ -144,7 +144,7 @@ const I18nContextProvider = ({ ) const [translations, setTranslations] = useState(defaultTranslations) const [namespaces, setNamespaces] = useState([]) - const [dateFnsLocale, setDateFnsLocale] = useState(defaultDateLocale ?? {}) + const [dateFnsLocale, setDateFnsLocale] = useState(defaultDateLocale ?? undefined) useEffect(() => { loadDateLocale?.(currentLocale === 'en' ? 'en-GB' : currentLocale)