Whalebird-desktop-client-ma.../renderer/utils/i18n.tsx

43 lines
1.1 KiB
TypeScript
Raw Normal View History

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'
2023-11-04 07:32:37 +01:00
import { flattenMessages } from './flattenMessage'
import { createContext, useState } from 'react'
import { IntlProvider } from 'react-intl'
2023-12-04 17:08:12 +01:00
export type localeType = 'en' | 'ja'
2023-11-04 07:32:37 +01:00
type Props = {
children: React.ReactNode
}
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) },
2023-12-04 17:17:52 +01:00
{ locale: 'ja', messages: flattenMessages(ja) }
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)
2023-12-04 17:17:52 +01:00
console.log(changeLang)
2023-11-04 07:32:37 +01:00
if (changeLang == null) {
return
}
setLang(changeLang)
}
return (
<Context.Provider value={{ switchLang }}>
<IntlProvider {...lang} defaultLocale="en" fallbackOnEmptyString={true}>
{props.children}
</IntlProvider>
</Context.Provider>
)
}