2023-11-04 07:32:37 +01:00
|
|
|
import en from '../../locales/en/translation.json'
|
2023-12-04 17:08:12 +01:00
|
|
|
import ja from '../../locales/ja/translation.json'
|
2024-02-05 13:52:13 +01:00
|
|
|
import pt_pt from '../../locales/pt_pt/translation.json'
|
2024-04-09 16:28:16 +02:00
|
|
|
import zh_tw from '../../locales/zh_tw/translation.json'
|
2024-01-29 16:10:44 +01:00
|
|
|
import { flattenMessages } from '../utils/flattenMessage'
|
2023-11-04 07:32:37 +01:00
|
|
|
import { createContext, useState } from 'react'
|
|
|
|
import { IntlProvider } from 'react-intl'
|
|
|
|
|
2024-04-09 16:28:16 +02:00
|
|
|
export type localeType = 'en' | 'ja' | 'pt-PT' | 'zh-TW'
|
2023-11-04 07:32:37 +01:00
|
|
|
|
|
|
|
type Props = {
|
2023-12-27 14:49:59 +01:00
|
|
|
children: React.ReactNode
|
2023-11-04 07:32:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Lang {
|
|
|
|
switchLang(lang: string): void
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Context = createContext<Lang>({} as Lang)
|
|
|
|
|
|
|
|
export const IntlProviderWrapper: React.FC<Props> = props => {
|
2023-12-04 17:08:12 +01:00
|
|
|
const langs = [
|
|
|
|
{ locale: 'en', messages: flattenMessages(en) },
|
2024-02-05 13:52:13 +01:00
|
|
|
{ locale: 'ja', messages: flattenMessages(ja) },
|
2024-04-09 16:28:16 +02:00
|
|
|
{ locale: 'pt-PT', messages: flattenMessages(pt_pt) },
|
|
|
|
{ locale: 'zh-TW', messages: flattenMessages(zh_tw) }
|
2023-12-04 17:08:12 +01:00
|
|
|
]
|
2023-11-04 07:32:37 +01:00
|
|
|
const [lang, setLang] = useState(langs[0])
|
|
|
|
|
|
|
|
const switchLang = (locale: string) => {
|
|
|
|
const changeLang = langs.find(lang => lang.locale === locale)
|
|
|
|
if (changeLang == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setLang(changeLang)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Context.Provider value={{ switchLang }}>
|
|
|
|
<IntlProvider {...lang} defaultLocale="en" fallbackOnEmptyString={true}>
|
|
|
|
{props.children}
|
|
|
|
</IntlProvider>
|
|
|
|
</Context.Provider>
|
|
|
|
)
|
|
|
|
}
|