Making my Python GTK application international

I’ve been trying for the last week to make my application international, what I mean by international is simply translated to other languages depending on the system’s locale.

For better context, here is my application’s github: GitHub - GustavoPeredo/Font-Downloader: Download fonts from the web!

I was able to use gettext to translate certain parts of the application such as the name and About dialog (although I think this is automatically done by the build system):

But other parts like buttons and headerbar texts weren’t translated, even though these are translated in the pt_BR.po file. I also added a print(_(“Download”), where _ = gettext.gettext, but “Download” is also not translated.

Looking around the web I find many solutions on how to “solve” this issue, some are considered hacky, other are more organized (such as Apostrophe’s), but I’d like to know the GNOMEst way of doing translations in python with gettext and GTK.

Any help is very welcome.

I looked at your code, and it seems that you’re not calling gettext.textdomain() anywhere. Possibly you may also need to call gettext.bindtextdomain() if your .mo files are not installed into the system-wide directories (/usr/share/locale/xx_XX/LC_MESSAGES/

I’ve also discovered that you may need to call locale.textdomain()/locale.bindtextdomain() if you want GTK itself to translate its own strings (like stock button titles and such).

If you want a working code example, gtimelog is my Python/GTK application that uses gettext for internationalization.


Actually now I see that you are calling gettext.install() from the meson-generated fontdownloader script, so things should work.

OTOH gettext.install() makes _ available in the Python builtin namespace without any need for explicit imports, so perhaps your explicit import statement overrides it with an unbound version, which breaks everything?

Try removing the import statement and seeing if that helps?


Thank you for checking out my code!

Yes it helped! And a lot! But not completely, but Glade files were still untranslated :/.

I could technically add all labels manually with set_text(_(‘Text’)) and problem solved, but …

I tried doing what you said here:

And everything is working awesomelly! Thank you very much!

1 Like

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