From c362643fd698cfe58d8782da5acf3e823991ff2f Mon Sep 17 00:00:00 2001 From: twwu Date: Wed, 23 Jul 2025 10:14:38 +0800 Subject: [PATCH] refactor: update setLocaleOnClient to return a promise and handle async calls in i18n components --- web/app/components/i18n.tsx | 12 ++++++++++-- web/app/signin/invite-settings/page.tsx | 2 +- web/context/i18n.ts | 6 ++++-- web/i18n/index.ts | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/web/app/components/i18n.tsx b/web/app/components/i18n.tsx index f04f8d6cbe..2493e5b5af 100644 --- a/web/app/components/i18n.tsx +++ b/web/app/components/i18n.tsx @@ -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 = ({ locale, children, }) => { + const [loading, setLoading] = useState(true) + useEffect(() => { - setLocaleOnClient(locale, false) + setLocaleOnClient(locale, false).then(() => { + setLoading(false) + }) }, [locale]) + if (loading) + return
+ return ( - setLocaleOnClient: (_lang: Locale, _reloadPage?: boolean) => void + setLocaleOnClient: (_lang: Locale, _reloadPage?: boolean) => Promise } const I18NContext = createContext({ locale: 'en-US', i18n: {}, - setLocaleOnClient: noop, + setLocaleOnClient: async (_lang: Locale, _reloadPage?: boolean) => { + noop() + }, }) export const useI18N = () => useContext(I18NContext) diff --git a/web/i18n/index.ts b/web/i18n/index.ts index 9506e146fa..27ed3022ad 100644 --- a/web/i18n/index.ts +++ b/web/i18n/index.ts @@ -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() }