Skip to content

Commit

Permalink
fix: dynamic load i18n resource in electron prod
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 0267b08 commit 2328648
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/main/tipc/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getRendererHandlers } from "@egoist/tipc/main"
import { callGlobalContextMethod } from "@shared/bridge"
import type { BrowserWindow } from "electron"
import { clipboard, dialog, screen } from "electron"
import { app, clipboard, dialog, screen } from "electron"

import { isWindows11 } from "../env"
import { downloadFile } from "../lib/download"
Expand Down Expand Up @@ -200,6 +200,8 @@ export const appRoute = {
if (!senderWindow) return
callGlobalContextMethod(senderWindow, "toast.success", ["Download success!"])
}),

getAppPath: t.procedure.action(async () => app.getAppPath()),
}
interface Sender extends Electron.WebContents {
getOwnerBrowserWindow: () => Electron.BrowserWindow | null
Expand Down
12 changes: 11 additions & 1 deletion src/renderer/src/lib/load-language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import dayjs from "dayjs"
import i18next from "i18next"
import { toast } from "sonner"

import { tipcClient } from "./client"

const loadingLangLock = new Set<string>()
const loadedLangs = new Set<string>([fallbackLanguage])

Expand Down Expand Up @@ -59,7 +61,15 @@ export const loadLanguageAndApply = async (lang: string) => {
}
}
} else {
const res = await eval(`import('/locales/${lang}.js')`)
let importFilePath = ""

if (window.electron) {
const electronAsarPath = await tipcClient?.getAppPath()
importFilePath = `${electronAsarPath}/dist/renderer/locales/${lang}.js`
} else {
importFilePath = `/locales/${lang}.js`
}
const res = await eval(`import('${importFilePath}')`)
.then((res: any) => res?.default || res)
.catch(() => {
toast.error(`${t("common:tips.load-lng-error")}: ${lang}`)
Expand Down

0 comments on commit 2328648

Please sign in to comment.