11import { app , BrowserWindow , screen , Menu } from 'electron'
22import type { MenuItemConstructorOptions } from 'electron'
33import { fileURLToPath } from 'node:url'
4+ import { isDev } from './lib/is-dev'
45import path from 'node:path'
5- import GlobalSetting from '../setting.global'
66import initIPC from './ipc'
77import { initSqlite } from './sqlite'
8+ import i18next from 'i18next'
9+ import { changeAppLanguage , initI18n } from './i18n'
10+ import { i18nLanguages } from './i18n/common-options'
811import useCookieAllowCrossSite from './lib/cookie-allow-cross-site'
912
1013// 用于引入 CommonJS 模块的方法
@@ -29,17 +32,14 @@ export const VITE_DEV_SERVER_URL = process.env['VITE_DEV_SERVER_URL']
2932export const MAIN_DIST = path . join ( process . env . APP_ROOT , 'dist-electron' )
3033export const RENDERER_DIST = path . join ( process . env . APP_ROOT , 'dist' )
3134
32- process . env . VITE_PUBLIC = VITE_DEV_SERVER_URL
33- ? path . join ( process . env . APP_ROOT , 'public' )
34- : RENDERER_DIST
35+ process . env . VITE_PUBLIC = isDev ? path . join ( process . env . APP_ROOT , 'public' ) : RENDERER_DIST
3536
3637let win : BrowserWindow | null
3738
3839function createWindow ( ) {
3940 const { width, height } = screen . getPrimaryDisplay ( ) . workAreaSize
4041 win = new BrowserWindow ( {
4142 icon : path . join ( process . env . VITE_PUBLIC , 'icon.png' ) ,
42- title : GlobalSetting . appName ,
4343 width : Math . ceil ( width * 0.8 ) ,
4444 height : Math . ceil ( height * 0.8 ) ,
4545 minWidth : 800 ,
@@ -76,76 +76,63 @@ function buildMenu() {
7676 ...( process . platform === 'darwin'
7777 ? [
7878 {
79- label : app . name ,
79+ label : i18next . t ( ' app.name' ) ,
8080 submenu : [
81- { role : 'about' } ,
81+ {
82+ label : i18next . t ( 'menu.app.about' ) ,
83+ click : async ( ) => {
84+ const { shell } = await import ( 'electron' )
85+ await shell . openExternal ( 'https://github.com/YILS-LIN/short-video-factory' )
86+ } ,
87+ } ,
8288 { type : 'separator' } ,
83- { role : 'services' } ,
89+ { label : i18next . t ( 'menu.app.services' ) , role : 'services' } ,
8490 { type : 'separator' } ,
85- { role : 'hide' } ,
86- { role : 'hideOthers' } ,
87- { role : 'unhide' } ,
91+ { label : i18next . t ( 'menu.app.hide' ) , role : 'hide' } ,
92+ { label : i18next . t ( 'menu.app.hideOthers' ) , role : 'hideOthers' } ,
93+ { label : i18next . t ( 'menu.app.unhide' ) , role : 'unhide' } ,
8894 { type : 'separator' } ,
89- { role : 'quit' } ,
95+ { label : i18next . t ( 'menu.app.quit' ) , role : 'quit' } ,
9096 ] as MenuItemConstructorOptions [ ] ,
9197 } ,
9298 ]
9399 : [ ] ) ,
94100 {
95- label : 'Language' ,
96- submenu : [
97- {
98- label : 'English' ,
99- type : 'radio' ,
100- checked : true ,
101- click : ( ) => {
102- BrowserWindow . getAllWindows ( ) . forEach ( ( w ) => w . webContents . send ( 'set-locale' , 'en' ) )
103- } ,
101+ label : i18next . t ( 'menu.language' ) ,
102+ submenu : i18nLanguages . map ( ( lng ) => ( {
103+ label : lng . name ,
104+ type : 'radio' ,
105+ checked : i18next . language === lng . code ,
106+ click : ( ) => {
107+ changeAppLanguage ( lng . code )
104108 } ,
105- {
106- label : '中文' ,
107- type : 'radio' ,
108- click : ( ) => {
109- BrowserWindow . getAllWindows ( ) . forEach ( ( w ) => w . webContents . send ( 'set-locale' , 'zh-CN' ) )
110- } ,
111- } ,
112- ] as MenuItemConstructorOptions [ ] ,
109+ } ) ) as MenuItemConstructorOptions [ ] ,
113110 } ,
114111 {
115- label : 'Edit' ,
112+ label : i18next . t ( 'menu.view.root' ) ,
116113 submenu : [
117- { role : 'undo' } ,
118- { role : 'redo' } ,
114+ { role : 'toggleDevTools' , visible : false } ,
115+ { label : i18next . t ( 'menu.view.resetZoom' ) , role : 'resetZoom' } ,
116+ { label : i18next . t ( 'menu.view.zoomIn' ) , role : 'zoomIn' } ,
117+ { label : i18next . t ( 'menu.view.zoomOut' ) , role : 'zoomOut' } ,
119118 { type : 'separator' } ,
120- { role : 'cut' } ,
121- { role : 'copy' } ,
122- { role : 'paste' } ,
123- { role : 'selectAll' } ,
119+ { label : i18next . t ( 'menu.view.toggleFullscreen' ) , role : 'togglefullscreen' } ,
124120 ] as MenuItemConstructorOptions [ ] ,
125121 } ,
126122 {
127- label : 'View' ,
123+ label : i18next . t ( 'menu.window.root' ) ,
124+ role : 'window' ,
128125 submenu : [
129- { role : 'reload' } ,
130- { role : 'forceReload' } ,
131- { role : 'toggleDevTools' } ,
132- { type : 'separator' } ,
133- { role : 'resetZoom' } ,
134- { role : 'zoomIn' } ,
135- { role : 'zoomOut' } ,
136- { type : 'separator' } ,
137- { role : 'togglefullscreen' } ,
126+ { label : i18next . t ( 'menu.window.minimize' ) , role : 'minimize' } ,
127+ { label : i18next . t ( 'menu.window.close' ) , role : 'close' } ,
138128 ] as MenuItemConstructorOptions [ ] ,
139129 } ,
140130 {
141- role : 'window' ,
142- submenu : [ { role : 'minimize' } , { role : 'close' } ] as MenuItemConstructorOptions [ ] ,
143- } ,
144- {
131+ label : i18next . t ( 'menu.help.root' ) ,
145132 role : 'help' ,
146133 submenu : [
147134 {
148- label : 'Learn More' ,
135+ label : i18next . t ( 'menu.help.learnMore' ) ,
149136 click : async ( ) => {
150137 const { shell } = await import ( 'electron' )
151138 await shell . openExternal ( 'https://github.com/YILS-LIN/short-video-factory' )
@@ -181,17 +168,19 @@ app.on('activate', () => {
181168// app.disableHardwareAcceleration();
182169
183170app . whenReady ( ) . then ( ( ) => {
184- createWindow ( )
185171 initSqlite ( )
186- initIPC ( win as BrowserWindow )
172+ initI18n ( )
173+ initIPC ( )
174+ createWindow ( )
175+
176+ i18next . on ( 'languageChanged' , ( ) => {
177+ buildMenu ( )
178+ } )
187179
188180 // 允许跨站请求携带cookie
189181 useCookieAllowCrossSite ( )
190182 // 禁用 CORS
191183 app . commandLine . appendSwitch ( 'disable-features' , 'OutOfBlinkCors' )
192184 // 允许本地网络请求
193185 app . commandLine . appendSwitch ( 'disable-features' , 'BlockInsecurePrivateNetworkRequests' )
194-
195- // Build application menu
196- buildMenu ( )
197186} )
0 commit comments