[solved]How to embed qm translation files into executable

  • Hi all,

    I have a Qt widget that I have made a translation file to. It works when loading using qm file path, but I would like to embed the qm file in the executable, and thus into the resources. So I made a qrc file that includes the qm file and also added

    TRANSLATIONS = myApp_da_DK.ts

    in the pro file. I then hoped that it would be a straight-forward thing to call the resource item like when you use icons or qml widgets. These are the tests:

    res = appTranslator.load("myApp_da_DK.qm");                                          // OK
    res = appTranslator.load("myApp_da_DK.qm", "qrc:/res/translations");                 // FAILS
    res = appTranslator.load("myApp_da_DK.qm", ":/res/translations");                    // FAILS
    if (res)

    (filename of qm file hardcoded to make sure I din't get the name wrong)

    Do I have to do anything extraordinary to make it work?

  • Ok, so a quick update. There is no extra things needed to be done in order to make this work. I'm not sure if I accidentially mixed something up, but it actually works now. I think the reason for the loading not to work was the fact that I put the qm file in another resource file, which was prefixed inside the qrc file with "translations". I wasn't aware that I needed put this into the path of the load function.
    The way I discovered this was that the resources can be inspected through a QDir(":/") - this helped a lot in order to get the right search path!

    So in the end, I put the translation file next to the other resources in my resources.qrc file and indexed it like this:
    bool res = appTranslator.load("myApp_" + QLocale::system().name(), ":/res/translations");

    The qm file is thus located in the /res/translations subdir of the project root.

    Hopefully other people can benefit from this problem I had.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Glad you found out and thanks for sharing !

    Since you have it working now, please update the thread title prepending [solved] so other forum users may know that a solution has been found :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.