简体   繁体   中英

Qt - Use builtin translations

I'm using Qt and want to translate the texts "natively" shown by Qt widgets. By "texts natively shown" I'm for instance referring to the ones shown in context menus for text edits (copy, paste, ...).

Here is what I've already done:

#include <QApplication>
#include <QDebug>
#include <QTranslator>

#include <QFile>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTranslator translator;
    if(translator.load("qt_fr.qm", QApplication::applicationDirPath())) {
        qDebug() << a.installTranslator(&translator);
    }

    qDebug() << QFile::exists(QApplication::applicationDirPath() + "/qt_fr.qm"); // just to debug file existence

    // MainWindow w;      // not related to my question
    // w.showMaximized(); // neither is this

    return a.exec();
}

The qt_fr.qm file is located at path_to_qt/Qt5.6.2/5.6/mingw49_32/translations for Qt5.6.2 and MinGW users. I copy the said file to the running software directory but the translator always fails to load it. But when I use my own qm file (built from a .ts file using the Qt lupdate and lrelease tools), the qm file is properly loaded and installed.

Is there something I'm missing or doing wrong?

I think the problem may be that you haven't copied the complete message catalog. The following works for me on a Debian system, using the QM files in their standard locations:

#include <QApplication>
#include <QDebug>
#include <QLocale>
#include <QTranslator>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTranslator translator;
    const QString dir = "/usr/share/qt5/translations";

    if (translator.load("qt_fr", dir)) {
        qDebug() << "first load succeeded:"
                 << "'Open' =>" << translator.translate("QShortcut", "Open");
    }

    if (translator.load(QLocale::French, "qt", "_", dir)) {
        qDebug() << "second load succeeded:"
                 << "'Open' =>" << translator.translate("QShortcut", "Open");
    }
}

Output is

first load succeeded: 'Open' => "Ouvrir"
second load succeeded: 'Open' => "Ouvrir"

(I removed the .qm from the filename, as Qt will try that first, and I've also shown how to compose the filename from a specific locale object).

If we inspect the qt_fr.qm file using lconvert -of ts /usr/share/qt5/translations/qt_fr.qm , we can see it's just a very small file that incorporates other files by reference:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="fr_FR">
<dependencies>
<dependency catalog="qtbase_fr"/>
<dependency catalog="qtscript_fr"/>
<dependency catalog="qtquick1_fr"/>
<dependency catalog="qtmultimedia_fr"/>
<dependency catalog="qtxmlpatterns_fr"/>
</dependencies>
</TS>

I think that the most likely cause of your symptoms is that one or more of the dependency files could not be loaded. You should ensure that all of those files area available in the same location that you copied qt_fr.qm to - or, if you only need the "base" translations, just copy qtbase_fr.qm , and change your translator.load() call appropriately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM