feat: 国际化 (i18n) 功能实现

This commit is contained in:
alger
2025-02-19 01:01:43 +08:00
parent da2a32e420
commit ead017e4b1
64 changed files with 1870 additions and 510 deletions

View File

@@ -2,13 +2,14 @@ import { electronApp, optimizer } from '@electron-toolkit/utils';
import { app, ipcMain, nativeImage } from 'electron';
import { join } from 'path';
import type { Language } from '../i18n/main';
import i18n from '../i18n/main';
import { loadLyricWindow } from './lyric';
import { initializeConfig } from './modules/config';
import { initializeFileManager } from './modules/fileManager';
import { initializeFonts } from './modules/fonts';
import { initializeShortcuts, registerShortcuts } from './modules/shortcuts';
import { initializeTray } from './modules/tray';
import { initializeTray, updateTrayMenu } from './modules/tray';
import { setupUpdateHandlers } from './modules/update';
import { createMainWindow, initializeWindowManager } from './modules/window';
import { startMusicApi } from './server';
@@ -28,7 +29,14 @@ let mainWindow: Electron.BrowserWindow;
// 初始化应用
function initialize() {
// 初始化配置管理
initializeConfig();
const store = initializeConfig();
// 设置初始语言
const savedLanguage = store.get('set.language') as Language;
if (savedLanguage) {
i18n.global.locale = savedLanguage;
}
// 初始化文件管理
initializeFileManager();
// 初始化窗口管理
@@ -97,8 +105,11 @@ if (!isSingleInstance) {
});
// 监听语言切换
ipcMain.on('change-language', (_, locale) => {
ipcMain.on('change-language', (_, locale: Language) => {
// 更新主进程的语言设置
i18n.global.locale = locale;
// 更新托盘菜单
updateTrayMenu();
// 通知所有窗口语言已更改
mainWindow?.webContents.send('language-changed', locale);
});

View File

@@ -1,8 +1,59 @@
import { app, BrowserWindow, Menu, nativeImage, Tray } from 'electron';
import { join } from 'path';
import type { Language } from '../../i18n/main';
import i18n from '../../i18n/main';
let tray: Tray | null = null;
const LANGUAGES: { label: string; value: Language }[] = [
{ label: '简体中文', value: 'zh-CN' },
{ label: 'English', value: 'en-US' }
];
// 导出更新菜单的函数
export function updateTrayMenu() {
if (!tray) return;
// 创建一个上下文菜单
const contextMenu = Menu.buildFromTemplate([
{
label: i18n.global.t('common.tray.show'),
click: () => {
BrowserWindow.getAllWindows()[0]?.show();
}
},
{ type: 'separator' },
{
label: i18n.global.t('common.language'),
submenu: LANGUAGES.map(({ label, value }) => ({
label,
type: 'radio',
checked: i18n.global.locale === value,
click: () => {
// 更新主进程的语言设置
i18n.global.locale = value;
// 更新托盘菜单
updateTrayMenu();
// 通知渲染进程
const win = BrowserWindow.getAllWindows()[0];
win?.webContents.send('set-language', value);
}
}))
},
{ type: 'separator' },
{
label: i18n.global.t('common.tray.quit'),
click: () => {
app.quit();
}
}
]);
// 设置系统托盘图标的上下文菜单
tray.setContextMenu(contextMenu);
}
/**
* 初始化系统托盘
*/
@@ -12,25 +63,8 @@ export function initializeTray(iconPath: string, mainWindow: BrowserWindow) {
.resize({ width: 16, height: 16 });
tray = new Tray(trayIcon);
// 创建一个上下文菜单
const contextMenu = Menu.buildFromTemplate([
{
label: '显示',
click: () => {
mainWindow.show();
}
},
{
label: '退出',
click: () => {
mainWindow.destroy();
app.quit();
}
}
]);
// 设置系统托盘图标的上下文菜单
tray.setContextMenu(contextMenu);
// 初始化菜单
updateTrayMenu();
// 当系统托盘图标被点击时,切换窗口的显示/隐藏
tray.on('click', () => {