Why is it that text written directly in python file gets translated but the text in interface definition file does not? Is this a bug in GTK4 or am I doing something wrong?
Note
I also tried settings Gtk.Builder's translation domain and adding _ at the start of translatable text in .ui file. None of these methods worked.
Your application is missing calls to bindtextdomain(), bind_textdomain_codeset(), and textdomain(). These are normally needed at the start of your application.
Hmmm… OK sorry, I didn’t read your post closely enough anyway. Try gtk_builder_set_translation_domain(), or alternatively: <interface domain="test-gtk-builder-l10n> in the UI file. Hopefully one of those will work.
I decided to play along at home, and in my testing changing the <interface> tag in the .ui file to <interface domain="test-gtk-builder-l10n"> was enough to enable UI translations… however, the compiled translations also had to be installed as (for this example) /usr/share/locale/ur/LC_MESSAGES/test-gtk-builder-l10n.mo.
I couldn’t find any way to convince Gtk.Builder to load them from a different location, even if I provided a localedir= argument to gettext.install() or gettext.bindtextdomain(). Maybe there’s an environment variable that would’ve worked.