Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. "Preferences" menu item on Mac OS: setting QAction::PreferencesRole breaks localization
Forum Update on Monday, May 27th 2025

"Preferences" menu item on Mac OS: setting QAction::PreferencesRole breaks localization

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by
    #1

    My application uses my own translator that loads strings from my own localization files (not .qm). No problem here. Now, consider Mac OS: some menu items, like Preferences, About and Exit, need special treatment - they should be placed in the "Application Name" category in the menu. To achieve that, I set the appropriate role for that menu item:

    ui.actionPreferences->setMenuRole(QAction::PreferencesRole);
    

    Problem: this works, but when I switch the application to another language, this item is still called "Preferences" - it remains English. Even calling ui.actionPreferences->setText("12345"); after switching the language has no effect.

    How can I fix this? The problem is two-fold:

    1. I want to supply my own translations;
    2. even if I'm going to agree on using Qt's default names for these menu items, it's not clear how I can combine that with my own Translator class. Certainly not looking forward to including the .qm files in my .app, either.
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      I think you can access this menu via Cocoa and change the titles yourself.

      You can create a special class for calling Cocoa methods like that:
      CocoaBridge.h

      #ifndef COCOABRIDGE_H
      #define COCOABRIDGE_H
      
      class CocoaBridge
      {
      
      		CocoaBridge() {}
      
      	public:
      
          static void setMainMenu();
      
      };
      
      #endif // COCOABRIDGE_H
      

      CocoaBridge.mm

      #import <Cocoa/Cocoa.h>
      
      void CocoaBridge::setMainMenu()
      {
          NSMenu* menu=[NSApp mainMenu];
          NSMenu* menuApp=[[menu itemAtIndex:0] submenu];  // Application menu
          NSLog(@"%@",[menuApp itemArray]);
      // change the titles here
      }
      

      You need to add the folowing in your .pro file

      macx{
      
          HEADERS +=		CocoaBridge.h
      
       OBJECTIVE_SOURCES +=	CocoaBridge.mm 
      
       LIBS +=		-framework AppKit
      }
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        See the macOS specifics here for handling the application menu translation.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        V 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          See the macOS specifics here for handling the application menu translation.

          V Offline
          V Offline
          Violet Giraffe
          wrote on last edited by
          #4

          @SGaist said in "Preferences" menu item on Mac OS: setting QAction::PreferencesRole breaks localization:

          Hi,

          See the macOS specifics here for handling the application menu translation.

          Thank you, great answer as always. Don't know why I couldn't Google that, duh.
          Anyway, one thing is unclear from that guide: how can I (if at all possible) retranslate the menus at runtime? My application does support live selection of the language (application relaunch not required).

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved