[Python] i18n at GTK4 and Python

Discussão em 'Python' iniciado por Stack, Setembro 1, 2025.

  1. Stack

    Stack Membro Participativo

    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, the 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? I changed the user language in Ubuntu. The menu items of the other programs then showed "Strg", but not my program.

    In Windows, the locale approach does not work. Simple gettext in Python has no link to GTK. I used the latest release from text. 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 want to use different hotkeys for different user languages what kind of approach would you recommend?

    Kind regards, Andreas

    Continue reading...

Compartilhe esta Página