Unable to load translations from resource file



  • Hi,

    I'm having trouble loading translationd from a .qrc file. Here is a miniml example to show the problem:

    #include <QApplication>
    #include <QTranslator>
    #include <QtDebug>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QTranslator translator;
        bool ok = translator.load(QLocale(), "Test", ".", ":/Translations");
        qDebug() << "Locale: " << QLocale::system().name() << (ok ? " ok" : " not ok");
    
        return a.exec();
    }
    

    The .pro file is:

    #-------------------------------------------------
    
    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = test
    TEMPLATE = app
    
    
    SOURCES += main.cpp
    
    HEADERS  +=
    
    FORMS    += mainwindow.ui
    
    RESOURCES += \
        translations.qrc
    

    translations.qrc contains a set of .qm files from a top level Traslations folder including Test.en_GB.qm yet when the progam runs it outputs:

    Locale: "en_GB" not ok

    What am I doing wrong?

    Thanks


  • Lifetime Qt Champion

    Hi,

    Silly question but do you really have that Translations prefix in your .qrc file ? Can you share it ?





  • @SGaist The file reads as follows:

    <RCC>
    <qresource prefix="/Translations">
    <file>Translations/Test.qm</file>
    <file>Translations/Test.fr.qm</file>
    <file>Translations/Test.en.qm</file>
    <file>Translations/Test.de.qm</file>
    <file>Translations/Test.en_GB.qm</file>
    </qresource>
    </RCC>

    I've tried it with and without the Translations prefix and it fails in both


  • Qt Champions 2016

    @oberluz
    If I read your qrc correctly, the path to the file(s) is: ":/Translations/Translations/Test.qm"



  • translator.load(QLocale(), "Test", ".", ":/Translations/Translations"); also fails


  • Qt Champions 2016

    translator.load(QLocale(), "Test", QString(), ":/Translations/Translations");
    

    But in any case I'd first check if the file can be opened by that path, i.e.:

    QFile file(":/Translations/Translations/Test.qm");
    if (!file.open())
        qDebug() << "Can't find it!";
    


  • @kshegunov Thank you, that did it!


  • Qt Champions 2016

    @oberluz
    No problem, although the merit should go to @SGaist who actually sensed what the problem is. :)
    Cheers!


Log in to reply
 

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