托盘菜单添加多语言支持(#1802)

pull/1072/merge
lyswhut 2024-03-03 19:20:20 +08:00
parent 03781ea692
commit 9d8fe4be06
4 changed files with 70 additions and 13 deletions

View File

@ -7,6 +7,7 @@
- 主题设置默认折叠其他主题以优化进入设置界面时的性能
- 不再丢弃kg源逐行歌词@helloplhm-qwq
- 支持kw源排行榜显示大小revert @Folltoshe #1460
- 托盘菜单添加多语言支持(#1802
### 修复

View File

@ -1,4 +1,4 @@
import type { I18n } from '@root/lang/i18n'
import type { I18n } from '../../lang/i18n'
declare global {

View File

@ -19,7 +19,8 @@ const watchConfigKeys = [
'desktopLyric.isAlwaysOnTop',
'tray.themeId',
'tray.enable',
] as const
'common.langId',
] satisfies Array<keyof LX.AppSetting>
const themeList = [
{
@ -39,6 +40,56 @@ const themeList = [
},
]
const messages = {
'en-us': {
hide_win_main: 'Hide Main Window',
show_win_main: 'Show Main Window',
hide_win_lyric: 'Close desktop lyrics',
show_win_lyric: 'Open desktop lyrics',
lock_win_lyric: 'Lock desktop lyrics',
unlock_win_lyric: 'Unlock desktop lyrics',
top_win_lyric: 'Set top lyrics',
untop_win_lyric: 'Cancel top lyrics',
exit: 'Exit',
},
'zh-cn': {
hide_win_main: '隐藏主界面',
show_win_main: '显示主界面',
hide_win_lyric: '关闭桌面歌词',
show_win_lyric: '开启桌面歌词',
lock_win_lyric: '锁定桌面歌词',
unlock_win_lyric: '解锁桌面歌词',
top_win_lyric: '置顶歌词',
untop_win_lyric: '取消置顶',
exit: '退出',
},
'zh-tw': {
hide_win_main: '隱藏主界面',
show_win_main: '顯示主界面',
hide_win_lyric: '關閉桌面歌詞',
show_win_lyric: '開啟桌面歌詞',
lock_win_lyric: '鎖定桌面歌詞',
unlock_win_lyric: '解鎖桌面歌詞',
top_win_lyric: '置頂歌詞',
untop_win_lyric: '取消置頂',
exit: '退出',
},
} as const
type Messages = typeof messages
type Langs = keyof Messages
const i18n = {
message: messages['zh-cn'] as Messages[Langs],
fallbackLocale: 'en-us' as 'en-us',
getMessage(key: keyof Messages[Langs]) {
return this.message[key]
},
setLang(lang?: Langs | null) {
this.message = lang
? messages[lang] ?? messages[this.fallbackLocale]
: messages[this.fallbackLocale]
},
}
export const createTray = () => {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if ((tray && !tray.isDestroyed()) || !global.lx.appSetting['tray.enable']) return
@ -50,7 +101,7 @@ export const createTray = () => {
// 托盘
tray = new Tray(nativeImage.createFromPath(iconPath))
tray.setToolTip('洛雪音乐助手')
tray.setToolTip('LX Music')
createMenu()
tray.setIgnoreDoubleClickEvents(true)
tray.on('click', () => {
@ -76,13 +127,13 @@ export const createMenu = () => {
const isShow = isShowMainWindow()
menu.push(isShow
? {
label: '隐藏主界面',
label: i18n.getMessage('hide_win_main'),
click() {
hideMainWindow()
},
}
: {
label: '显示主界面',
label: i18n.getMessage('show_win_main'),
click() {
showMainWindow()
},
@ -90,45 +141,45 @@ export const createMenu = () => {
}
menu.push(global.lx.appSetting['desktopLyric.enable']
? {
label: '关闭桌面歌词',
label: i18n.getMessage('hide_win_lyric'),
click() {
handleUpdateConfig({ 'desktopLyric.enable': false })
},
}
: {
label: '开启桌面歌词',
label: i18n.getMessage('show_win_lyric'),
click() {
handleUpdateConfig({ 'desktopLyric.enable': true })
},
})
menu.push(global.lx.appSetting['desktopLyric.isLock']
? {
label: '解锁桌面歌词',
label: i18n.getMessage('unlock_win_lyric'),
click() {
handleUpdateConfig({ 'desktopLyric.isLock': false })
},
}
: {
label: '锁定桌面歌词',
label: i18n.getMessage('lock_win_lyric'),
click() {
handleUpdateConfig({ 'desktopLyric.isLock': true })
},
})
menu.push(global.lx.appSetting['desktopLyric.isAlwaysOnTop']
? {
label: '取消置顶',
label: i18n.getMessage('untop_win_lyric'),
click() {
handleUpdateConfig({ 'desktopLyric.isAlwaysOnTop': false })
},
}
: {
label: '置顶歌词',
label: i18n.getMessage('top_win_lyric'),
click() {
handleUpdateConfig({ 'desktopLyric.isAlwaysOnTop': true })
},
})
menu.push({
label: '退出',
label: i18n.getMessage('exit'),
click() {
quitApp()
},
@ -157,8 +208,11 @@ const init = () => {
}
export default () => {
global.lx.event_app.on('updated_config', (keys) => {
global.lx.event_app.on('updated_config', (keys, setting) => {
if (!watchConfigKeys.some(key => keys.includes(key))) return
if (keys.includes('common.langId')) i18n.setLang(setting['common.langId'])
init()
})
@ -184,6 +238,7 @@ export default () => {
})
global.lx.event_app.on('app_inited', () => {
i18n.setLang(global.lx.appSetting['common.langId'])
init()
})
}

View File

@ -87,6 +87,7 @@ export const watchConfigKeys = [
export const buildLyricConfig = (appSetting: Partial<LX.AppSetting>): Partial<LX.DesktopLyric.Config> => {
const setting: Partial<LX.DesktopLyric.Config> = {}
for (const key of watchConfigKeys) {
// @ts-expect-error
if (key in appSetting) setting[key] = appSetting[key]
}
return setting