Skip to content

Commit

Permalink
try to fix for turbo #2222
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Dec 6, 2023
1 parent 343872a commit f3f60bf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 9 additions & 5 deletions src/appWithTranslation.client.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ describe('appWithTranslation', () => {
locales: ['en', 'de'],
},
resources: {
xyz: {
custom: 'resources',
en: {
xyz: {
custom: 'resources',
},
},
},
} as any
Expand All @@ -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',
},
}
})
})

Expand Down
38 changes: 24 additions & 14 deletions src/appWithTranslation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = <Props extends NextJsAppProps>(
WrappedComponent: React.ComponentType<Props>,
configOverride: UserConfig | null = null
Expand Down Expand Up @@ -69,19 +89,7 @@ export const appWithTranslation = <Props extends NextJsAppProps>(

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({
Expand All @@ -93,6 +101,8 @@ export const appWithTranslation = <Props extends NextJsAppProps>(
resources,
}).i18n

addResourcesToI18next(instance, resources)

globalI18n = instance
instanceRef.current = instance
}
Expand Down

0 comments on commit f3f60bf

Please sign in to comment.