|
1
|
import { I18nProvider as Provider } from "@medusajs/ui" |
|
2
|
import { PropsWithChildren, useEffect } from "react" |
|
3
|
import { useTranslation } from "react-i18next" |
|
4
|
import { languages } from "../../i18n/languages" |
|
5
|
|
|
6
|
type I18nProviderProps = PropsWithChildren |
|
7
|
|
|
8
|
const formatLocaleCode = (code: string) => { |
|
9
|
return code.replace(/([a-z])([A-Z])/g, "$1-$2") |
|
10
|
} |
|
11
|
|
|
12
|
export const I18nProvider = ({ children }: I18nProviderProps) => { |
|
13
|
const { i18n } = useTranslation() |
|
14
|
|
|
15
|
const currentLanguage = |
|
16
|
languages.find((lan) => lan.code === i18n.language) || languages[0] |
|
17
|
const locale = currentLanguage.code |
|
18
|
const direction = currentLanguage.ltr ? "ltr" : "rtl" |
|
19
|
|
|
20
|
useEffect(() => { |
|
21
|
document.documentElement.setAttribute("dir", direction) |
|
22
|
}, [direction]) |
|
23
|
|
|
24
|
useEffect(() => { |
|
25
|
document.documentElement.setAttribute("lang", formatLocaleCode(locale)) |
|
26
|
}, [locale]) |
|
27
|
|
|
28
|
return <Provider locale={formatLocaleCode(locale)}>{children}</Provider> |
|
29
|
} |