[PyQt] Mac QMenuBar localization issues

Hans-Peter Jansen hpj at urpla.net
Mon Jun 29 01:26:35 BST 2009


Am Sonntag, 28. Juni 2009 schrieb Hans-Peter Jansen:
> Dear Mac-attending audience,
>
> after learning, how to get my application name into the Mac menu bar, I'm
> stuck again: did anybody of you succeed in localization of the special
> Mac menu actions, that (should) get magically moved into the Mac
> Application menu? (E.g. About App, About Qt, Preferences, Quit)
>
> In the fine docs, I found the note, that those menu items have to be
> translated in QMenuBar context. Hmm, since we usually add QActions to
> menu bars, I didn't grasp, how this translates to proper PyQt code.

Okay, that one was easy, it translates simply to:
	QtGui.QApplication.translate("QMenuBar", "...")

The reason, why this didn't worked before was pure sillyness on my side. :-(

> In my QAction invocation, I did something like:
> 	QtGui.QMenuBar.tr(self.cfg.mac and QtGui.QMenuBar() or
>                           self.menuBar(), "About %1").arg(self.cfg.app)
>
> but that simply fails to translate that menu item.

Funny enough, I got it working _partly_ now: "About App" and "About Qt" are 
fine, but neither the "Preferences..." nor the "Quit App" appear 
translated. The "Preferences..." menu item is especially strange, since it 
got moved, and it is operational (calls my settings widget).

More notes:
I've studied "src/gui/widgets/qmenu_mac.mm", where all the (black!) magic 
happens in qt_mac_menu_merge_action() and qt_mac_menu_merge_text(), and I 
wonder, why the supplied qt_de.qm doesn't carry any translation for 
QMenuBar::tr("Config") and friends. I can't imagine, how this can work, if 
one trusts purely on QAction.TextHeuristicRole. As noted before, I 
workarounded this by supplying the specific menu roles in the QAction, and 
aslo provide translations for all interesting messages now.

Next, I've intercepted QTranslator.translate() in order to catch what's 
really going on, and here it is:

ctx: MainWindow, msg: Plain text, dis: None -> Roher Text
ctx: QMenuBar, msg: Quit %1, dis: None -> %1 beenden
ctx: QMenuBar, msg: Quit, dis: None -> Beenden
ctx: QMenuBar, msg: Preferences, dis: None -> Einstellungen...
ctx: QMenuBar, msg: Preference, dis: None -> Einstellung
ctx: QMenuBar, msg: About %1, dis: None -> Über %1
ctx: QMenuBar, msg: About Qt, dis: None -> Über Qt
ctx: QMenuBar, msg: File, dis: None -> Ablage
ctx: MainWindow, msg: &New, dis: None -> &Neu

These are expected, as they come from my code, but then [abbreviated]:

ctx: QMenuBar, msg: Config, dis: None -> 
ctx: QMenuBar, msg: Config, dis: None -> 
ctx: QMenuBar, msg: Preference, dis: None -> Einstellung
ctx: QMenuBar, msg: Options, dis: None -> 
ctx: QMenuBar, msg: Options, dis: None -> 
ctx: QMenuBar, msg: Setting, dis: None -> Einstellung
ctx: QMenuBar, msg: Setup, dis: None -> 
ctx: QMenuBar, msg: Setup, dis: None -> 
ctx: QMenuBar, msg: Quit, dis: None -> Beenden
ctx: QMenuBar, msg: Exit, dis: None -> 
ctx: QMenuBar, msg: Exit, dis: None -> 
ctx: QMenuBar, msg: About, dis: None -> 
ctx: QMenuBar, msg: About, dis: None -> 
ctx: QMenuBar, msg: Config, dis: None -> 
ctx: QMenuBar, msg: Config, dis: None -> 
ctx: QMenuBar, msg: Preference, dis: None -> Einstellung
ctx: QMenuBar, msg: Options, dis: None -> 
ctx: QMenuBar, msg: Options, dis: None -> 
ctx: QMenuBar, msg: Setting, dis: None -> Einstellung
ctx: QMenuBar, msg: Setup, dis: None -> 
ctx: QMenuBar, msg: Setup, dis: None -> 
ctx: QMenuBar, msg: Quit, dis: None -> Beenden
ctx: QMenuBar, msg: Exit, dis: None -> 
ctx: QMenuBar, msg: Exit, dis: None -> 
ctx: QMenuBar, msg: About, dis: None -> 
ctx: QMenuBar, msg: About, dis: None -> 
ctx: QMenuBar, msg: Config, dis: None -> 
ctx: QMenuBar, msg: Config, dis: None -> 
ctx: QMenuBar, msg: Preference, dis: None -> Einstellung
ctx: QMenuBar, msg: Options, dis: None -> 
ctx: QMenuBar, msg: Options, dis: None -> 
ctx: QMenuBar, msg: Setting, dis: None -> Einstellung
ctx: QMenuBar, msg: Setup, dis: None -> 
ctx: QMenuBar, msg: Setup, dis: None -> 
ctx: QMenuBar, msg: Quit, dis: None -> Beenden
ctx: QMenuBar, msg: Exit, dis: None -> 
ctx: QMenuBar, msg: Exit, dis: None -> 
ctx: QMenuBar, msg: About, dis: None -> 
ctx: QMenuBar, msg: About, dis: None -> 
ctx: QMenuBar, msg: Config, dis: None -> 
ctx: QMenuBar, msg: Config, dis: None -> 
ctx: QMenuBar, msg: Preference, dis: None -> Einstellung
ctx: QMenuBar, msg: Options, dis: None -> 
ctx: QMenuBar, msg: Options, dis: None -> 
ctx: QMenuBar, msg: Setting, dis: None -> Einstellung
ctx: QMenuBar, msg: Setup, dis: None -> 
ctx: QMenuBar, msg: Setup, dis: None -> 
ctx: QMenuBar, msg: Quit, dis: None -> Beenden
ctx: QMenuBar, msg: Exit, dis: None -> 
ctx: QMenuBar, msg: Exit, dis: None -> 

These shouldn't trigger, since they come from "case 
QAction::TextHeuristicRole:" in qmenu_mac.mm::qt_mac_menu_merge_action(), 
but even then, I've no idea, what prevents them from doing the right thing.

ctx: QMenuBar, msg: About %1, dis: None -> Über %1
ctx: QMenuBar, msg: About Qt, dis: None -> Über Qt

These come from qt_mac_menu_merge_text(), but it escapes me, why the similar 
PreferencesRole/QuitRole QActions does not behave right, although printing 
the editSettingsAction.menuRole() says 5 as expected.

Puzzled,
Pete



More information about the PyQt mailing list