mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-11-30 01:00:27 +03:00
fix(electron): localize the tray menu (#15895)
The tray menu labels were hardcoded in English. They now come from i18n keys, and the tray menu is rebuilt when the main process language changes (the renderer already syncs its language to the main process). Adds the Simplified Chinese translations. A unit test builds the tray menu, switches the language to zh-Hans and checks the labels. fix #15684 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * Desktop tray menu labels are now localized in English and Simplified Chinese. * Tray labels update when the app’s language changes, including recording controls, settings, and app information. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: DarkSky <darksky2048@gmail.com>
This commit is contained in:
1 parent
c91b08fbcc
commit
f89487fa20
5 files changed
+240
-15
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { getOrCreateI18n, I18n } from '@affine/i18n';
|
||||
import {
|
||||
app,
|
||||
Menu,
|
||||
@@ -103,7 +104,7 @@ class TrayState implements Disposable {
|
||||
key: 'primary',
|
||||
getConfig: () => [
|
||||
{
|
||||
label: 'Open Journal',
|
||||
label: I18n['com.affine.tray.open-journal'](),
|
||||
icon: icons.journal,
|
||||
click: () => {
|
||||
logger.info('User action: Open Journal');
|
||||
@@ -112,7 +113,7 @@ class TrayState implements Disposable {
|
||||
},
|
||||
},
|
||||
{
|
||||
label: 'New Page',
|
||||
label: I18n['com.affine.tray.new-page'](),
|
||||
icon: icons.page,
|
||||
click: () => {
|
||||
logger.info('User action: New Page');
|
||||
@@ -121,7 +122,7 @@ class TrayState implements Disposable {
|
||||
},
|
||||
},
|
||||
{
|
||||
label: 'New Edgeless',
|
||||
label: I18n['com.affine.tray.new-edgeless'](),
|
||||
icon: icons.edgeless,
|
||||
click: () => {
|
||||
logger.info('User action: New Edgeless');
|
||||
@@ -142,12 +143,12 @@ class TrayState implements Disposable {
|
||||
const items: TrayMenuConfig = [];
|
||||
if (!MeetingsSettingsState.value.enabled) {
|
||||
items.push({
|
||||
label: 'Meetings are disabled',
|
||||
label: I18n['com.affine.tray.meetings-disabled'](),
|
||||
disabled: true,
|
||||
});
|
||||
} else if (!checkCanRecordMeeting()) {
|
||||
items.push({
|
||||
label: 'Required permissions not granted',
|
||||
label: I18n['com.affine.tray.meetings-permissions-missing'](),
|
||||
disabled: true,
|
||||
});
|
||||
} else {
|
||||
@@ -179,11 +180,11 @@ class TrayState implements Disposable {
|
||||
|
||||
items.push(
|
||||
{
|
||||
label: 'Start Recording Meeting',
|
||||
label: I18n['com.affine.tray.start-recording'](),
|
||||
icon: icons.record,
|
||||
submenu: [
|
||||
{
|
||||
label: 'System audio (all audio will be recorded)',
|
||||
label: I18n['com.affine.tray.record-system-audio'](),
|
||||
icon: icons.monitor,
|
||||
click: () => {
|
||||
logger.info(
|
||||
@@ -201,8 +202,10 @@ class TrayState implements Disposable {
|
||||
);
|
||||
} else {
|
||||
const recordingLabel = recordingStatus.appName
|
||||
? `Recording (${recordingStatus.appName})`
|
||||
: 'Recording';
|
||||
? I18n['com.affine.tray.recording-app']({
|
||||
appName: recordingStatus.appName,
|
||||
})
|
||||
: I18n['com.affine.tray.recording']();
|
||||
|
||||
// recording is active
|
||||
items.push(
|
||||
@@ -212,7 +215,7 @@ class TrayState implements Disposable {
|
||||
disabled: true,
|
||||
},
|
||||
{
|
||||
label: 'Stop',
|
||||
label: I18n['com.affine.tray.stop-recording'](),
|
||||
disabled: recordingStatus.status !== 'recording',
|
||||
click: () => {
|
||||
logger.info('User action: Stop Recording');
|
||||
@@ -228,7 +231,7 @@ class TrayState implements Disposable {
|
||||
}
|
||||
if (checkRecordingAvailable()) {
|
||||
items.push({
|
||||
label: `Meetings Settings...`,
|
||||
label: I18n['com.affine.tray.meetings-settings'](),
|
||||
click: () => {
|
||||
activateMainWindow();
|
||||
applicationMenuSubjects.openInSettingModal$.next({
|
||||
@@ -252,14 +255,14 @@ class TrayState implements Disposable {
|
||||
key: 'secondary',
|
||||
getConfig: () => [
|
||||
{
|
||||
label: 'Open AFFiNE',
|
||||
label: I18n['com.affine.tray.open-affine'](),
|
||||
click: () => {
|
||||
logger.info('User action: Open AFFiNE');
|
||||
activateMainWindow();
|
||||
},
|
||||
},
|
||||
{
|
||||
label: 'Menubar settings...',
|
||||
label: I18n['com.affine.tray.menubar-settings'](),
|
||||
click: () => {
|
||||
activateMainWindow();
|
||||
applicationMenuSubjects.openInSettingModal$.next({
|
||||
@@ -269,7 +272,7 @@ class TrayState implements Disposable {
|
||||
},
|
||||
},
|
||||
{
|
||||
label: `About ${app.getName()}`,
|
||||
label: I18n['com.affine.tray.about']({ appName: app.getName() }),
|
||||
click: () => {
|
||||
activateMainWindow();
|
||||
applicationMenuSubjects.openInSettingModal$.next({
|
||||
@@ -279,7 +282,7 @@ class TrayState implements Disposable {
|
||||
},
|
||||
'separator',
|
||||
{
|
||||
label: 'Quit AFFiNE Completely...',
|
||||
label: I18n['com.affine.tray.quit'](),
|
||||
click: () => {
|
||||
logger.info('User action: Quit AFFiNE Completely');
|
||||
app.quit();
|
||||
@@ -333,11 +336,16 @@ class TrayState implements Disposable {
|
||||
logger.debug('App groups updated, refreshing tray menu');
|
||||
this.update();
|
||||
});
|
||||
const languageChangedHandler = () => {
|
||||
this.update();
|
||||
};
|
||||
getOrCreateI18n().on('languageChanged', languageChangedHandler);
|
||||
|
||||
this.disposables.push(() => {
|
||||
this.tray?.off('click', clickHandler);
|
||||
this.tray?.destroy();
|
||||
appGroupsSubscription.unsubscribe();
|
||||
getOrCreateI18n().off('languageChanged', languageChangedHandler);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
import { getOrCreateI18n } from '@affine/i18n';
|
||||
import { BehaviorSubject, Subject } from 'rxjs';
|
||||
import { afterAll, describe, expect, test, vi } from 'vitest';
|
||||
|
||||
const { contextMenus } = vi.hoisted(() => ({
|
||||
contextMenus: [] as { items: { label?: string }[] }[],
|
||||
}));
|
||||
|
||||
vi.mock('electron', () => {
|
||||
const image = {
|
||||
resize: () => image,
|
||||
setTemplateImage: () => {},
|
||||
};
|
||||
class Menu {
|
||||
items: { label?: string }[] = [];
|
||||
append(item: { label?: string }) {
|
||||
this.items.push(item);
|
||||
}
|
||||
}
|
||||
class MenuItem {
|
||||
label?: string;
|
||||
constructor(options: { label?: string }) {
|
||||
this.label = options.label;
|
||||
}
|
||||
}
|
||||
class Tray {
|
||||
setToolTip() {}
|
||||
on() {}
|
||||
off() {}
|
||||
destroy() {}
|
||||
setContextMenu(menu: Menu) {
|
||||
contextMenus.push(menu);
|
||||
}
|
||||
}
|
||||
return {
|
||||
app: { getName: () => 'AFFiNE', quit: vi.fn() },
|
||||
Menu,
|
||||
MenuItem,
|
||||
nativeImage: {
|
||||
createFromPath: () => image,
|
||||
createFromBuffer: () => image,
|
||||
},
|
||||
Tray,
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock('../../src/shared/utils', () => ({
|
||||
isMacOS: () => true,
|
||||
resourcesPath: '/resources',
|
||||
}));
|
||||
|
||||
vi.mock('../../src/main/logger', () => ({
|
||||
logger: { info: vi.fn(), debug: vi.fn(), error: vi.fn() },
|
||||
}));
|
||||
|
||||
vi.mock('../../src/main/cleanup', () => ({
|
||||
beforeAppQuit: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('../../src/main/application-menu', () => ({
|
||||
applicationMenuSubjects: {},
|
||||
}));
|
||||
|
||||
vi.mock('../../src/main/windows-manager', () => ({
|
||||
showMainWindow: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('../../src/main/shared-storage/storage', () => ({
|
||||
globalStateStorage: {
|
||||
get: () => ({ enabled: true }),
|
||||
watch: () => new BehaviorSubject({ enabled: true }),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('../../src/main/recording/feature', () => ({
|
||||
appGroups$: new BehaviorSubject([]),
|
||||
checkCanRecordMeeting: () => true,
|
||||
checkRecordingAvailable: () => false,
|
||||
getCurrentRecordingStatus: () => null,
|
||||
MeetingsSettingsState: { value: { enabled: false } },
|
||||
startRecording: vi.fn(),
|
||||
stopRecording: vi.fn(),
|
||||
updateApplicationsPing$: new Subject(),
|
||||
}));
|
||||
|
||||
describe('tray menu', () => {
|
||||
afterAll(async () => {
|
||||
await getOrCreateI18n().changeLanguage('en');
|
||||
});
|
||||
|
||||
test('labels follow the app language', async () => {
|
||||
const { setupTrayState } = await import('../../src/main/tray');
|
||||
setupTrayState();
|
||||
|
||||
const labels = () => contextMenus.at(-1)?.items.map(item => item.label);
|
||||
|
||||
expect(labels()).toEqual([
|
||||
'Open Journal',
|
||||
'New Page',
|
||||
'New Edgeless',
|
||||
undefined,
|
||||
'Open AFFiNE',
|
||||
'Menubar settings...',
|
||||
'About AFFiNE',
|
||||
undefined,
|
||||
'Quit AFFiNE Completely...',
|
||||
]);
|
||||
|
||||
await getOrCreateI18n().changeLanguage('zh-Hans');
|
||||
|
||||
expect(labels()).toEqual([
|
||||
'打开日记',
|
||||
'新建文档',
|
||||
'新建无界页面',
|
||||
undefined,
|
||||
'打开 AFFiNE',
|
||||
'菜单栏设置...',
|
||||
'关于 AFFiNE',
|
||||
undefined,
|
||||
'完全退出 AFFiNE...',
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -1235,6 +1235,70 @@ export function useAFFiNEI18N(): {
|
||||
* `Start AFFiNE minimized to the system tray.`
|
||||
*/
|
||||
["com.affine.appearanceSettings.menubar.windowBehavior.startMinimized.description"](): string;
|
||||
/**
|
||||
* `Open Journal`
|
||||
*/
|
||||
["com.affine.tray.open-journal"](): string;
|
||||
/**
|
||||
* `New Page`
|
||||
*/
|
||||
["com.affine.tray.new-page"](): string;
|
||||
/**
|
||||
* `New Edgeless`
|
||||
*/
|
||||
["com.affine.tray.new-edgeless"](): string;
|
||||
/**
|
||||
* `Meetings are disabled`
|
||||
*/
|
||||
["com.affine.tray.meetings-disabled"](): string;
|
||||
/**
|
||||
* `Required permissions not granted`
|
||||
*/
|
||||
["com.affine.tray.meetings-permissions-missing"](): string;
|
||||
/**
|
||||
* `Start Recording Meeting`
|
||||
*/
|
||||
["com.affine.tray.start-recording"](): string;
|
||||
/**
|
||||
* `System audio (all audio will be recorded)`
|
||||
*/
|
||||
["com.affine.tray.record-system-audio"](): string;
|
||||
/**
|
||||
* `Recording`
|
||||
*/
|
||||
["com.affine.tray.recording"](): string;
|
||||
/**
|
||||
* `Recording ({{appName}})`
|
||||
*/
|
||||
["com.affine.tray.recording-app"](options: {
|
||||
readonly appName: string;
|
||||
}): string;
|
||||
/**
|
||||
* `Stop`
|
||||
*/
|
||||
["com.affine.tray.stop-recording"](): string;
|
||||
/**
|
||||
* `Meetings Settings...`
|
||||
*/
|
||||
["com.affine.tray.meetings-settings"](): string;
|
||||
/**
|
||||
* `Open AFFiNE`
|
||||
*/
|
||||
["com.affine.tray.open-affine"](): string;
|
||||
/**
|
||||
* `Menubar settings...`
|
||||
*/
|
||||
["com.affine.tray.menubar-settings"](): string;
|
||||
/**
|
||||
* `About {{appName}}`
|
||||
*/
|
||||
["com.affine.tray.about"](options: {
|
||||
readonly appName: string;
|
||||
}): string;
|
||||
/**
|
||||
* `Quit AFFiNE Completely...`
|
||||
*/
|
||||
["com.affine.tray.quit"](): string;
|
||||
/**
|
||||
* `Theme`
|
||||
*/
|
||||
|
||||
@@ -291,6 +291,21 @@
|
||||
"com.affine.appearanceSettings.menubar.windowBehavior.closeToTray.description": "Close AFFiNE to the system tray.",
|
||||
"com.affine.appearanceSettings.menubar.windowBehavior.startMinimized.toggle": "Start minimized",
|
||||
"com.affine.appearanceSettings.menubar.windowBehavior.startMinimized.description": "Start AFFiNE minimized to the system tray.",
|
||||
"com.affine.tray.open-journal": "Open Journal",
|
||||
"com.affine.tray.new-page": "New Page",
|
||||
"com.affine.tray.new-edgeless": "New Edgeless",
|
||||
"com.affine.tray.meetings-disabled": "Meetings are disabled",
|
||||
"com.affine.tray.meetings-permissions-missing": "Required permissions not granted",
|
||||
"com.affine.tray.start-recording": "Start Recording Meeting",
|
||||
"com.affine.tray.record-system-audio": "System audio (all audio will be recorded)",
|
||||
"com.affine.tray.recording": "Recording",
|
||||
"com.affine.tray.recording-app": "Recording ({{appName}})",
|
||||
"com.affine.tray.stop-recording": "Stop",
|
||||
"com.affine.tray.meetings-settings": "Meetings Settings...",
|
||||
"com.affine.tray.open-affine": "Open AFFiNE",
|
||||
"com.affine.tray.menubar-settings": "Menubar settings...",
|
||||
"com.affine.tray.about": "About {{appName}}",
|
||||
"com.affine.tray.quit": "Quit AFFiNE Completely...",
|
||||
"com.affine.appearanceSettings.theme.title": "Theme",
|
||||
"com.affine.appearanceSettings.title": "Appearance settings",
|
||||
"com.affine.appearanceSettings.translucentUI.description": "Use transparency effect on the sidebar.",
|
||||
|
||||
@@ -263,6 +263,21 @@
|
||||
"com.affine.appearanceSettings.menubar.windowBehavior.closeToTray.description": "关闭AFFiNE时最小化到系统托盘。",
|
||||
"com.affine.appearanceSettings.menubar.windowBehavior.startMinimized.toggle": "启动时最小化",
|
||||
"com.affine.appearanceSettings.menubar.windowBehavior.startMinimized.description": "启动AFFiNE时最小化到系统托盘。",
|
||||
"com.affine.tray.open-journal": "打开日记",
|
||||
"com.affine.tray.new-page": "新建文档",
|
||||
"com.affine.tray.new-edgeless": "新建无界页面",
|
||||
"com.affine.tray.meetings-disabled": "会议已禁用",
|
||||
"com.affine.tray.meetings-permissions-missing": "未授予所需权限",
|
||||
"com.affine.tray.start-recording": "开始录制会议",
|
||||
"com.affine.tray.record-system-audio": "系统音频(将录制所有音频)",
|
||||
"com.affine.tray.recording": "正在录制",
|
||||
"com.affine.tray.recording-app": "正在录制({{appName}})",
|
||||
"com.affine.tray.stop-recording": "停止",
|
||||
"com.affine.tray.meetings-settings": "会议设置...",
|
||||
"com.affine.tray.open-affine": "打开 AFFiNE",
|
||||
"com.affine.tray.menubar-settings": "菜单栏设置...",
|
||||
"com.affine.tray.about": "关于 {{appName}}",
|
||||
"com.affine.tray.quit": "完全退出 AFFiNE...",
|
||||
"com.affine.appearanceSettings.theme.title": "主题",
|
||||
"com.affine.appearanceSettings.title": "外观设置",
|
||||
"com.affine.appearanceSettings.translucentUI.description": "在侧边栏使用半透明效果。",
|
||||
|
||||
Reference in new issue
Block a user