From f3f60bf3305a95b7de73a536eef29bb532cf20e2 Mon Sep 17 00:00:00 2001 From: Adriano Raiano Date: Wed, 6 Dec 2023 23:32:59 +0100 Subject: [PATCH] try to fix for turbo #2222 --- package-lock.json | 14 +++++----- package.json | 2 +- src/appWithTranslation.client.test.tsx | 14 ++++++---- src/appWithTranslation.tsx | 38 ++++++++++++++++---------- 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/package-lock.json b/package-lock.json index bbf388cc..c4a7c872 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,7 +27,7 @@ "@types/hoist-non-react-statics": "^3.3.4", "core-js": "^3", "hoist-non-react-statics": "^3.3.2", - "i18next-fs-backend": "^2.3.0" + "i18next-fs-backend": "^2.3.1" }, "devDependencies": { "@babel/cli": "7.23.0", @@ -10394,9 +10394,9 @@ } }, "node_modules/i18next-fs-backend": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/i18next-fs-backend/-/i18next-fs-backend-2.3.0.tgz", - "integrity": "sha512-N0SS2WojoVIh2x/QkajSps8RPKzXqryZsQh12VoFY4cLZgkD+62EPY2fY+ZjkNADu8xA5I5EadQQXa8TXBKN3w==" + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/i18next-fs-backend/-/i18next-fs-backend-2.3.1.tgz", + "integrity": "sha512-tvfXskmG/9o+TJ5Fxu54sSO5OkY6d+uMn+K6JiUGLJrwxAVfer+8V3nU8jq3ts9Pe5lXJv4b1N7foIjJ8Iy2Gg==" }, "node_modules/iconv-lite": { "version": "0.4.24", @@ -26446,9 +26446,9 @@ } }, "i18next-fs-backend": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/i18next-fs-backend/-/i18next-fs-backend-2.3.0.tgz", - "integrity": "sha512-N0SS2WojoVIh2x/QkajSps8RPKzXqryZsQh12VoFY4cLZgkD+62EPY2fY+ZjkNADu8xA5I5EadQQXa8TXBKN3w==" + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/i18next-fs-backend/-/i18next-fs-backend-2.3.1.tgz", + "integrity": "sha512-tvfXskmG/9o+TJ5Fxu54sSO5OkY6d+uMn+K6JiUGLJrwxAVfer+8V3nU8jq3ts9Pe5lXJv4b1N7foIjJ8Iy2Gg==" }, "iconv-lite": { "version": "0.4.24", diff --git a/package.json b/package.json index 8d9090f6..18fb9776 100644 --- a/package.json +++ b/package.json @@ -134,7 +134,7 @@ "@types/hoist-non-react-statics": "^3.3.4", "core-js": "^3", "hoist-non-react-statics": "^3.3.2", - "i18next-fs-backend": "^2.3.0" + "i18next-fs-backend": "^2.3.1" }, "peerDependencies": { "i18next": "^23.7.7", diff --git a/src/appWithTranslation.client.test.tsx b/src/appWithTranslation.client.test.tsx index 4b69907a..5125d7ff 100644 --- a/src/appWithTranslation.client.test.tsx +++ b/src/appWithTranslation.client.test.tsx @@ -119,8 +119,10 @@ describe('appWithTranslation', () => { locales: ['en', 'de'], }, resources: { - xyz: { - custom: 'resources', + en: { + xyz: { + custom: 'resources', + }, }, }, } as any @@ -129,9 +131,11 @@ describe('appWithTranslation', () => { const [args] = (I18nextProvider as jest.Mock).mock.calls expect(args[0].i18n.options.resources).toMatchObject({ - xyz: { - custom: 'resources', - }, + en: { + xyz: { + custom: 'resources', + }, + } }) }) diff --git a/src/appWithTranslation.tsx b/src/appWithTranslation.tsx index 3b4c1190..fa4e5c64 100644 --- a/src/appWithTranslation.tsx +++ b/src/appWithTranslation.tsx @@ -8,7 +8,7 @@ import createClient from './createClient' import { SSRConfig, UserConfig } from './types' -import { i18n as I18NextClient } from 'i18next' +import { i18n as I18NextClient, Resource } from 'i18next' import { useIsomorphicLayoutEffect } from './utils' export { Trans, @@ -18,6 +18,26 @@ export { export let globalI18n: I18NextClient | null = null +const addResourcesToI18next = (instance: I18NextClient, resources: Resource) => { + if (resources && instance.isInitialized) { + for (const locale of Object.keys(resources)) { + for (const ns of Object.keys(resources[locale])) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + if (instance.hasLoadedNamespace(ns, { lng: locale })) { + instance.addResourceBundle( + locale, + ns, + resources[locale][ns], + true, + true + ) + } + } + } + } +} + export const appWithTranslation = ( WrappedComponent: React.ComponentType, configOverride: UserConfig | null = null @@ -69,19 +89,7 @@ export const appWithTranslation = ( let instance = instanceRef.current if (instance) { - if (resources) { - for (const locale of Object.keys(resources)) { - for (const ns of Object.keys(resources[locale])) { - instance.addResourceBundle( - locale, - ns, - resources[locale][ns], - true, - true - ) - } - } - } + addResourcesToI18next(instance, resources) } else { instance = createClient({ ...createConfig({ @@ -93,6 +101,8 @@ export const appWithTranslation = ( resources, }).i18n + addResourcesToI18next(instance, resources) + globalI18n = instance instanceRef.current = instance }