Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Loading qm file from ressources fails



  • I try to load a qm file from the ressource system but it fails.

    The code:

    LanguageSelector::LanguageSelector(QObject *parent)
        : QObject{parent}, mTranslator{new QTranslator{this}}
    {
        loadEnglish();
    }
    
    void LanguageSelector::loadEnglish()
    {
        QFile file(":/translations/quiz.en.qm");
        if (!file.open(stdout, QIODevice::ReadOnly)) {
            qDebug() << "Can't find it!";
        }
    
        if (!mTranslator->load(QLocale(QLocale::English), QLatin1String("quiz.en"),
                               QLatin1String(":/translations"))) {
            qDebug() << "load english failed";
        }
    }
    

    Strangely I don't get an error on the QFile check as expected but loading the file with QTranslator fails. What could be the Issue?

    The file with the code is in the folder:

    /src

    The ressource file for the languages is directlyy in root:

    translations.qrc

    The translations file:

    <RCC>
        <qresource prefix="/">
            <file>translations/quiz.de.qm</file>
            <file>translations/quiz.en.qm</file>
            <file>translations/quiz.es.qm</file>
        </qresource>
    </RCC>
    


  • @sandro4912 said in Loading qm file from ressources fails:

    mTranslator->load(QLocale(QLocale::English), QLatin1String("quiz.en"), QLatin1String(":/translations"))) {
    

    you may want to check the documentation of QTranslator::load() method



  • @Pablo-J-Rogina

    I checked it but I could not find an issue



  • I missed stating the separator in the method. Like this it works:

    void LanguageSelector::loadLanguage(const QLocale::Language &newLanguage)
    {
        if (!mTranslator->load(QLocale(newLanguage), QLatin1String("quiz"),
                               QLatin1String("."),
                               QLatin1String(":/translations"))) {
            auto metaEnum = QMetaEnum::fromType<QLocale::Language>();
            qDebug() << tr("load language %1 failed")
                            .arg(metaEnum.valueToKey(newLanguage));
        }
        else {
            qDebug() << mTranslator->filePath();
        }
    }
    

Log in to reply