Skip to content

Commit

Permalink
fix: i18n persist
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Sep 19, 2024
1 parent 9ead475 commit 18e0353
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 29 deletions.
1 change: 1 addition & 0 deletions apps/renderer/src/constants/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getStorageNS } from "~/lib/ns"
export const FEED_COLLECTION_LIST = "collections"
/// Local storage keys
export const QUERY_PERSIST_KEY = getStorageNS("REACT_QUERY_OFFLINE_CACHE")
export const I18N_LOCALE_KEY = getStorageNS("I18N_LOCALE")

/// Route Keys
export const ROUTE_FEED_PENDING = "all"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ export const TransactionsSection = () => {
<Table>
<TableHeader>
<TableRow className="[&_*]:!font-semibold">
<TableHead className="w-16 text-center" size="sm">
<TableHead className="min-w-16 whitespace-nowrap text-center" size="sm">
{t("wallet.transactions.type")}
</TableHead>
<TableHead className="text-center" size="sm">
<TableHead className="whitespace-nowrap text-center" size="sm">
{t("wallet.transactions.amount")}
</TableHead>
<TableHead className="pl-8" size="sm">
<TableHead className="whitespace-nowrap pl-8" size="sm">
{t("wallet.transactions.from")}
</TableHead>
<TableHead className="pl-8" size="sm">
<TableHead className="whitespace-nowrap pl-8" size="sm">
{t("wallet.transactions.to")}
</TableHead>
<TableHead className="pl-6" size="sm">
<TableHead className="whitespace-nowrap pl-6" size="sm">
{t("wallet.transactions.date")}
</TableHead>
<TableHead className="pl-6" size="sm">
<TableHead className="whitespace-nowrap pl-6" size="sm">
{t("wallet.transactions.tx")}
</TableHead>
</TableRow>
Expand Down
30 changes: 7 additions & 23 deletions apps/renderer/src/providers/i18n-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import { I18nextProvider } from "react-i18next"

import { currentSupportedLanguages } from "~/@types/constants"
import { getGeneralSettings, setGeneralSetting } from "~/atoms/settings/general"
import { i18nAtom, langChain } from "~/i18n"
import { I18N_LOCALE_KEY } from "~/constants"
import { i18nAtom } from "~/i18n"
import { EventBus } from "~/lib/event-bus"
import { loadLanguageAndApply } from "~/lib/load-language"
import { appLog } from "~/lib/log"

export const I18nProvider: FC<PropsWithChildren> = ({ children }) => {
const [currentI18NInstance, update] = useAtom(i18nAtom)
Expand All @@ -24,33 +23,18 @@ export const I18nProvider: FC<PropsWithChildren> = ({ children }) => {
const nextI18n = i18next.cloneInstance({
lng: lang,
})

update(nextI18n)
}),
[update],
)

useLayoutEffect(() => {
const i18next = currentI18NInstance

const handler = (lang: string) => {
if (i18next.language === lang) return
loadLanguageAndApply(lang).then(() => {
langChain.next(() => {
appLog("change language to", lang)
return i18next.changeLanguage(lang)
})
})
}
i18next.on("languageChanged", handler)

return () => {
i18next.off("languageChanged")
}
}, [currentI18NInstance])

useLayoutEffect(() => {
const languageDetector = new LanguageDetector(null, {
order: ["navigator", "htmlTag"],
order: ["querystring", "localStorage", "navigator"],
lookupQuerystring: "lng",
lookupLocalStorage: I18N_LOCALE_KEY,
caches: ["localStorage"],
})
const userLang = languageDetector.detect()
if (!userLang) return
Expand Down
2 changes: 2 additions & 0 deletions apps/renderer/src/providers/setting-sync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useEffect, useInsertionEffect, useLayoutEffect } from "react"

import { useGeneralSettingKey } from "~/atoms/settings/general"
import { useUISettingValue } from "~/atoms/settings/ui"
import { I18N_LOCALE_KEY } from "~/constants"
import { useReduceMotion } from "~/hooks/biz/useReduceMotion"
import { useSyncThemeark } from "~/hooks/common"
import { langChain } from "~/i18n"
Expand Down Expand Up @@ -65,6 +66,7 @@ const useLanguageSync = () => {
loadLanguageAndApply(language as string).then(() => {
langChain.next(() => {
if (mounted) {
localStorage.setItem(I18N_LOCALE_KEY, language as string)
return i18next.changeLanguage(language as string)
}
})
Expand Down

0 comments on commit 18e0353

Please sign in to comment.