-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import i18n, { type i18n as I18nInstance } from 'i18next' | ||
import LanguageDetector from 'i18next-browser-languagedetector' | ||
import HttpBackend from 'i18next-http-backend' | ||
import { initReactI18next } from 'react-i18next' | ||
|
||
export const initI18n = async ( | ||
localeUrl: string | undefined | ||
): Promise<I18nInstance> => { | ||
await i18n | ||
.use(HttpBackend) | ||
.use(LanguageDetector) | ||
.use(initReactI18next) | ||
.init({ | ||
backend: { | ||
loadPath: | ||
// TODO: Define the path to the i18n public files | ||
localeUrl ?? 'https://cdn.commercelayer.io/i18n/{{lng}}.json' | ||
}, | ||
fallbackLng: 'en', | ||
interpolation: { | ||
escapeValue: false | ||
}, | ||
react: { | ||
useSuspense: true | ||
} | ||
}) | ||
return i18n | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { initI18n } from '#helpers/i18n' | ||
import { type i18n as I18nInstance } from 'i18next' | ||
import React, { type ReactNode, useEffect, useState } from 'react' | ||
import { I18nextProvider } from 'react-i18next' | ||
|
||
interface I18NProviderProps { | ||
localeUrl?: string | ||
children: ReactNode | ||
} | ||
|
||
export const I18NProvider: React.FC<I18NProviderProps> = ({ | ||
localeUrl, | ||
children | ||
}) => { | ||
const [i18nInstance, setI18nInstance] = useState<I18nInstance | undefined>() | ||
|
||
useEffect(() => { | ||
const setupI18n = async (): Promise<void> => { | ||
try { | ||
const instance = await initI18n(localeUrl) | ||
setI18nInstance(instance) | ||
} catch (error) { | ||
console.error('Error initializing i18n:', error) | ||
} | ||
} | ||
|
||
void setupI18n() | ||
}, [localeUrl]) | ||
|
||
if (i18nInstance == null) { | ||
return <div>{children}</div> | ||
} | ||
|
||
return <I18nextProvider i18n={i18nInstance}>{children}</I18nextProvider> | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.