Compare commits

...

2 Commits

Author SHA1 Message Date
xmk23333
2467306903 feat: remove hardcoded Chinese strings and improve i18n coverage 2025-12-28 12:34:13 +08:00
xmk23333
573be5501e refactor: simplify main process IPC handler registration 2025-12-28 12:22:28 +08:00
19 changed files with 313 additions and 247 deletions

View File

@ -13,6 +13,8 @@ import { join } from 'path'
import { app } from 'electron'
import { mihomoUpgradeConfig } from '../core/mihomoApi'
import i18next from 'i18next'
let profileConfig: IProfileConfig // profile.yaml
// 最终选中订阅ID
let targetProfileId: string | null = null
@ -33,7 +35,8 @@ export async function setProfileConfig(config: IProfileConfig): Promise<void> {
export async function getProfileItem(id: string | undefined): Promise<IProfileItem | undefined> {
const { items } = await getProfileConfig()
if (!id || id === 'default') return { id: 'default', type: 'local', name: '空白订阅' }
if (!id || id === 'default')
return { id: 'default', type: 'local', name: i18next.t('profiles.emptyProfile') }
return items.find((item) => item.id === id)
}
@ -126,7 +129,13 @@ export async function removeProfileItem(id: string): Promise<void> {
export async function getCurrentProfileItem(): Promise<IProfileItem> {
const { current } = await getProfileConfig()
return (await getProfileItem(current)) || { id: 'default', type: 'local', name: '空白订阅' }
return (
(await getProfileItem(current)) || {
id: 'default',
type: 'local',
name: i18next.t('profiles.emptyProfile')
}
)
}
interface FetchOptions {

View File

@ -11,6 +11,7 @@ import { exec, execSync, spawn } from 'child_process'
import { promisify } from 'util'
import { appLogger } from '../utils/logger'
import { checkAdminPrivileges } from '../core/manager'
import i18next from 'i18next'
export async function checkUpdate(): Promise<IAppVersion | undefined> {
const { 'mixed-port': mixedPort = 7890 } = await getControledMihomoConfig()
@ -68,7 +69,7 @@ export async function downloadAndInstallUpdate(version: string): Promise<void> {
file = file.replace('-setup.exe', '-portable.7z')
}
if (!file) {
throw new Error('不支持自动更新,请手动下载更新')
throw new Error(i18next.t('common.error.autoUpdateNotSupported'))
}
if (process.platform === 'win32' && parseInt(os.release()) < 10) {
file = file.replace('windows', 'win7')

View File

@ -11,10 +11,11 @@ import {
profilePath,
resourcesDir
} from '../utils/dirs'
import i18next from 'i18next'
export function getFilePath(ext: string[]): string[] | undefined {
return dialog.showOpenDialogSync({
title: '选择订阅文件',
title: i18next.t('common.dialog.selectSubscriptionFile'),
filters: [{ name: `${ext} file`, extensions: ext }],
properties: ['openFile']
})

View File

@ -126,23 +126,31 @@ import { addProfileUpdater, removeProfileUpdater } from '../core/profileUpdater'
import { readFile, writeFile } from 'fs/promises'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function wrapAsync<T extends (...args: any[]) => Promise<any>>(
type AsyncFn = (...args: any[]) => Promise<any>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type SyncFn = (...args: any[]) => any
function wrapAsync<T extends AsyncFn>(
fn: T
): (...args: Parameters<T>) => Promise<ReturnType<T> | { invokeError: unknown }> {
return async (...args) => {
try {
return await fn(...args)
} catch (e) {
if (e && typeof e === 'object') {
if ('message' in e) {
if (e && typeof e === 'object' && 'message' in e) {
return { invokeError: e.message }
}
return { invokeError: JSON.stringify(e) }
return { invokeError: typeof e === 'string' ? e : 'Unknown Error' }
}
if (typeof e === 'string') {
return { invokeError: e }
}
return { invokeError: 'Unknown Error' }
}
function registerHandlers(handlers: Record<string, AsyncFn | SyncFn>, async = true): void {
for (const [channel, handler] of Object.entries(handlers)) {
if (async) {
ipcMain.handle(channel, (_e, ...args) => wrapAsync(handler as AsyncFn)(...args))
} else {
ipcMain.handle(channel, (_e, ...args) => (handler as SyncFn)(...args))
}
}
}
@ -190,232 +198,160 @@ async function setTitleBarOverlay(overlay: Electron.TitleBarOverlayOptions): Pro
}
}
export function registerIpcMainHandlers(): void {
const asyncHandlers: Record<string, AsyncFn> = {
// Mihomo API
ipcMain.handle('mihomoVersion', wrapAsync(mihomoVersion))
ipcMain.handle('mihomoCloseConnection', (_e, id: string) => wrapAsync(mihomoCloseConnection)(id))
ipcMain.handle('mihomoCloseAllConnections', wrapAsync(mihomoCloseAllConnections))
ipcMain.handle('mihomoRules', wrapAsync(mihomoRules))
ipcMain.handle('mihomoProxies', wrapAsync(mihomoProxies))
ipcMain.handle('mihomoGroups', wrapAsync(mihomoGroups))
ipcMain.handle('mihomoProxyProviders', wrapAsync(mihomoProxyProviders))
ipcMain.handle('mihomoUpdateProxyProviders', (_e, name: string) =>
wrapAsync(mihomoUpdateProxyProviders)(name)
)
ipcMain.handle('mihomoRuleProviders', wrapAsync(mihomoRuleProviders))
ipcMain.handle('mihomoUpdateRuleProviders', (_e, name: string) =>
wrapAsync(mihomoUpdateRuleProviders)(name)
)
ipcMain.handle('mihomoChangeProxy', (_e, group: string, proxy: string) =>
wrapAsync(mihomoChangeProxy)(group, proxy)
)
ipcMain.handle('mihomoUnfixedProxy', (_e, group: string) => wrapAsync(mihomoUnfixedProxy)(group))
ipcMain.handle('mihomoUpgradeGeo', wrapAsync(mihomoUpgradeGeo))
ipcMain.handle('mihomoUpgrade', wrapAsync(mihomoUpgrade))
ipcMain.handle('mihomoUpgradeUI', wrapAsync(mihomoUpgradeUI))
ipcMain.handle('mihomoUpgradeConfig', wrapAsync(mihomoUpgradeConfig))
ipcMain.handle('mihomoProxyDelay', (_e, proxy: string, url?: string) =>
wrapAsync(mihomoProxyDelay)(proxy, url)
)
ipcMain.handle('mihomoGroupDelay', (_e, group: string, url?: string) =>
wrapAsync(mihomoGroupDelay)(group, url)
)
ipcMain.handle('patchMihomoConfig', (_e, patch: Partial<IMihomoConfig>) =>
wrapAsync(patchMihomoConfig)(patch)
)
ipcMain.handle('mihomoSmartGroupWeights', (_e, groupName: string) =>
wrapAsync(mihomoSmartGroupWeights)(groupName)
)
ipcMain.handle('mihomoSmartFlushCache', (_e, configName?: string) =>
wrapAsync(mihomoSmartFlushCache)(configName)
)
mihomoVersion,
mihomoCloseConnection,
mihomoCloseAllConnections,
mihomoRules,
mihomoProxies,
mihomoGroups,
mihomoProxyProviders,
mihomoUpdateProxyProviders,
mihomoRuleProviders,
mihomoUpdateRuleProviders,
mihomoChangeProxy,
mihomoUnfixedProxy,
mihomoUpgradeGeo,
mihomoUpgrade,
mihomoUpgradeUI,
mihomoUpgradeConfig,
mihomoProxyDelay,
mihomoGroupDelay,
patchMihomoConfig,
mihomoSmartGroupWeights,
mihomoSmartFlushCache,
// AutoRun
ipcMain.handle('checkAutoRun', wrapAsync(checkAutoRun))
ipcMain.handle('enableAutoRun', wrapAsync(enableAutoRun))
ipcMain.handle('disableAutoRun', wrapAsync(disableAutoRun))
checkAutoRun,
enableAutoRun,
disableAutoRun,
// Config
ipcMain.handle('getAppConfig', (_e, force?: boolean) => wrapAsync(getAppConfig)(force))
ipcMain.handle('patchAppConfig', (_e, config: Partial<IAppConfig>) =>
wrapAsync(patchAppConfig)(config)
)
ipcMain.handle('getControledMihomoConfig', (_e, force?: boolean) =>
wrapAsync(getControledMihomoConfig)(force)
)
ipcMain.handle('patchControledMihomoConfig', (_e, config: Partial<IMihomoConfig>) =>
wrapAsync(patchControledMihomoConfig)(config)
)
ipcMain.handle('resetAppConfig', () => resetAppConfig())
getAppConfig,
patchAppConfig,
getControledMihomoConfig,
patchControledMihomoConfig,
// Profile
ipcMain.handle('getProfileConfig', (_e, force?: boolean) => wrapAsync(getProfileConfig)(force))
ipcMain.handle('setProfileConfig', (_e, config: IProfileConfig) =>
wrapAsync(setProfileConfig)(config)
)
ipcMain.handle('getCurrentProfileItem', wrapAsync(getCurrentProfileItem))
ipcMain.handle('getProfileItem', (_e, id?: string) => wrapAsync(getProfileItem)(id))
ipcMain.handle('getProfileStr', (_e, id: string) => wrapAsync(getProfileStr)(id))
ipcMain.handle('setProfileStr', (_e, id: string, str: string) =>
wrapAsync(setProfileStr)(id, str)
)
ipcMain.handle('addProfileItem', (_e, item: Partial<IProfileItem>) =>
wrapAsync(addProfileItem)(item)
)
ipcMain.handle('removeProfileItem', (_e, id: string) => wrapAsync(removeProfileItem)(id))
ipcMain.handle('updateProfileItem', (_e, item: IProfileItem) => wrapAsync(updateProfileItem)(item))
ipcMain.handle('changeCurrentProfile', (_e, id: string) => wrapAsync(changeCurrentProfile)(id))
ipcMain.handle('addProfileUpdater', (_e, item: IProfileItem) => wrapAsync(addProfileUpdater)(item))
ipcMain.handle('removeProfileUpdater', (_e, id: string) => wrapAsync(removeProfileUpdater)(id))
getProfileConfig,
setProfileConfig,
getCurrentProfileItem,
getProfileItem,
getProfileStr,
setProfileStr,
addProfileItem,
removeProfileItem,
updateProfileItem,
changeCurrentProfile,
addProfileUpdater,
removeProfileUpdater,
// Override
ipcMain.handle('getOverrideConfig', (_e, force?: boolean) => wrapAsync(getOverrideConfig)(force))
ipcMain.handle('setOverrideConfig', (_e, config: IOverrideConfig) =>
wrapAsync(setOverrideConfig)(config)
)
ipcMain.handle('getOverrideItem', (_e, id: string) => wrapAsync(getOverrideItem)(id))
ipcMain.handle('addOverrideItem', (_e, item: Partial<IOverrideItem>) =>
wrapAsync(addOverrideItem)(item)
)
ipcMain.handle('removeOverrideItem', (_e, id: string) => wrapAsync(removeOverrideItem)(id))
ipcMain.handle('updateOverrideItem', (_e, item: IOverrideItem) =>
wrapAsync(updateOverrideItem)(item)
)
ipcMain.handle('getOverride', (_e, id: string, ext: 'js' | 'yaml' | 'log') =>
wrapAsync(getOverride)(id, ext)
)
ipcMain.handle('setOverride', (_e, id: string, ext: 'js' | 'yaml', str: string) =>
wrapAsync(setOverride)(id, ext, str)
)
getOverrideConfig,
setOverrideConfig,
getOverrideItem,
addOverrideItem,
removeOverrideItem,
updateOverrideItem,
getOverride,
setOverride,
// File
ipcMain.handle('getFileStr', (_e, filePath: string) => wrapAsync(getFileStr)(filePath))
ipcMain.handle('setFileStr', (_e, filePath: string, str: string) =>
wrapAsync(setFileStr)(filePath, str)
)
ipcMain.handle('convertMrsRuleset', (_e, filePath: string, behavior: string) =>
wrapAsync(convertMrsRuleset)(filePath, behavior)
)
ipcMain.handle('getRuntimeConfig', wrapAsync(getRuntimeConfig))
ipcMain.handle('getRuntimeConfigStr', wrapAsync(getRuntimeConfigStr))
ipcMain.handle('getSmartOverrideContent', wrapAsync(getSmartOverrideContent))
ipcMain.handle('getRuleStr', (_e, id: string) => wrapAsync(getRuleStr)(id))
ipcMain.handle('setRuleStr', (_e, id: string, str: string) => wrapAsync(setRuleStr)(id, str))
ipcMain.handle('getFilePath', (_e, ext: string[]) => getFilePath(ext))
ipcMain.handle('readTextFile', (_e, filePath: string) => wrapAsync(readTextFile)(filePath))
ipcMain.handle('openFile', (_e, type: 'profile' | 'override', id: string, ext?: 'yaml' | 'js') =>
openFile(type, id, ext)
)
getFileStr,
setFileStr,
convertMrsRuleset,
getRuntimeConfig,
getRuntimeConfigStr,
getSmartOverrideContent,
getRuleStr,
setRuleStr,
readTextFile,
// Core
ipcMain.handle('restartCore', wrapAsync(restartCore))
ipcMain.handle('startMonitor', (_e, detached?: boolean) => wrapAsync(startMonitor)(detached))
ipcMain.handle('quitWithoutCore', wrapAsync(quitWithoutCore))
restartCore,
startMonitor,
quitWithoutCore,
// System
ipcMain.handle('triggerSysProxy', (_e, enable: boolean) => wrapAsync(triggerSysProxy)(enable))
ipcMain.handle('checkTunPermissions', wrapAsync(checkTunPermissions))
ipcMain.handle('grantTunPermissions', wrapAsync(grantTunPermissions))
ipcMain.handle('manualGrantCorePermition', wrapAsync(manualGrantCorePermition))
ipcMain.handle('checkAdminPrivileges', wrapAsync(checkAdminPrivileges))
ipcMain.handle('restartAsAdmin', (_e, forTun?: boolean) => wrapAsync(restartAsAdmin)(forTun))
ipcMain.handle('checkMihomoCorePermissions', wrapAsync(checkMihomoCorePermissions))
ipcMain.handle('requestTunPermissions', wrapAsync(requestTunPermissions))
ipcMain.handle('checkHighPrivilegeCore', wrapAsync(checkHighPrivilegeCore))
ipcMain.handle('showTunPermissionDialog', wrapAsync(showTunPermissionDialog))
ipcMain.handle('showErrorDialog', (_e, title: string, message: string) =>
wrapAsync(showErrorDialog)(title, message)
)
ipcMain.handle('openUWPTool', wrapAsync(openUWPTool))
ipcMain.handle('setupFirewall', wrapAsync(setupFirewall))
ipcMain.handle('getInterfaces', getInterfaces)
ipcMain.handle('setNativeTheme', (_e, theme: 'system' | 'light' | 'dark') => setNativeTheme(theme))
ipcMain.handle('copyEnv', (_e, type: 'bash' | 'cmd' | 'powershell') => wrapAsync(copyEnv)(type))
triggerSysProxy,
checkTunPermissions,
grantTunPermissions,
manualGrantCorePermition,
checkAdminPrivileges,
restartAsAdmin,
checkMihomoCorePermissions,
requestTunPermissions,
checkHighPrivilegeCore,
showTunPermissionDialog,
showErrorDialog,
openUWPTool,
setupFirewall,
copyEnv,
// Update
ipcMain.handle('checkUpdate', wrapAsync(checkUpdate))
ipcMain.handle('downloadAndInstallUpdate', (_e, version: string) =>
wrapAsync(downloadAndInstallUpdate)(version)
)
ipcMain.handle('getVersion', () => app.getVersion())
ipcMain.handle('platform', () => process.platform)
ipcMain.handle('fetchMihomoTags', (_e, forceRefresh?: boolean) =>
wrapAsync(fetchMihomoTags)(forceRefresh)
)
ipcMain.handle('installSpecificMihomoCore', (_e, version: string) =>
wrapAsync(installSpecificMihomoCore)(version)
)
ipcMain.handle('clearMihomoVersionCache', wrapAsync(clearMihomoVersionCache))
checkUpdate,
downloadAndInstallUpdate,
fetchMihomoTags,
installSpecificMihomoCore,
clearMihomoVersionCache,
// Backup
ipcMain.handle('webdavBackup', wrapAsync(webdavBackup))
ipcMain.handle('webdavRestore', (_e, filename: string) => wrapAsync(webdavRestore)(filename))
ipcMain.handle('listWebdavBackups', wrapAsync(listWebdavBackups))
ipcMain.handle('webdavDelete', (_e, filename: string) => wrapAsync(webdavDelete)(filename))
ipcMain.handle('reinitWebdavBackupScheduler', wrapAsync(reinitScheduler))
ipcMain.handle('exportLocalBackup', wrapAsync(exportLocalBackup))
ipcMain.handle('importLocalBackup', wrapAsync(importLocalBackup))
webdavBackup,
webdavRestore,
listWebdavBackups,
webdavDelete,
reinitWebdavBackupScheduler: reinitScheduler,
exportLocalBackup,
importLocalBackup,
// SubStore
ipcMain.handle('startSubStoreFrontendServer', wrapAsync(startSubStoreFrontendServer))
ipcMain.handle('stopSubStoreFrontendServer', wrapAsync(stopSubStoreFrontendServer))
ipcMain.handle('startSubStoreBackendServer', wrapAsync(startSubStoreBackendServer))
ipcMain.handle('stopSubStoreBackendServer', wrapAsync(stopSubStoreBackendServer))
ipcMain.handle('downloadSubStore', wrapAsync(downloadSubStore))
ipcMain.handle('subStorePort', () => subStorePort)
ipcMain.handle('subStoreFrontendPort', () => subStoreFrontendPort)
ipcMain.handle('subStoreSubs', wrapAsync(subStoreSubs))
ipcMain.handle('subStoreCollections', wrapAsync(subStoreCollections))
startSubStoreFrontendServer,
stopSubStoreFrontendServer,
startSubStoreBackendServer,
stopSubStoreBackendServer,
downloadSubStore,
subStoreSubs,
subStoreCollections,
// Theme
ipcMain.handle('resolveThemes', wrapAsync(resolveThemes))
ipcMain.handle('fetchThemes', wrapAsync(fetchThemes))
ipcMain.handle('importThemes', (_e, files: string[]) => wrapAsync(importThemes)(files))
ipcMain.handle('readTheme', (_e, theme: string) => wrapAsync(readTheme)(theme))
ipcMain.handle('writeTheme', (_e, theme: string, css: string) =>
wrapAsync(writeTheme)(theme, css)
)
ipcMain.handle('applyTheme', (_e, theme: string) => wrapAsync(applyTheme)(theme))
resolveThemes,
fetchThemes,
importThemes,
readTheme,
writeTheme,
applyTheme,
// Tray
ipcMain.handle('showTrayIcon', wrapAsync(showTrayIcon))
ipcMain.handle('closeTrayIcon', wrapAsync(closeTrayIcon))
ipcMain.handle('updateTrayIcon', wrapAsync(updateTrayIcon))
ipcMain.handle('updateTrayIconImmediate', (_e, sysProxyEnabled: boolean, tunEnabled: boolean) =>
updateTrayIconImmediate(sysProxyEnabled, tunEnabled)
)
// Window
ipcMain.handle('showMainWindow', showMainWindow)
ipcMain.handle('closeMainWindow', closeMainWindow)
ipcMain.handle('triggerMainWindow', (_e, force?: boolean) => triggerMainWindow(force))
ipcMain.handle('showFloatingWindow', wrapAsync(showFloatingWindow))
ipcMain.handle('closeFloatingWindow', wrapAsync(closeFloatingWindow))
ipcMain.handle('showContextMenu', wrapAsync(showContextMenu))
ipcMain.handle('setTitleBarOverlay', (_e, overlay: Electron.TitleBarOverlayOptions) =>
wrapAsync(setTitleBarOverlay)(overlay)
)
ipcMain.handle('setAlwaysOnTop', (_e, alwaysOnTop: boolean) =>
mainWindow?.setAlwaysOnTop(alwaysOnTop)
)
ipcMain.handle('isAlwaysOnTop', () => mainWindow?.isAlwaysOnTop())
ipcMain.handle('openDevTools', () => mainWindow?.webContents.openDevTools())
ipcMain.handle('createHeapSnapshot', () =>
v8.writeHeapSnapshot(path.join(logDir(), `${Date.now()}.heapsnapshot`))
)
// Shortcut
ipcMain.handle('registerShortcut', (_e, oldShortcut: string, newShortcut: string, action: string) =>
wrapAsync(registerShortcut)(oldShortcut, newShortcut, action)
)
showTrayIcon,
closeTrayIcon,
updateTrayIcon,
// Floating Window
showFloatingWindow,
closeFloatingWindow,
showContextMenu,
// Misc
ipcMain.handle('getGistUrl', wrapAsync(getGistUrl))
ipcMain.handle('getImageDataURL', (_e, url: string) => wrapAsync(getImageDataURL)(url))
ipcMain.handle('relaunchApp', () => {
getGistUrl,
getImageDataURL,
changeLanguage,
setTitleBarOverlay,
registerShortcut
}
const syncHandlers: Record<string, SyncFn> = {
resetAppConfig,
getFilePath,
openFile,
getInterfaces,
setNativeTheme,
getVersion: () => app.getVersion(),
platform: () => process.platform,
subStorePort: () => subStorePort,
subStoreFrontendPort: () => subStoreFrontendPort,
updateTrayIconImmediate,
showMainWindow,
closeMainWindow,
triggerMainWindow,
setAlwaysOnTop: (alwaysOnTop: boolean) => mainWindow?.setAlwaysOnTop(alwaysOnTop),
isAlwaysOnTop: () => mainWindow?.isAlwaysOnTop(),
openDevTools: () => mainWindow?.webContents.openDevTools(),
createHeapSnapshot: () => v8.writeHeapSnapshot(path.join(logDir(), `${Date.now()}.heapsnapshot`)),
relaunchApp: () => {
app.relaunch()
app.quit()
})
ipcMain.handle('quitApp', () => app.quit())
ipcMain.handle('changeLanguage', (_e, lng: string) => wrapAsync(changeLanguage)(lng))
},
quitApp: () => app.quit()
}
export function registerIpcMainHandlers(): void {
registerHandlers(asyncHandlers, true)
registerHandlers(syncHandlers, false)
}

View File

@ -1,6 +1,7 @@
import React, { useEffect, useState, useCallback } from 'react'
import { createPortal } from 'react-dom'
import { IoCheckmark, IoClose, IoAlertSharp, IoInformationSharp, IoCopy } from 'react-icons/io5'
import i18next from 'i18next'
type ToastType = 'success' | 'error' | 'warning' | 'info'
@ -125,7 +126,9 @@ const ToastItem: React.FC<{
>
{icon}
</div>
<p className="text-base font-semibold text-foreground">{data.title || '错误'}</p>
<p className="text-base font-semibold text-foreground">
{data.title || i18next.t('common.error.default')}
</p>
</div>
<div className="relative" style={{ zIndex: 99999 }}>
<button
@ -145,7 +148,7 @@ const ToastItem: React.FC<{
className={`absolute top-full mt-1 left-1/2 -translate-x-1/2 px-2 py-1 text-xs text-foreground bg-content2 border border-default-200 rounded shadow-md whitespace-nowrap transition-all duration-200 ${copied ? 'opacity-100 translate-y-0' : 'opacity-0 -translate-y-1 pointer-events-none'}`}
style={{ zIndex: 99999 }}
>
{i18next.t('common.copied')}
</div>
</div>
</div>
@ -158,7 +161,7 @@ const ToastItem: React.FC<{
onClick={handleClose}
className="self-end px-4 py-1.5 text-sm font-medium text-white bg-danger rounded-lg hover:bg-danger/90 transition-colors"
>
{i18next.t('common.ok')}
</button>
</div>
)

View File

@ -1,4 +1,5 @@
import React, { createContext, useContext, ReactNode } from 'react'
import { useTranslation } from 'react-i18next'
import { showError } from '@renderer/utils/error-display'
import useSWR from 'swr'
import { getAppConfig, patchAppConfig as patch } from '@renderer/utils/ipc'
@ -12,13 +13,14 @@ interface AppConfigContextType {
const AppConfigContext = createContext<AppConfigContextType | undefined>(undefined)
export const AppConfigProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
const { t } = useTranslation()
const { data: appConfig, mutate: mutateAppConfig } = useSWR('getConfig', () => getAppConfig())
const patchAppConfig = async (value: Partial<IAppConfig>): Promise<void> => {
try {
await patch(value)
} catch (e) {
await showError(e, '更新应用配置失败')
await showError(e, t('common.error.updateAppConfigFailed'))
} finally {
mutateAppConfig()
}

View File

@ -1,4 +1,5 @@
import React, { createContext, useContext, ReactNode } from 'react'
import { useTranslation } from 'react-i18next'
import { showError } from '@renderer/utils/error-display'
import useSWR from 'swr'
import { getControledMihomoConfig, patchControledMihomoConfig as patch } from '@renderer/utils/ipc'
@ -14,6 +15,7 @@ const ControledMihomoConfigContext = createContext<ControledMihomoConfigContextT
)
export const ControledMihomoConfigProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
const { t } = useTranslation()
const { data: controledMihomoConfig, mutate: mutateControledMihomoConfig } = useSWR(
'getControledMihomoConfig',
() => getControledMihomoConfig()
@ -23,7 +25,7 @@ export const ControledMihomoConfigProvider: React.FC<{ children: ReactNode }> =
try {
await patch(value)
} catch (e) {
await showError(e, '更新内核配置失败')
await showError(e, t('common.error.updateCoreConfigFailed'))
} finally {
mutateControledMihomoConfig()
}

View File

@ -1,4 +1,5 @@
import React, { createContext, useContext, ReactNode } from 'react'
import { useTranslation } from 'react-i18next'
import { showError } from '@renderer/utils/error-display'
import useSWR from 'swr'
import {
@ -21,6 +22,7 @@ interface OverrideConfigContextType {
const OverrideConfigContext = createContext<OverrideConfigContextType | undefined>(undefined)
export const OverrideConfigProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
const { t } = useTranslation()
const { data: overrideConfig, mutate: mutateOverrideConfig } = useSWR('getOverrideConfig', () =>
getOverrideConfig()
)
@ -29,7 +31,7 @@ export const OverrideConfigProvider: React.FC<{ children: ReactNode }> = ({ chil
try {
await set(config)
} catch (e) {
await showError(e, '保存覆写配置失败')
await showError(e, t('common.error.saveOverrideConfigFailed'))
} finally {
mutateOverrideConfig()
}
@ -39,7 +41,7 @@ export const OverrideConfigProvider: React.FC<{ children: ReactNode }> = ({ chil
try {
await add(item)
} catch (e) {
await showError(e, '添加覆写失败')
await showError(e, t('common.error.addOverrideFailed'))
} finally {
mutateOverrideConfig()
}
@ -49,7 +51,7 @@ export const OverrideConfigProvider: React.FC<{ children: ReactNode }> = ({ chil
try {
await remove(id)
} catch (e) {
await showError(e, '删除覆写失败')
await showError(e, t('common.error.deleteOverrideFailed'))
} finally {
mutateOverrideConfig()
}
@ -59,7 +61,7 @@ export const OverrideConfigProvider: React.FC<{ children: ReactNode }> = ({ chil
try {
await update(item)
} catch (e) {
await showError(e, '更新覆写失败')
await showError(e, t('common.error.updateOverrideFailed'))
} finally {
mutateOverrideConfig()
}

View File

@ -1,4 +1,5 @@
import React, { createContext, ReactNode, useContext } from 'react'
import { useTranslation } from 'react-i18next'
import { showError } from '@renderer/utils/error-display'
import useSWR from 'swr'
import {
@ -23,6 +24,7 @@ interface ProfileConfigContextType {
const ProfileConfigContext = createContext<ProfileConfigContextType | undefined>(undefined)
export const ProfileConfigProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
const { t } = useTranslation()
const { data: profileConfig, mutate: mutateProfileConfig } = useSWR('getProfileConfig', () =>
getProfileConfig()
)
@ -33,7 +35,7 @@ export const ProfileConfigProvider: React.FC<{ children: ReactNode }> = ({ child
try {
await set(config)
} catch (e) {
await showError(e, '保存配置失败')
await showError(e, t('common.error.saveProfileConfigFailed'))
} finally {
mutateProfileConfig()
window.electron.ipcRenderer.send('updateTrayMenu')
@ -44,7 +46,7 @@ export const ProfileConfigProvider: React.FC<{ children: ReactNode }> = ({ child
try {
await add(item)
} catch (e) {
await showError(e, '添加配置失败')
await showError(e, t('common.error.addProfileFailed'))
} finally {
mutateProfileConfig()
window.electron.ipcRenderer.send('updateTrayMenu')
@ -55,7 +57,7 @@ export const ProfileConfigProvider: React.FC<{ children: ReactNode }> = ({ child
try {
await remove(id)
} catch (e) {
await showError(e, '删除配置失败')
await showError(e, t('common.error.deleteProfileFailed'))
} finally {
mutateProfileConfig()
window.electron.ipcRenderer.send('updateTrayMenu')
@ -66,7 +68,7 @@ export const ProfileConfigProvider: React.FC<{ children: ReactNode }> = ({ child
try {
await update(item)
} catch (e) {
await showError(e, '更新配置失败')
await showError(e, t('common.error.updateProfileFailed'))
} finally {
mutateProfileConfig()
window.electron.ipcRenderer.send('updateTrayMenu')
@ -108,7 +110,7 @@ export const ProfileConfigProvider: React.FC<{ children: ReactNode }> = ({ child
if (errorMsg.includes('reply was never sent')) {
setTimeout(() => mutateProfileConfig(), 1000)
} else {
await showError(errorMsg, '切换配置失败')
await showError(errorMsg, t('common.error.switchProfileFailed'))
mutateProfileConfig()
}
} finally {

View File

@ -39,6 +39,27 @@
"common.error.shortcutRegistrationFailedWithError": "Failed to register shortcut: {{error}}",
"common.error.adminRequired": "Please run with administrator privileges for first launch",
"common.error.initFailed": "Application initialization failed",
"common.error.default": "Error",
"common.error.updateAppConfigFailed": "Failed to update app config",
"common.error.updateCoreConfigFailed": "Failed to update core config",
"common.error.saveProfileConfigFailed": "Failed to save profile config",
"common.error.addProfileFailed": "Failed to add profile",
"common.error.deleteProfileFailed": "Failed to delete profile",
"common.error.updateProfileFailed": "Failed to update profile",
"common.error.switchProfileFailed": "Failed to switch profile",
"common.error.saveOverrideConfigFailed": "Failed to save override config",
"common.error.addOverrideFailed": "Failed to add override",
"common.error.deleteOverrideFailed": "Failed to delete override",
"common.error.updateOverrideFailed": "Failed to update override",
"common.error.firewallSetupFailed": "Failed to setup firewall",
"common.error.coreAuthFailed": "Failed to authorize core",
"common.error.sysproxySetupFailed": "Failed to setup system proxy",
"common.error.snifferConfigSaveFailed": "Failed to save sniffer config",
"common.error.dnsConfigSaveFailed": "Failed to save DNS config",
"common.copied": "Copied",
"common.ok": "OK",
"common.error.autoUpdateNotSupported": "Auto update not supported, please download manually",
"common.dialog.selectSubscriptionFile": "Select Subscription File",
"core.highPrivilege.title": "High Privilege Core Detected",
"core.highPrivilege.message": "A high-privilege core is detected. The application needs to restart in administrator mode to match permissions. Restart now?",
"common.updater.versionReady": "v{{version}} Version Ready",

View File

@ -39,6 +39,27 @@
"common.error.shortcutRegistrationFailedWithError": "ثبت میانبر با خطا مواجه شد: {{error}}",
"common.error.adminRequired": "لطفا برای اولین اجرا با دسترسی مدیر برنامه را اجرا کنید",
"common.error.initFailed": "راه‌اندازی برنامه با خطا مواجه شد",
"common.error.default": "خطا",
"common.error.updateAppConfigFailed": "به‌روزرسانی تنظیمات برنامه با خطا مواجه شد",
"common.error.updateCoreConfigFailed": "به‌روزرسانی تنظیمات هسته با خطا مواجه شد",
"common.error.saveProfileConfigFailed": "ذخیره تنظیمات پروفایل با خطا مواجه شد",
"common.error.addProfileFailed": "افزودن پروفایل با خطا مواجه شد",
"common.error.deleteProfileFailed": "حذف پروفایل با خطا مواجه شد",
"common.error.updateProfileFailed": "به‌روزرسانی پروفایل با خطا مواجه شد",
"common.error.switchProfileFailed": "تغییر پروفایل با خطا مواجه شد",
"common.error.saveOverrideConfigFailed": "ذخیره تنظیمات بازنویسی با خطا مواجه شد",
"common.error.addOverrideFailed": "افزودن بازنویسی با خطا مواجه شد",
"common.error.deleteOverrideFailed": "حذف بازنویسی با خطا مواجه شد",
"common.error.updateOverrideFailed": "به‌روزرسانی بازنویسی با خطا مواجه شد",
"common.error.firewallSetupFailed": "تنظیم فایروال با خطا مواجه شد",
"common.error.coreAuthFailed": "احراز هویت هسته با خطا مواجه شد",
"common.error.sysproxySetupFailed": "تنظیم پروکسی سیستم با خطا مواجه شد",
"common.error.snifferConfigSaveFailed": "ذخیره تنظیمات اسنیفر با خطا مواجه شد",
"common.error.dnsConfigSaveFailed": "ذخیره تنظیمات DNS با خطا مواجه شد",
"common.copied": "کپی شد",
"common.ok": "تأیید",
"common.error.autoUpdateNotSupported": "به‌روزرسانی خودکار پشتیبانی نمی‌شود، لطفاً به صورت دستی دانلود کنید",
"common.dialog.selectSubscriptionFile": "انتخاب فایل اشتراک",
"core.highPrivilege.title": "هسته با سطح دسترسی بالا شناسایی شد",
"core.highPrivilege.message": "هسته‌ای با سطح دسترسی بالا شناسایی شد. برنامه باید در حالت مدیر سیستم برای تطابق سطح دسترسی‌ها دوباره راه‌اندازی شود. آیا می‌خواهید اکنون راه‌اندازی مجدد شود؟",
"common.updater.versionReady": "نسخه v{{version}} آماده است",

View File

@ -39,6 +39,27 @@
"common.error.shortcutRegistrationFailedWithError": "Не удалось зарегистрировать сочетание клавиш: {{error}}",
"common.error.adminRequired": "Для первого запуска требуются права администратора",
"common.error.initFailed": "Не удалось инициализировать приложение",
"common.error.default": "Ошибка",
"common.error.updateAppConfigFailed": "Не удалось обновить конфигурацию приложения",
"common.error.updateCoreConfigFailed": "Не удалось обновить конфигурацию ядра",
"common.error.saveProfileConfigFailed": "Не удалось сохранить конфигурацию профиля",
"common.error.addProfileFailed": "Не удалось добавить профиль",
"common.error.deleteProfileFailed": "Не удалось удалить профиль",
"common.error.updateProfileFailed": "Не удалось обновить профиль",
"common.error.switchProfileFailed": "Не удалось переключить профиль",
"common.error.saveOverrideConfigFailed": "Не удалось сохранить конфигурацию переопределения",
"common.error.addOverrideFailed": "Не удалось добавить переопределение",
"common.error.deleteOverrideFailed": "Не удалось удалить переопределение",
"common.error.updateOverrideFailed": "Не удалось обновить переопределение",
"common.error.firewallSetupFailed": "Не удалось настроить брандмауэр",
"common.error.coreAuthFailed": "Не удалось авторизовать ядро",
"common.error.sysproxySetupFailed": "Не удалось настроить системный прокси",
"common.error.snifferConfigSaveFailed": "Не удалось сохранить конфигурацию сниффера",
"common.error.dnsConfigSaveFailed": "Не удалось сохранить конфигурацию DNS",
"common.copied": "Скопировано",
"common.ok": "ОК",
"common.error.autoUpdateNotSupported": "Автообновление не поддерживается, пожалуйста, скачайте вручную",
"common.dialog.selectSubscriptionFile": "Выберите файл подписки",
"core.highPrivilege.title": "High Privilege Core Detected",
"core.highPrivilege.message": "Обнаружено ядро с повышенными привилегиями. Приложение необходимо перезапустить в режиме администратора для согласования прав. Перезапустить сейчас?",
"common.updater.versionReady": "Обнаружено ядро с повышенными привилегиями",

View File

@ -39,6 +39,27 @@
"common.error.shortcutRegistrationFailedWithError": "快捷键注册失败:{{error}}",
"common.error.adminRequired": "首次启动请以管理员权限运行",
"common.error.initFailed": "应用初始化失败",
"common.error.default": "错误",
"common.error.updateAppConfigFailed": "更新应用配置失败",
"common.error.updateCoreConfigFailed": "更新内核配置失败",
"common.error.saveProfileConfigFailed": "保存配置失败",
"common.error.addProfileFailed": "添加配置失败",
"common.error.deleteProfileFailed": "删除配置失败",
"common.error.updateProfileFailed": "更新配置失败",
"common.error.switchProfileFailed": "切换配置失败",
"common.error.saveOverrideConfigFailed": "保存覆写配置失败",
"common.error.addOverrideFailed": "添加覆写失败",
"common.error.deleteOverrideFailed": "删除覆写失败",
"common.error.updateOverrideFailed": "更新覆写失败",
"common.error.firewallSetupFailed": "防火墙设置失败",
"common.error.coreAuthFailed": "内核授权失败",
"common.error.sysproxySetupFailed": "系统代理设置失败",
"common.error.snifferConfigSaveFailed": "嗅探配置保存失败",
"common.error.dnsConfigSaveFailed": "DNS 配置保存失败",
"common.copied": "已复制",
"common.ok": "确定",
"common.error.autoUpdateNotSupported": "不支持自动更新,请手动下载更新",
"common.dialog.selectSubscriptionFile": "选择订阅文件",
"core.highPrivilege.title": "检测到高权限内核",
"core.highPrivilege.message": "检测到运行中的高权限内核,需以管理员模式重启应用以匹配权限,确定重启?",
"common.updater.versionReady": "v{{version}} 版本就绪",

View File

@ -39,6 +39,27 @@
"common.error.shortcutRegistrationFailedWithError": "快捷鍵註冊失敗:{{error}}",
"common.error.adminRequired": "首次啟動請以系統管理員身分執行",
"common.error.initFailed": "應用初始化失敗",
"common.error.default": "錯誤",
"common.error.updateAppConfigFailed": "更新應用配置失敗",
"common.error.updateCoreConfigFailed": "更新內核配置失敗",
"common.error.saveProfileConfigFailed": "保存配置失敗",
"common.error.addProfileFailed": "添加配置失敗",
"common.error.deleteProfileFailed": "刪除配置失敗",
"common.error.updateProfileFailed": "更新配置失敗",
"common.error.switchProfileFailed": "切換配置失敗",
"common.error.saveOverrideConfigFailed": "保存覆寫配置失敗",
"common.error.addOverrideFailed": "添加覆寫失敗",
"common.error.deleteOverrideFailed": "刪除覆寫失敗",
"common.error.updateOverrideFailed": "更新覆寫失敗",
"common.error.firewallSetupFailed": "防火牆設置失敗",
"common.error.coreAuthFailed": "內核授權失敗",
"common.error.sysproxySetupFailed": "系統代理設置失敗",
"common.error.snifferConfigSaveFailed": "嗅探配置保存失敗",
"common.error.dnsConfigSaveFailed": "DNS 配置保存失敗",
"common.copied": "已複製",
"common.ok": "確定",
"common.error.autoUpdateNotSupported": "不支持自動更新,請手動下載更新",
"common.dialog.selectSubscriptionFile": "選擇訂閱文件",
"core.highPrivilege.title": "檢測到高權限內核",
"core.highPrivilege.message": "檢測到運行中的高權限內核,需以系統管理員模式重新啟動應用程式以匹配權限,確定重新啟動?",
"common.updater.versionReady": "v{{version}} 版本就緒",

View File

@ -146,7 +146,7 @@ const DNS: React.FC = () => {
await restartCore()
}
} catch (e) {
showErrorSync(e, 'DNS 配置保存失败')
showErrorSync(e, t('common.error.dnsConfigSaveFailed'))
}
}

View File

@ -71,7 +71,7 @@ const Sniffer: React.FC = () => {
await restartCore()
}
} catch (e) {
showErrorSync(e, '嵅探配置保存失败')
showErrorSync(e, t('common.error.snifferConfigSaveFailed'))
}
}

View File

@ -106,7 +106,7 @@ const Sysproxy: React.FC = () => {
} catch (e) {
setValues({ ...values, enable: previousState })
setChanged(true)
showErrorSync(e, '系统代理设置失败')
showErrorSync(e, t('common.error.sysproxySetupFailed'))
await patchAppConfig({ sysProxy: { enable: false } })
}

View File

@ -113,7 +113,7 @@ const Tun: React.FC = () => {
new Notification(t('tun.notifications.firewallResetSuccess'))
await restartCore()
} catch (e) {
showErrorSync(e, '防火墙设置失败')
showErrorSync(e, t('common.error.firewallSetupFailed'))
} finally {
setLoading(false)
}
@ -134,7 +134,7 @@ const Tun: React.FC = () => {
new Notification(t('tun.notifications.coreAuthSuccess'))
await restartCore()
} catch (e) {
showErrorSync(e, '内核授权失败')
showErrorSync(e, t('common.error.coreAuthFailed'))
}
}}
>

View File

@ -1,4 +1,5 @@
import { toast } from '@renderer/components/base/toast'
import i18next from 'i18next'
const DETAILED_ERROR_KEYWORDS = [
'yaml',
@ -33,9 +34,10 @@ function shouldShowDetailedError(message: string): boolean {
export async function showError(error: unknown, title?: string): Promise<void> {
const message = error instanceof Error ? error.message : String(error)
const defaultTitle = i18next.t('common.error.default')
if (shouldShowDetailedError(message)) {
toast.detailedError(message, title || '错误')
toast.detailedError(message, title || defaultTitle)
} else {
toast.error(message, title)
}
@ -43,9 +45,10 @@ export async function showError(error: unknown, title?: string): Promise<void> {
export function showErrorSync(error: unknown, title?: string): void {
const message = error instanceof Error ? error.message : String(error)
const defaultTitle = i18next.t('common.error.default')
if (shouldShowDetailedError(message)) {
toast.detailedError(message, title || '错误')
toast.detailedError(message, title || defaultTitle)
} else {
toast.error(message, title)
}