gtkgettextglibgtkmm

Embed gettext translation (po) data into binary


Is there a way to embed the contents of po files and bind the text domain (~ bindtextdomain(GETTEXT_PACKAGE,...)) to that embedded data?

The background is that I'm building a GTK application with gtkmm and I'm embedding most of the resources inside the application as gresources. The aim is to make the application as portable as possible as this will not be something that is installed or packaged - so a single binary/executable file is ideal. Currently the translation po files are the only thing I'm not able to embed.

Note: I don't need a GTK/Glib/gresource specific solution.


Solution

  • You state that you do not need a GTK/Glib/gresource specific solution. For many gettext implementations in other languages than C/C++, you can often monkey patch the .mo loading functionality. But with Gtk/GLib/GResource you are probably out of luck. I think it is not even sufficient to override GLib I/O functionality but you would have to override fopen() or even open() from the target system's libc (not GLib). And that will never be portable because you will rely on a specific implementation of gettext() and friends.

    But in your comment you say that you dynamically download "them" (the .mo files?) "if they're not where they should be". That suggests that there is a directory that is writable for your application, and that the location of this directory is known to your application.

    Taken that this assumption is valid, add some initialization code that checks whether the .mo files are "where they should be", and only if they are missing, extract the data from the GResource and write out the .mo files. With bindtextdomain(), you can then force this directory to be used for loading the .mo files.

    Alternative solution: File an issue for GResource. If it is possible to bundle a .ui file but not an .mo file, the concept of GResource seems to be missing an important use case.