Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Rendering pages on browser when Instance i18n finished initiali… #2306

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/appWithTranslation.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo, useRef } from 'react'
import React, { useMemo, useState, useRef } from 'react'
import hoistNonReactStatics from 'hoist-non-react-statics'
import { I18nextProvider } from 'react-i18next'
import type { AppProps as NextJsAppProps } from 'next/app'
Expand All @@ -18,11 +18,18 @@ export {

export let globalI18n: I18NextClient | null = null

const addResourcesToI18next = (instance: I18NextClient, resources: Resource) => {
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])) {
if (!instance?.store?.data || !instance.store.data[locale] || !instance.store.data[locale][ns]) {
if (
!instance?.store?.data ||
!instance.store.data[locale] ||
!instance.store.data[locale][ns]
) {
instance.addResourceBundle(
locale,
ns,
Expand All @@ -49,6 +56,9 @@ export const appWithTranslation = <Props extends NextJsAppProps>(
const ns = _nextI18Next?.ns

const instanceRef = useRef<I18NextClient | null>(null)
const [ready, setReady] = useState<boolean>(
!configOverride?.clientAwaitInit
)

/**
* Memoize i18n instance and reuse it rather than creating new instance.
Expand Down Expand Up @@ -89,15 +99,21 @@ export const appWithTranslation = <Props extends NextJsAppProps>(
if (instance) {
addResourcesToI18next(instance, resources)
} else {
instance = createClient({
const { i18n: ins, initPromise } = createClient({
...createConfig({
...userConfig,
lng: locale,
}),
lng: locale,
...(ns && { ns }),
resources,
}).i18n
})

instance = ins

if (configOverride?.clientAwaitInit) {
initPromise.then(() => setReady(true))
}

addResourcesToI18next(instance, resources)

Expand All @@ -117,7 +133,7 @@ export const appWithTranslation = <Props extends NextJsAppProps>(
i18n.changeLanguage(locale)
}, [i18n, locale])

return i18n !== null ? (
return i18n !== null && ready ? (
<I18nextProvider i18n={i18n}>
<WrappedComponent {...props} />
</I18nextProvider>
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
reloadOnPrerender?: boolean
serializeConfig?: boolean
use?: any[]
clientAwaitInit?: boolean

Check failure on line 52 in src/types.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

Expected interface keys to be in ascending order. 'clientAwaitInit' should be before 'use'
} & InitOptions

export type InternalConfig = Omit<UserConfig, 'i18n'> &
Expand Down
Loading