Search code examples
javascriptnode.jselectronmenuitemrenderer

How to properly update electron application menu labels?


I'm trying to add translate support on an electron application, but I've encountered problems when updating the Application menu. I mention that the development is done & tested on macOS, but I'm willing to get it working on Windows too.

I understand that the menu feature is something that can be managed only in the main process, so I'm using ipcMain & ipcRenderer to communicate between the main & renderer process.

The problem is that I'm updating one label, it seems to be updated (shown in logs) but it does not update it in rendered menu.

    //Menu.getApplicationMenu() is in english
    
    mainMenu.language = "nl_nl";
    //updating 1 label
    mainMenu.object.items[0].submenu.items[0].label =i18n.__({phrase: 'Hello', locale: mainMenu.language}),
    
    Menu.setApplicationMenu(mainMenu.object);
    let currentMenu = Menu.getApplicationMenu();
    //currentMenu reflects the change but the rendered menu still shows the old value (english)


Solution

  • I think your missing Menu.buildFromTemplate(mainMenu.object).

    To use Menu.setApplicationMenu(template) you first need to run Menu.buildFromTemplate(mainMenu.object) on the template to convert it into something Menu.setApplicationMenu(template) can pick up.

    It would be something like this:

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

    Ref.