diff --git a/.vscode/settings.json b/.vscode/settings.json index 69f798259b..b7150ae22f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -32,11 +32,11 @@ }, "editor.formatOnSave": true, "files.associations": { - "*.css": "tailwindcss", - ".oxlintrc.json": "jsonc" + ".oxlintrc.json": "jsonc", + "*.css": "tailwindcss" }, "files.eol": "\n", - "i18n-ally.defaultNamespace": "translation", + // "i18n-ally.defaultNamespace": "translation", // "i18n-ally.displayLanguage": "zh-cn", // 界面显示语言 "i18n-ally.enabledFrameworks": [ "react-i18next", diff --git a/src/main/services/ContextMenu.ts b/src/main/services/ContextMenu.ts index ab81ff8eb0..3c85d9c6b4 100644 --- a/src/main/services/ContextMenu.ts +++ b/src/main/services/ContextMenu.ts @@ -1,4 +1,4 @@ -import { getI18n } from '@main/utils/language' +import { t } from '@main/utils/language' import type { MenuItemConstructorOptions } from 'electron' import { Menu } from 'electron' @@ -26,12 +26,10 @@ class ContextMenu { } private createInspectMenuItems(w: Electron.WebContents): MenuItemConstructorOptions[] { - const i18n = getI18n() - const { common } = i18n.translation const template: MenuItemConstructorOptions[] = [ { id: 'inspect', - label: common.inspect, + label: t('common.inspect'), click: () => { w.toggleDevTools() }, @@ -43,29 +41,27 @@ class ContextMenu { } private createEditMenuItems(properties: Electron.ContextMenuParams): MenuItemConstructorOptions[] { - const i18n = getI18n() - const { common } = i18n.translation const hasText = properties.selectionText.trim().length > 0 const can = (type: string) => properties.editFlags[`can${type}`] && hasText const template: MenuItemConstructorOptions[] = [ { id: 'copy', - label: common.copy, + label: t('common.copy'), role: 'copy', enabled: can('Copy'), visible: properties.isEditable || hasText }, { id: 'paste', - label: common.paste, + label: t('common.paste'), role: 'paste', enabled: properties.editFlags.canPaste, visible: properties.isEditable }, { id: 'cut', - label: common.cut, + label: t('common.cut'), role: 'cut', enabled: can('Cut'), visible: properties.isEditable diff --git a/src/main/services/TrayService.ts b/src/main/services/TrayService.ts index ccabfe2ca0..d55be43196 100644 --- a/src/main/services/TrayService.ts +++ b/src/main/services/TrayService.ts @@ -1,6 +1,6 @@ import { preferenceService } from '@data/PreferenceService' import { isLinux, isMac, isWin } from '@main/constant' -import { getI18n } from '@main/utils/language' +import { t } from '@main/utils/language' import type { MenuItemConstructorOptions } from 'electron' import { app, Menu, nativeImage, nativeTheme, Tray } from 'electron' @@ -72,23 +72,20 @@ export class TrayService { } private updateContextMenu() { - const i18n = getI18n() - const { tray: trayLocale, selection: selectionLocale } = i18n.translation - const quickAssistantEnabled = preferenceService.get('feature.quick_assistant.enabled') const selectionAssistantEnabled = preferenceService.get('feature.selection.enabled') const template = [ { - label: trayLocale.show_window, + label: t('tray.show_window'), click: () => windowService.showMainWindow() }, quickAssistantEnabled && { - label: trayLocale.show_mini_window, + label: t('tray.show_mini_window'), click: () => windowService.showMiniWindow() }, (isWin || isMac) && { - label: selectionLocale.name + (selectionAssistantEnabled ? ' - On' : ' - Off'), + label: t('selection.name') + (selectionAssistantEnabled ? ' - On' : ' - Off'), click: () => { if (selectionService) { selectionService.toggleEnabled() @@ -98,7 +95,7 @@ export class TrayService { }, { type: 'separator' }, { - label: trayLocale.quit, + label: t('tray.quit'), click: () => this.quit() } ].filter(Boolean) as MenuItemConstructorOptions[] diff --git a/src/main/utils/language.ts b/src/main/utils/language.ts index 850a532dbc..fad6c28562 100644 --- a/src/main/utils/language.ts +++ b/src/main/utils/language.ts @@ -3,7 +3,7 @@ import { loggerService } from '@logger' import { defaultLanguage } from '@shared/config/constant' import type { LanguageVarious } from '@shared/data/preference/preferenceTypes' import { app } from 'electron' -import i18next from 'i18next' +import i18n from 'i18next' // import deDE from '../../renderer/src/i18n/locales/de-de.json' // import elGR from '../../renderer/src/i18n/locales/el-gr.json' @@ -19,9 +19,9 @@ import ZhTw from '../../renderer/src/i18n/locales/zh-tw.json' const logger = loggerService.withContext('i18n') const resources = Object.fromEntries([ - ['en-US', EnUs], - ['zh-CN', ZhCn], - ['zh-TW', ZhTw] + ['en-us', EnUs], + ['zh-cn', ZhCn], + ['zh-tw', ZhTw] // ['ja-JP', JaJP], // ['ru-RU', RuRu], // ['de-DE', deDE], @@ -47,7 +47,7 @@ export const getI18n = (): Record => { return resources[language] } -const i18n = i18next.init({ +i18n.init({ resources, lng: getAppLanguage(), fallbackLng: defaultLanguage, @@ -60,4 +60,8 @@ const i18n = i18next.init({ } }) -export { i18n } +const t = i18n.t + +const changeLang: typeof i18n.changeLanguage = i18n.changeLanguage + +export { changeLang, i18n, t } diff --git a/src/renderer/src/i18n/index.ts b/src/renderer/src/i18n/index.ts index 3726c2d850..9244099b77 100644 --- a/src/renderer/src/i18n/index.ts +++ b/src/renderer/src/i18n/index.ts @@ -4,14 +4,14 @@ import { defaultLanguage } from '@shared/config/constant' import i18n from 'i18next' import { initReactI18next } from 'react-i18next' -import deDE from './locales/de-de.json' -import elGR from './locales/el-gr.json' +// import deDE from './locales/de-de.json' +// import elGR from './locales/el-gr.json' import enUS from './locales/en-us.json' -import esES from './locales/es-es.json' -import frFR from './locales/fr-fr.json' -import jaJP from './locales/ja-jp.json' -import ptPT from './locales/pt-pt.json' -import ruRU from './locales/ru-ru.json' +// import esES from './locales/es-es.json' +// import frFR from './locales/fr-fr.json' +// import jaJP from './locales/ja-jp.json' +// import ptPT from './locales/pt-pt.json' +// import ruRU from './locales/ru-ru.json' import zhCN from './locales/zh-cn.json' import zhTW from './locales/zh-tw.json' @@ -19,15 +19,15 @@ const logger = loggerService.withContext('I18N') const resources = Object.fromEntries([ ['en-US', enUS], - ['ja-JP', jaJP], - ['ru-RU', ruRU], + // ['ja-JP', jaJP], + // ['ru-RU', ruRU], ['zh-CN', zhCN], - ['zh-TW', zhTW], - ['de-DE', deDE], - ['el-GR', elGR], - ['es-ES', esES], - ['fr-FR', frFR], - ['pt-PT', ptPT] + ['zh-TW', zhTW] + // ['de-DE', deDE], + // ['el-GR', elGR], + // ['es-ES', esES], + // ['fr-FR', frFR], + // ['pt-PT', ptPT] ]) export const getLanguage = async () => {