Skip to content

Commit

Permalink
feat: add manual setting lang lock keyu
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Sep 16, 2024
1 parent 6b1009e commit b9c6a8b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/renderer/src/constants/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ export const ROUTE_FEED_IN_FOLDER = "folder-"

export const DAILY_CLAIM_AMOUNT = "20"
export const INVITATION_PRICE = "100"

// NS key
export const IS_MANUAL_CHANGE_LANGUAGE_KEY = getStorageNS("isManualChangeLanguage")
2 changes: 2 additions & 0 deletions src/renderer/src/modules/settings/tabs/general.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
SelectTrigger,
SelectValue,
} from "@renderer/components/ui/select"
import { IS_MANUAL_CHANGE_LANGUAGE_KEY } from "@renderer/constants"
import { fallbackLanguage } from "@renderer/i18n"
import { initPostHog } from "@renderer/initialize/posthog"
import { tipcClient } from "@renderer/lib/client"
Expand Down Expand Up @@ -213,6 +214,7 @@ export const LanguageSelector = () => {
defaultValue={finalRenderLanguage}
value={finalRenderLanguage}
onValueChange={(value) => {
localStorage.setItem(IS_MANUAL_CHANGE_LANGUAGE_KEY, "true")
setGeneralSetting("language", value as string)
i18n.changeLanguage(value as string)
}}
Expand Down
28 changes: 14 additions & 14 deletions src/renderer/src/providers/i18n-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {
currentSupportedLanguages,
dayjsLocaleImportMap,
} from "@renderer/@types/constants"
import { currentSupportedLanguages, dayjsLocaleImportMap } from "@renderer/@types/constants"
import { defaultResources } from "@renderer/@types/default-resource"
import { getGeneralSettings, setGeneralSetting } from "@renderer/atoms/settings/general"
import { IS_MANUAL_CHANGE_LANGUAGE_KEY } from "@renderer/constants"
import { fallbackLanguage, i18nAtom, LocaleCache } from "@renderer/i18n"
import { EventBus } from "@renderer/lib/event-bus"
import { jotaiStore } from "@renderer/lib/jotai"
Expand Down Expand Up @@ -110,7 +108,19 @@ export const I18nProvider: FC<PropsWithChildren> = ({ children }) => {

const callOnce = useRef(false)
const detectOnce = useRef(false)

useLayoutEffect(() => {
const i18next = currentI18NInstance

i18next.on("languageChanged", langChangedHandler)

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

useLayoutEffect(() => {
if (localStorage.getItem(IS_MANUAL_CHANGE_LANGUAGE_KEY)) return
if (detectOnce.current) return
const languageDetector = new LanguageDetector()
const userLang = languageDetector.detect()
Expand All @@ -122,16 +132,6 @@ export const I18nProvider: FC<PropsWithChildren> = ({ children }) => {
detectOnce.current = true
}, [])

useLayoutEffect(() => {
const i18next = currentI18NInstance

i18next.on("languageChanged", langChangedHandler)

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

useLayoutEffect(() => {
if (callOnce.current) return
const { language } = getGeneralSettings()
Expand Down

0 comments on commit b9c6a8b

Please sign in to comment.