I18n at GTK4 and Python

Hi,
I’m writing a Python app with GTK4 for Linux (Ubuntu 24) and Windows. I’m still a beginner.

This is my menu:

<?xml version='1.0' encoding='UTF-8'?>
<interface>
  <requires lib="gio" version="2.0"/>
  <menu id="menubar">
    <submenu>
      <attribute name="label" translatable="yes">_File</attribute>
     <section>
        <item>
          <attribute name="action">app.new</attribute>
          <attribute name="label" translatable="yes" >_New</attribute>
         <attribute name="accel">&lt;Ctrl&gt;n</attribute>
        </item>
        <item>
        <attribute name="label" translatable="yes">_Quit</attribute>
        <attribute name="action">app.quit</attribute>
        <attribute name="accel">&lt;Primary&gt;q</attribute> 
      </item>
      </section>
    </submenu>
  </menu>
</interface>

In Linux, my menu items will be translated, but not the keyboard labels:

os.environ["LANG"] = "de_DE.utf8"
os.environ["LANGUAGE"] = "de_DE.utf8"
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
locale.bindtextdomain("test-menu", "locale")
locale.textdomain("test-menu")

In English, I see “New Ctrl+N” and “Quit Ctrl+Q”. In German, I see “Neu Ctrl+N” and “Beenden Ctrl+Q”. How can I ensure that when I select German there is a Strg+N or Strg+Q and no Ctrl+N or Ctrl+Q?

In Windows, the locale approach does not work. Simple gettext in Python has no link to GTK. I used the latest release from https://github.com/wingtk/gvsbuild?tab=readme-ov-file.
So, I tried this, also with de and de_DE and the menu items “File”, “New” and “Quit” weren’t translated. But, I see Strg+N and Strg+Q:

os.environ["LANGUAGE"] = "de_DE.utf8"
os.environ["LANG"] = "de_DE.utf8"
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
libintl = ctypes.cdll.LoadLibrary(r"c:\gtk\bin\intl.dll")
libintl.bindtextdomain("test-menu", "locale")
libintl.textdomain("test-menu")

How can I make these menu items localize in Windows?

With plain text, I use po and mo files. If I also want to use different hotkeys for different user languages what kind of approach would you recommend? Maybe different ui files?

Kind regards,
Andreas

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.