electron menu translation WIP

This commit is contained in:
SPRINX0\prochazka
2025-11-18 09:20:56 +01:00
parent 6e439adb51
commit 7bb9414be8
4 changed files with 35 additions and 7 deletions

View File

@@ -31,6 +31,10 @@ let mainModule;
let appUpdateStatus = ''; let appUpdateStatus = '';
let settingsJson = {}; let settingsJson = {};
function _t(key, { defaultMessage } = {}) {
return global.TRANSLATION_DATA?.[key] || defaultMessage;
}
process.on('uncaughtException', function (error) { process.on('uncaughtException', function (error) {
console.error('uncaughtException', error); console.error('uncaughtException', error);
}); });
@@ -63,6 +67,7 @@ try {
let mainWindow; let mainWindow;
let mainMenu; let mainMenu;
let runCommandOnLoad = null; let runCommandOnLoad = null;
let mainWindowMenuSet = false;
log.transports.file.level = 'debug'; log.transports.file.level = 'debug';
autoUpdater.logger = log; autoUpdater.logger = log;
@@ -93,7 +98,7 @@ function commandItem(item, disableAll = false) {
} }
return { return {
id, id,
label: command ? command.menuName || command.toolbarName || command.name : id, label: command ? _t(command.menuName) || _t(command.toolbarName) || command.name : id,
accelerator: formatKeyText(command ? command.keyText : undefined), accelerator: formatKeyText(command ? command.keyText : undefined),
enabled: command ? command.enabled && (!disableAll || command.systemCommand) : false, enabled: command ? command.enabled && (!disableAll || command.systemCommand) : false,
click() { click() {
@@ -155,11 +160,14 @@ ipcMain.on('update-commands', async (event, arg) => {
const command = commands[key]; const command = commands[key];
// rebuild menu // rebuild menu
if (menu.label != command.text || menu.accelerator != command.keyText) { if (menu.label != command.text || (menu.accelerator != command.keyText && global.TRANSLATION_DATA)) {
mainMenu = buildMenu(isModalOpened || !!dbgatePage); mainMenu = buildMenu(isModalOpened || !!dbgatePage);
Menu.setApplicationMenu(mainMenu); Menu.setApplicationMenu(mainMenu);
// mainWindow.setMenu(mainMenu); if (!mainWindowMenuSet) {
mainWindow.setMenu(mainMenu);
mainWindowMenuSet = true;
}
return; return;
} }
@@ -306,6 +314,12 @@ ipcMain.on('check-for-updates', async (event, url) => {
autoUpdater.autoDownload = false; autoUpdater.autoDownload = false;
autoUpdater.checkForUpdates(); autoUpdater.checkForUpdates();
}); });
ipcMain.on('translation-data', async (event, arg) => {
global.TRANSLATION_DATA = JSON.parse(arg);
mainMenu = buildMenu();
Menu.setApplicationMenu(mainMenu);
mainWindow.setMenu(mainMenu);
});
function fillMissingSettings(value) { function fillMissingSettings(value) {
const res = { const res = {
@@ -382,8 +396,8 @@ function createWindow() {
mainWindow.setFullScreen(true); mainWindow.setFullScreen(true);
} }
mainMenu = buildMenu(); // mainMenu = buildMenu();
mainWindow.setMenu(mainMenu); // mainWindow.setMenu(mainMenu);
function loadMainWindow() { function loadMainWindow() {
const startUrl = const startUrl =

View File

@@ -1,6 +1,10 @@
function _t(key, { defaultMessage }) {
return global.TRANSLATION_DATA?.[key] || defaultMessage;
}
module.exports = ({ editMenu, isMac }) => [ module.exports = ({ editMenu, isMac }) => [
{ {
label: 'File', label: _t('app.databaseName', { defaultMessage: 'DB NAME' }),
submenu: [ submenu: [
{ command: 'new.connection', hideDisabled: true }, { command: 'new.connection', hideDisabled: true },
{ command: 'new.sqliteDatabase', hideDisabled: true }, { command: 'new.sqliteDatabase', hideDisabled: true },

View File

@@ -27,7 +27,7 @@
import SettingsListener from './utility/SettingsListener.svelte'; import SettingsListener from './utility/SettingsListener.svelte';
import { handleAuthOnStartup } from './clientAuth'; import { handleAuthOnStartup } from './clientAuth';
import { initializeAppUpdates } from './utility/appUpdate'; import { initializeAppUpdates } from './utility/appUpdate';
import { _t, saveSelectedLanguageToCache } from './translations'; import { _t, getCurrentTranslations, saveSelectedLanguageToCache } from './translations';
import { installCloudListeners } from './utility/cloudListeners'; import { installCloudListeners } from './utility/cloudListeners';
export let isAdminPage = false; export let isAdminPage = false;
@@ -62,6 +62,11 @@
installCloudListeners(); installCloudListeners();
refreshPublicCloudFiles(); refreshPublicCloudFiles();
saveSelectedLanguageToCache(); saveSelectedLanguageToCache();
const electron = getElectron();
if (electron) {
electron.send('translation-data', JSON.stringify(getCurrentTranslations()));
}
} }
loadedApi = loadedApiValue; loadedApi = loadedApiValue;

View File

@@ -58,6 +58,11 @@ function getTranslation(key: string, defaultMessage: string, language: string) {
return translation; return translation;
} }
export function getCurrentTranslations(): Record<string, string> {
const selectedLanguage = getSelectedLanguage();
return translations[selectedLanguage] || {};
}
export function _t(key: string, options: TranslateOptions): string { export function _t(key: string, options: TranslateOptions): string {
const { defaultMessage, values } = options; const { defaultMessage, values } = options;