Skip to content

Commit

Permalink
Allow runtime translation of Electron menus (#3134)
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermemntt authored Feb 25, 2021
1 parent d088bf0 commit f6ed252
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 48 deletions.
7 changes: 6 additions & 1 deletion app/actions/SettingsActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import {
resetInventoryAndProposals
} from "actions/GovernanceActions";
import * as configConstants from "constants/config";
import { ipcRenderer } from "electron";

export const SETTINGS_SAVE = "SETTINGS_SAVE";
export const SETTINGS_CHANGED = "SETTINGS_CHANGED";
export const SETTINGS_UNCHANGED = "SETTINGS_UNCHANGED";

export const saveSettings = (settings) => async (dispatch, getState) => {
const {
settings: { needNetworkReset }
settings: { needNetworkReset, currentSettings: { locale } }
} = getState();
const {
daemon: { walletName }
Expand Down Expand Up @@ -69,6 +70,10 @@ export const saveSettings = (settings) => async (dispatch, getState) => {
wallet.reloadAllowedExternalRequests();
}

if (locale != settings.locale) {
ipcRenderer.sendSync("change-menu-locale", settings.locale);
}

const newDcrdataEnabled =
settings.allowedExternalRequests.indexOf(EXTERNALREQUEST_DCRDATA) > -1;
if (newDcrdataEnabled === true) {
Expand Down
81 changes: 48 additions & 33 deletions app/main.development.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,53 @@ ipcMain.on("get-cli-options", (event) => {
event.returnValue = cliOptions;
});

function setMenuLocale(locale) {
//Removes previous listeners of "context-menu" event.
mainWindow.webContents._events["context-menu"] = [];

mainWindow.webContents.on("context-menu", (e, props) => {
const { selectionText, isEditable, x, y } = props;
const inptMenu = inputMenu(
process.env.NODE_ENV === "development",
mainWindow,
x,
y,
locale
);
const slctionMenu = selectionMenu(
process.env.NODE_ENV === "development",
mainWindow,
x,
y,
locale
);

if (isEditable) {
Menu.buildFromTemplate(inptMenu).popup(mainWindow);
} else if (selectionText && selectionText.trim() !== "") {
Menu.buildFromTemplate(slctionMenu).popup(mainWindow);
} else if (process.env.NODE_ENV === "development") {
Menu.buildFromTemplate([
{
label: "Inspect element",
click: () => mainWindow.inspectElement(x, y)
}
]).popup(mainWindow);
}
});

const template = initTemplate(mainWindow, locale);

menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}

ipcMain.on("change-menu-locale", (event, newLocaleKey) => {
const locale = locales.find((value) => value.key === newLocaleKey);
setMenuLocale(locale);
event.returnValue = true;
});

const primaryInstance = app.requestSingleInstanceLock();
const stopSecondInstance = !primaryInstance && !daemonIsAdvanced;
if (stopSecondInstance) {
Expand Down Expand Up @@ -615,39 +662,7 @@ app.on("ready", async () => {

if (stopSecondInstance) return;

mainWindow.webContents.on("context-menu", (e, props) => {
const { selectionText, isEditable, x, y } = props;
const inptMenu = inputMenu(
process.env.NODE_ENV === "development",
mainWindow,
x,
y
);
const slctionMenu = selectionMenu(
process.env.NODE_ENV === "development",
mainWindow,
x,
y
);

if (isEditable) {
Menu.buildFromTemplate(inptMenu).popup(mainWindow);
} else if (selectionText && selectionText.trim() !== "") {
Menu.buildFromTemplate(slctionMenu).popup(mainWindow);
} else if (process.env.NODE_ENV === "development") {
Menu.buildFromTemplate([
{
label: "Inspect element",
click: () => mainWindow.inspectElement(x, y)
}
]).popup(mainWindow);
}
});

const template = initTemplate(mainWindow, locale);

menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
setMenuLocale(locale);
});

app.on("before-quit", (event) => {
Expand Down
28 changes: 14 additions & 14 deletions app/main_dev/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,17 @@ export const getGrpcVersions = () => grpcVersions;

export const setGrpcVersions = (versions) => (grpcVersions = versions);

const inputMenuRoles = [
{ role: "cut" },
{ role: "copy" },
{ role: "paste" },
const inputMenuRoles = (locale) => [
{ label: locale.messages["appMenu.cut"], role: "cut" },
{ label: locale.messages["appMenu.copy"], role: "copy" },
{ label: locale.messages["appMenu.paste"], role: "paste" },
{ type: "separator" },
{ role: "selectall" }
{ label: locale.messages["appMenu.selectAll"], role: "selectall" }
];
const selectionMenuRoles = [
{ role: "copy" },
const selectionMenuRoles = (locale) => [
{ label: locale.messages["appMenu.copy"], role: "copy" },
{ type: "separator" },
{ role: "selectall" }
{ label: locale.messages["appMenu.selectAll"], role: "selectall" }
];

const inspectElement = (mainWindow, x, y) => {
Expand All @@ -201,12 +201,12 @@ const inspectElement = (mainWindow, x, y) => {
};
};

export const inputMenu = (isDevelopment, mainWindow, x, y) =>
export const inputMenu = (isDevelopment, mainWindow, x, y, locale) =>
isDevelopment
? [...inputMenuRoles, inspectElement(mainWindow, x, y)]
: inputMenuRoles;
? [...inputMenuRoles(locale), inspectElement(mainWindow, x, y)]
: inputMenuRoles(locale);

export const selectionMenu = (isDevelopment, mainWindow, x, y) =>
export const selectionMenu = (isDevelopment, mainWindow, x, y, locale) =>
isDevelopment
? [...selectionMenuRoles, inspectElement(mainWindow, x, y)]
: selectionMenuRoles;
? [...selectionMenuRoles(locale), inspectElement(mainWindow, x, y)]
: selectionMenuRoles(locale);

0 comments on commit f6ed252

Please sign in to comment.