Qt 对话框窗口的动态翻译 [英] Qt Dynamic translation of dialog windows

查看:42
本文介绍了Qt 对话框窗口的动态翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 Qt 应用程序并添加了动态翻译(我遵循了 http://www.qtcentre.org/wiki/index.php?title=Dynamic_translation_in_Qt4_applications) 和一个列出不同语言的 QCombobox.它运行良好,但问题是我没有看到如何动态翻译对话框窗口中的文本(例如是"和否"按钮).

I am creating a Qt application and I added dynamic translation (I followed the example at http://www.qtcentre.org/wiki/index.php?title=Dynamic_translation_in_Qt4_applications) with a QCombobox which lists different languages. It works well but the problem is that I don't see how to translate dynamically the text in the dialog windows (for example YES and NO buttons).

在 main.cpp 中,在执行应用程序之前,我有:

In the main.cpp, before executing the app, I have :

QTranslator qtTranslator;
qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
a.installTranslator(&qtTranslator);

用用户系统语言翻译 Windows 对话框,但我想像我的应用程序的其余部分一样动态地完成它.

which translate the dialog Windows in the user system language but I would like to do it dynamically like the rest of my app.

示例代码如下:应用程序.h:

Here are the code of the example : application.h :

#ifndef APPLICATION_H

#include <QApplication>
#include <QHash>
#include <QStringList>
class QDir;
class QTranslator;

typedef QHash<QString, QTranslator*> Translators;

class Application : public QApplication
{
    Q_OBJECT

public:
    explicit Application(int& argc, char* argv[]);
    ~Application();

    static void loadTranslations(const QString& dir);
    static void loadTranslations(const QDir& dir);
    static const QStringList availableLanguages();

public slots:
    static void setLanguage(const QString& locale);

private:
    static QTranslator* current;
    static Translators translators;
    //static QTranslator* qtTranslator;//test to translate dialog windows
};

#endif // APPLICATION_H

应用程序.cpp:

    #include <QDir>
#include <QFileInfo>
#include <QTranslator>
#include <QLibraryInfo>

#include "application.h"


QTranslator* Application::current = 0;
//QTranslator* Application::qtTranslator = 0;//test to translate dialog windows

Translators Application::translators;

Application::Application(int& argc, char* argv[])
    : QApplication(argc, argv)
{

}

Application::~Application()
{
}

void Application::loadTranslations(const QString& dir)
{
    loadTranslations(QDir(dir));

    QString locale = QLocale::system().name().section('_', 0, 0);
    QString language=locale+ "_" + locale;

    if(!QFile::exists(":Localization/Localization/"+language+".qm"))//if system language is not available, load english version
        setLanguage("en_en");
    else
        setLanguage(language);
}

void Application::loadTranslations(const QDir& dir)
{
    // <language>_<country>.qm
    QString filter = "*_*.qm";
    QDir::Filters filters = QDir::Files | QDir::Readable;
    QDir::SortFlags sort = QDir::Name;
    QFileInfoList entries = dir.entryInfoList(QStringList() << filter, filters, sort);
    foreach (QFileInfo file, entries)
    {
        // pick country and language out of the file name
        QStringList parts = file.baseName().split("_");
        QString language = parts.at(parts.count() - 2);
        QString country  = parts.at(parts.count() - 1);
        // construct and load translator
        QTranslator* translator = new QTranslator(instance());
        if (translator->load(file.absoluteFilePath()))
        {
            QString locale = language + "_" + country;
            translators.insert(locale, translator);
        }
    }
}

const QStringList Application::availableLanguages()
{
    // the content won't get copied thanks to implicit sharing and constness
    return QStringList(translators.keys());
}

void Application::setLanguage(const QString& locale)
{
    //test to translate dialog windows
    /*
    QTranslator qtTranslator;
    QString qTLocale=locale.mid(0,2);
    qtTranslator->load("qt_"+ qTLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
    installTranslator(qtTranslator);
    //*/

    // remove previous
    if (current)
    {
        removeTranslator(current);
    }

    // install new
    current = translators.value(locale, 0);
    if (current)
    {
        installTranslator(current);
    }
}

我添加了注释为//test to translate dialog Windows"的行以尝试动态转换对话框 Windows,但它不起作用(编译时没有错误,但应用程序未启动并显示错误消息the程序突然停止",我在 Qt Creator 上).谢谢!

I added the lines commented with "//test to translate dialog Windows" to try the dynamic translation of the dialog Windows but it doesn't work (no error at compilation but the application isn't launched with error message "the program stopped suddenly", I am on Qt Creator). Thanks!

推荐答案

所以我在遇到同样的问题后终于让它工作了.在我的案例中,有两件事是错误的:

So I finally got this to work after having the same problems. There are two things which were wrong in my case:

  1. qt 翻译文件的名称:

  1. Name of the qt translation file:

QTranslator qtTranslator;
qtTranslator.load("qt_de");  // worked in older qt versions
qtTranslator.load("qtbase_de");  // works for qt5.2
a.installTranslator(&qtTranslator);

  • QMessageBox 有正确的父级.仔细想想,这很明显,但很容易错过.

  • Have the correct parent for the QMessageBox. This is obvious after you think about it but pretty easy to miss.

    QMessageBox::information(someChildOfMainWindow, ...);
    

  • 对于后者,如果你碰巧在一个QObject但不是QWidget的类中,你也可以使用下面的代码来访问你的MainWindow 从任何地方:

    For the latter, if you happen to be in a class which is a QObject but not a QWidget you can also use the following code to access your MainWindow from anywhere:

    QMainWindow* mw = 0;
    foreach(QWidget* widget, QApplication::topLevelWidgets()) {
        if(widget->objectName() == "<your-main-window-class-name-here>") {
            mw = qobject_cast<QMainWindow>(widget);
        }
    }
    

    这篇关于Qt 对话框窗口的动态翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆