refactor: update setLocaleOnClient to return a promise and handle async calls in i18n components

pull/22810/head
twwu 7 months ago
parent c8f0951504
commit c362643fd6

@ -1,10 +1,11 @@
'use client'
import type { FC } from 'react'
import React, { useEffect } from 'react'
import React, { useEffect, useState } from 'react'
import I18NContext from '@/context/i18n'
import type { Locale } from '@/i18n'
import { setLocaleOnClient } from '@/i18n'
import Loading from './base/loading'
export type II18nProps = {
locale: Locale
@ -14,10 +15,17 @@ const I18n: FC<II18nProps> = ({
locale,
children,
}) => {
const [loading, setLoading] = useState(true)
useEffect(() => {
setLocaleOnClient(locale, false)
setLocaleOnClient(locale, false).then(() => {
setLoading(false)
})
}, [locale])
if (loading)
return <div className='flex h-screen w-screen items-center justify-center'><Loading type='app' /></div>
return (
<I18NContext.Provider value={{
locale,

@ -57,7 +57,7 @@ export default function InviteSettingsPage() {
if (res.result === 'success') {
localStorage.setItem('console_token', res.data.access_token)
localStorage.setItem('refresh_token', res.data.refresh_token)
setLocaleOnClient(language, false)
await setLocaleOnClient(language, false)
router.replace('/apps')
}
}

@ -9,13 +9,15 @@ import { noop } from 'lodash-es'
type II18NContext = {
locale: Locale
i18n: Record<string, any>
setLocaleOnClient: (_lang: Locale, _reloadPage?: boolean) => void
setLocaleOnClient: (_lang: Locale, _reloadPage?: boolean) => Promise<void>
}
const I18NContext = createContext<II18NContext>({
locale: 'en-US',
i18n: {},
setLocaleOnClient: noop,
setLocaleOnClient: async (_lang: Locale, _reloadPage?: boolean) => {
noop()
},
})
export const useI18N = () => useContext(I18NContext)

@ -13,7 +13,7 @@ export type Locale = typeof i18n['locales'][number]
export const setLocaleOnClient = async (locale: Locale, reloadPage = true) => {
Cookies.set(LOCALE_COOKIE_NAME, locale, { expires: 365 })
changeLanguage(locale)
await changeLanguage(locale)
reloadPage && location.reload()
}

Loading…
Cancel
Save