fix(electron): do not return the translation function over IPC (#15901)

The `i18n.changeLanguage` IPC handler returned the result of i18next's
`changeLanguage`, which is a translation function. A function cannot be
structured-cloned, so every call from the renderer rejected with
`DataCloneError` even though the language had changed in the main
process.

The handler now awaits the change and returns nothing. A unit test calls
the handler and checks the language changed and the result can be
cloned; on canary it fails with `DataCloneError: (...args) =>
this.t(...args) could not be cloned.`

Found by @keepClamDown in #15616.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Language changes continue to apply as requested, while the request no
longer returns an internal result.
* **Tests**
* Added coverage for changing the application language and restoring the
default language after testing.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: DarkSky <darksky2048@gmail.com>
This commit is contained in:
CommaBotandDarkSky authored and GitHub committed 2026-11-23 22:54:13 +08:00
1 parent a28cad2999
commit bd338f8698
2 files changed
+50 -1

No files matched your search

@@ -28,7 +28,7 @@ export const debugHandlers = {
export const i18nHandlers = {
changeLanguage: async (_: Electron.IpcMainInvokeEvent, language: string) => {
return I18n.changeLanguage(language);
await I18n.changeLanguage(language);
},
};
@@ -0,0 +1,49 @@
import { afterAll, describe, expect, test, vi } from 'vitest';
vi.mock('electron', () => ({ ipcMain: { handle: vi.fn(), on: vi.fn() } }));
vi.mock('../../src/main/auth/handlers', () => ({ authHandlers: {} }));
vi.mock('../../src/main/byok-storage/handlers', () => ({
byokStorageHandlers: {},
}));
vi.mock('../../src/main/clipboard', () => ({ clipboardHandlers: {} }));
vi.mock('../../src/main/config-storage', () => ({
configStorageHandlers: {},
}));
vi.mock('../../src/main/find-in-page', () => ({ findInPageHandlers: {} }));
vi.mock('../../src/main/import', () => ({ importHandlers: {} }));
vi.mock('../../src/main/logger', () => ({
getLogFilePath: vi.fn(),
logger: { debug: vi.fn(), error: vi.fn() },
revealLogFile: vi.fn(),
}));
vi.mock('../../src/main/recording', () => ({ recordingHandlers: {} }));
vi.mock('../../src/main/security-restrictions', () => ({
checkSource: () => true,
}));
vi.mock('../../src/main/shared-storage', () => ({
sharedStorageHandlers: {},
}));
vi.mock('../../src/main/ui/handlers', () => ({ uiHandlers: {} }));
vi.mock('../../src/main/updater', () => ({ updaterHandlers: {} }));
vi.mock('../../src/main/windows-manager/popup', () => ({ popupHandlers: {} }));
vi.mock('../../src/main/worker/handlers', () => ({ workerHandlers: {} }));
import { I18n } from '@affine/i18n';
import { i18nHandlers } from '../../src/main/handlers';
describe('i18n IPC handlers', () => {
afterAll(async () => {
await I18n.changeLanguage('en');
});
test('changeLanguage switches the language and returns a cloneable result', async () => {
const result = await i18nHandlers.changeLanguage(
{} as Electron.IpcMainInvokeEvent,
'zh-Hans'
);
expect(I18n.language).toBe('zh-Hans');
expect(() => structuredClone(result)).not.toThrow();
});
});