简体   繁体   English

Qt-使用内置翻译

[英]Qt - Use builtin translations

I'm using Qt and want to translate the texts "natively" shown by Qt widgets. 我正在使用Qt,并且想要“本地”翻译Qt小部件显示的文本。 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. 对于Qt5.6.2和MinGW用户, qt_fr.qm文件位于path_to_qt / Qt5.6.2 / 5.6 / mingw49_32 / translations 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. 但是,当我使用自己的qm文件(使用Qt lupdate和lrelease工具从.ts文件构建)时,qm文件已正确加载和安装。

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: 以下在Debian系统上使用标准位置的QM文件为我工作:

#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). (我从文件名中删除了.qm ,因为Qt会首先尝试这样做,并且我还展示了如何从特定区域设置对象组成文件名)。

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: 如果我们使用lconvert -of ts /usr/share/qt5/translations/qt_fr.qm检查qt_fr.qm文件,我们可以看到它只是一个很小的文件,通过引用合并了其他文件:

<?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. 您应该确保所有这些文件区域在将qt_fr.qm复制到-的同一位置可用,或者,如果仅需要“基本”翻译,则只需复制qtbase_fr.qm ,并适当地更改您的translator.load()调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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