dify/web/app/components/i18n.tsx

47 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2023-05-15 08:51:32 +08:00
'use client'
import type { FC } from 'react'
import React, { useEffect, useState } from 'react'
2023-05-15 08:51:32 +08:00
import I18NContext from '@/context/i18n'
import type { Locale } from '@/i18n-config'
import { setLocaleOnClient } from '@/i18n-config'
import Loading from './base/loading'
import { usePrefetchQuery } from '@tanstack/react-query'
import { getSystemFeatures } from '@/service/common'
2023-05-15 08:51:32 +08:00
export type II18nProps = {
locale: Locale
children: React.ReactNode
}
const I18n: FC<II18nProps> = ({
2023-10-18 16:00:56 +08:00
locale,
2023-05-15 08:51:32 +08:00
children,
}) => {
const [loading, setLoading] = useState(true)
usePrefetchQuery({
queryKey: ['systemFeatures'],
queryFn: getSystemFeatures,
})
2023-10-18 16:00:56 +08:00
useEffect(() => {
setLocaleOnClient(locale, false).then(() => {
setLoading(false)
})
2023-10-18 16:00:56 +08:00
}, [locale])
if (loading)
return <div className='flex h-screen w-screen items-center justify-center'><Loading type='app' /></div>
2023-05-15 08:51:32 +08:00
return (
<I18NContext.Provider value={{
locale,
2024-01-11 11:08:32 +08:00
i18n: {},
2023-05-15 08:51:32 +08:00
setLocaleOnClient,
}}>
{children}
</I18NContext.Provider>
)
}
export default React.memo(I18n)