Skip to content

Commit f3f60bf

Browse files
committed
try to fix for turbo #2222
1 parent 343872a commit f3f60bf

File tree

4 files changed

+41
-27
lines changed

4 files changed

+41
-27
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
"@types/hoist-non-react-statics": "^3.3.4",
135135
"core-js": "^3",
136136
"hoist-non-react-statics": "^3.3.2",
137-
"i18next-fs-backend": "^2.3.0"
137+
"i18next-fs-backend": "^2.3.1"
138138
},
139139
"peerDependencies": {
140140
"i18next": "^23.7.7",

src/appWithTranslation.client.test.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ describe('appWithTranslation', () => {
119119
locales: ['en', 'de'],
120120
},
121121
resources: {
122-
xyz: {
123-
custom: 'resources',
122+
en: {
123+
xyz: {
124+
custom: 'resources',
125+
},
124126
},
125127
},
126128
} as any
@@ -129,9 +131,11 @@ describe('appWithTranslation', () => {
129131
const [args] = (I18nextProvider as jest.Mock).mock.calls
130132

131133
expect(args[0].i18n.options.resources).toMatchObject({
132-
xyz: {
133-
custom: 'resources',
134-
},
134+
en: {
135+
xyz: {
136+
custom: 'resources',
137+
},
138+
}
135139
})
136140
})
137141

src/appWithTranslation.tsx

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import createClient from './createClient'
88

99
import { SSRConfig, UserConfig } from './types'
1010

11-
import { i18n as I18NextClient } from 'i18next'
11+
import { i18n as I18NextClient, Resource } from 'i18next'
1212
import { useIsomorphicLayoutEffect } from './utils'
1313
export {
1414
Trans,
@@ -18,6 +18,26 @@ export {
1818

1919
export let globalI18n: I18NextClient | null = null
2020

21+
const addResourcesToI18next = (instance: I18NextClient, resources: Resource) => {
22+
if (resources && instance.isInitialized) {
23+
for (const locale of Object.keys(resources)) {
24+
for (const ns of Object.keys(resources[locale])) {
25+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
26+
// @ts-ignore
27+
if (instance.hasLoadedNamespace(ns, { lng: locale })) {
28+
instance.addResourceBundle(
29+
locale,
30+
ns,
31+
resources[locale][ns],
32+
true,
33+
true
34+
)
35+
}
36+
}
37+
}
38+
}
39+
}
40+
2141
export const appWithTranslation = <Props extends NextJsAppProps>(
2242
WrappedComponent: React.ComponentType<Props>,
2343
configOverride: UserConfig | null = null
@@ -69,19 +89,7 @@ export const appWithTranslation = <Props extends NextJsAppProps>(
6989

7090
let instance = instanceRef.current
7191
if (instance) {
72-
if (resources) {
73-
for (const locale of Object.keys(resources)) {
74-
for (const ns of Object.keys(resources[locale])) {
75-
instance.addResourceBundle(
76-
locale,
77-
ns,
78-
resources[locale][ns],
79-
true,
80-
true
81-
)
82-
}
83-
}
84-
}
92+
addResourcesToI18next(instance, resources)
8593
} else {
8694
instance = createClient({
8795
...createConfig({
@@ -93,6 +101,8 @@ export const appWithTranslation = <Props extends NextJsAppProps>(
93101
resources,
94102
}).i18n
95103

104+
addResourcesToI18next(instance, resources)
105+
96106
globalI18n = instance
97107
instanceRef.current = instance
98108
}

0 commit comments

Comments
 (0)