Congratulations to our 2022 Qt Champions!

Dynamic translations from external source

  • So, this is my problem:
    I have an app which does some langauge translation functionality.
    It does translations by calling a web server app.

    I'd like to make the UI of my app dynamically translational.
    Just to note, that I'm not talking about standard dynamic translation (on the fly translation).

    Here is how I would like it to behave.
    Start empty, without any translation files (except english default).
    When user chooses a language, get the translations via web service, and store them in a translation file.
    Dynamically load the translation file to retranslate the UI.
    Translation files should be saved locally on the device, so the next time user chooses that language it will load straight from the saved file, without the need to translate everything via web service.

    The problem is that I want to generate translation files and include them in my project dynamically (on the fly). So I can't add all the files in my .pro file (which will be translated via lupdate). I need to include and process them dynamically, only when they are needed.

    Is this possible with QT?

    Update: translations can also be done locally (not via web service), but the problem still remains how to generate and load translation files, without the need to declare them in .pro file

  • lupdate creates a .ts file (which happens to be a regular XML file). You can translate them with linguist. lrelease creates a kind-of compressed binary .qm file from that translation. You only need one .ts and do multiple translations in copies of this file. Your web service provides the .qm file(s), you store them in your cache directory and load them as soon as they are downloaded. See the doc of "QCoreApplication::installTranslator()": for how to apply the newly choosen translation to your widgets.

  • Thanks, I get the point.

    Just one more thing. My web service doesn't provide .qm files.
    I'll send a .ts file to it, it will escape all the xml stuff, and return a translated .ts file (note: file extension is not important, I'll send plain xml to service).

    Is it ok to presume that any device capable of running my app will have lrelease command supported so I can make .qm files locally?

    Or do I have to do it on the server? That way I need to install Qt (on server) just to run lrelease

  • You cannot install the .ts XML files directly. They must be converted to a .qm file for "QTranslator::load()": to load it. You can convert it with lrelease on the server (there is a mode to convert a single .ts to a .qm without using a .pro project file) or you could do the "compilation" on the client side, assuming you have also installed lrelease there.

  • Yes I know i need the .qm file.
    Probably it would be best to generate it on server.
    But the server is totally Qt unrelated, and I don't want to install the whole framework just to generate .qm files.

    Is is possible to run lrelease on it own, without the Qt framework on server?
    Btw. server is java+tomcat

  • You only need the lrelease binary, and probably some libs it depends on (use ldd on a linux box or "otool -L" on a mac).

Log in to reply