Translation doens't work in python3 and gtk.builder

I have a python3/Gtk3 application that uses Gtk.Builder, and I’m unable to make it work translated. The texts set from the code (using _(“blah blah”)) are translated, and also the standard buttons in the windows (like a standard button with the “Cancel” text inside), but none of the texts in my .ui files are translated. The odd thing is that this code did work until about two years ago, which suddenly stopped working.

This was the original code:

Gtk.init(sys.argv)

gettext.bindtextdomain(config_data.gettext_domain, config_data.share_locale)
try:
    locale.setlocale(locale.LC_ALL, "")
except locale.Error:
    pass
locale.bindtextdomain(config_data.gettext_domain, config_data.share_locale)
gettext.textdomain(config_data.gettext_domain)
gettext.install(config_data.gettext_domain, localedir=config_data.share_locale)

Did try with every possible combinations of the previous code. Also tried putting the .mo files directly at /usr/share/locale instead of using a custom folder, and then do

gettext.install(config_data.gettext_domain)
locale.textdomain(config_data.gettext_domain)
Gtk.init(sys.argv)

but the result is exactly the same: no text from the .ui files is translated, any other text is.

I also tried setting the domain name both using set_translation_domain() method in the Gtk.Builder, and directly in the XML. But again no dice.

How can I fix this?

Hi,

Does this helps?

Thanks, but unfortunately it doesn’t help. Still happens the same :frowning:

Hi @Rastersoft, for Gaphor we have a function to translate the .ui files:

import defusedxml.ElementTree as etree

def translated_ui_string(package: str, ui_filename: str) -> str:
    with (importlib.resources.files(package) / ui_filename).open(
        encoding="utf-8"
    ) as ui_file:
        ui_xml = etree.parse(ui_file)
    for node in ui_xml.findall(".//*[@translatable='yes']"):
        node.text = gettext(node.text) if node.text else ""
        del node.attrib["translatable"]
    return etree.tostring(ui_xml.getroot(), encoding="unicode", method="xml")

Maybe you could do something similar?

1 Like

Maybe it can work… I’ll try it. Thanks!

Ok, I found the problem: intltool-update doesn’t extract strings from .ui files anymore!!! I tried with msgmerge and it fixed it.

1 Like