[Translations] How can I change the translations of an app live?

Im making an app and I want to change the language on the fly, I have the code done/kinda stuff that gets written with _(“”) works but UI doesn’t and I don’t know how to redraw it/etc without restarting

You can’t: the existing localisation API provided by the C standard library—gettext—is based on loading the localised strings from “catalogues”, and uses environment variables to select the current language. The environment variables are only read when an application starts. On top of that, there is no mechanism that tells all the libraries to reload, and rebuild the text in the UI.

okay well can you somehow recommend on how I can maybe do that? Since I don’t think an OS installer in just English is good

either with somehow restarting the whole app
but keeping pythons variables intact?

But then how/why can I manually tell get text to get new translations (other language) and then every next print works even UI’s which are created directly in gtk but not ones from builder/.UI files

(Please edit your replies, instead of writing three of them in short sequence; it makes replying to you a pain)

Not with the current localisation machinery, such as it exists today.

You cannot “keep pythons variables intact”, whatever that entails. You’d have to quit and then restart the whole process, and change the locale in between.

I have zero idea of what you’re doing, or how, as you’ve been conspicuously thin on the details; not that it matters: you cannot safely reload the translation catalogues on a running process.

That’s just how things work, and you will have to live with it.

Its possible… at least on QT (one app I can name is calamares).

id like to have the same feature as them

Qt does not use gettext for localisation.

Then you’ll have to change the localisation infrastructure of everything based on the GNU libc.

We do this in gnome-initial-setup, though, so you can look into what it’s doing.

Although there’s no safe way to change the locale for a process, you can change the locale for a thread, and in practice generally changing the locale for the main thread is sufficient. I guess that’s enough for gettext to work?

It really is not, but it depends on the complexity of your UI.

Changing the locale means tearing down the whole UI, and rebuilding it, because things like labels do know when a locale has changed. All the strings in your UI need to be reset, and the widget templates need to be reloaded.

Yeah, it looks like gnome-initial-setup manually calls gettext() again for every UI element after changing the locale with uselocale(). So that’s not fun but if you really need a language chooser for whatever reason, that’s the way to go.

Fortunately it looks like changing environment variables is not required; if it were, then you’d have to spawn a new process because there is really no safe way to change the environment for a running process. (We actually once upon a time attempted to reset language environment variables in the language chooser in a downstream version of gnome-initial-setup, which resulted in crashes, so definitely do not attempt that.)

Can u point me to where exactly that is located?

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