Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTranslator::load fails in static library

QTranslator::load fails in static library

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 373 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DL5EU
    wrote on last edited by
    #1

    Dear all,

    I have got a problem loading a translation file. Certainly a newbie problem again :-)

    Qt Creator has added the following code to the .pro file and main.cpp of my application:

    HP34401A_GUI.pro file:

    TRANSLATIONS += \
        HP34401A_GUI_de_DE.ts
    CONFIG += lrelease
    CONFIG += embed_translations
    

    main.cpp:

        QTranslator translator;
        const QStringList uiLanguages = QLocale::system().uiLanguages();
        for (const QString &locale : uiLanguages) {
            const QString baseName = "HP34401A_GUI_" + QLocale(locale).name();
            if (translator.load(":/i18n/" + baseName)) {
                app.installTranslator(&translator);
                break;
            }
        }
    

    This works and loads the translated strings.

    I also have a statically linked library called "Shared" in my project. Now I have tried to do the same in the .pro file and a function of this library. The function is called from main() and returns a translator that is not deleted when the function exits. With a hex editor I can see that the original and translated strings are present in libShared.a but translator.load() with baseName = "Shared_" + QLocale(locale).name() fails. When I check the directories with

        QDirIterator it(":", QDirIterator::Subdirectories);
        while (it.hasNext()) {
            qDebug() << it.next();
        }
    

    I see that HP34401A_GUI_de_DE.qm is present but not Shared_de_DE.qm although this file exists in the same directory as libShared.a:

    ":/i18n"
    ":/i18n/HP34401A_GUI_de_DE.qm"
    

    So, what am I doing wrong? Why is HP34401A_GUI_de_DE.qm found automagically but not Shared_de_DE.qm?

    Thank you very much for your help!

    Kind regards,

    Ralf

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Your translation comes from a library. See here for how to handle resources from different libraries.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • D Offline
        D Offline
        DL5EU
        wrote on last edited by
        #3

        Hello SGaist,

        I had read this part of the documentation about the Qt Resource System but I did not understand why this would be necessary in my case.

        When I read the part about the application resources, nothing of what is mentioned in the documentation is done in my application (created by Qt Creator), at least not explicitly. There is no .qrc file in my source tree and no RESOURCES variable in the .pro file but nevertheless it works.

        Now I have noticed that files called "qmake_qmake_qm_files.qrc" are created automatically in the build tree (probably because of the "embed_translations" directive in the .pro files), one for the application and one for the library. And I have found this article in the wiki that explains why automatic resource loading only works for resources that are compiled into the main program but not in a library.

        In short, after a bit of additional thinking and doing as explained in the documentation it works now :-)

        Just in case anybody is interested in the code, here it is:

        // Load resources (main program)
        QTranslator translator;
        const QStringList uiLanguages = QLocale::system().uiLanguages();
        for (const QString &locale : uiLanguages) {
            const QString baseName = "HP34401A_GUI_" + QLocale(locale).name();
            if (translator.load(":/i18n/" + baseName)) {
                app.installTranslator(&translator);
                break;
            }
        }
        
        // Load resources (statically linked library "Shared")
        Q_INIT_RESOURCE(shared);
        QTranslator sharedLibTranslator;
        for (const QString &locale : uiLanguages) {
            const QString baseName = "Shared_" + QLocale(locale).name();
            if (sharedLibTranslator.load(":/i18n/" + baseName)) {
                app.installTranslator(&sharedLibTranslator);
                break;
            }
        }
        

        In the project file of the library I have added RESOURCES += shared.qrc (the resource file that links to "Shared_de_DE.qm" with the prefix "i18n").

        Thank you and kind regards,

        Ralf

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved