翻译在Qt中不起作用

时间:2015-02-13 21:33:17

标签: c++ qt translation

我一整天都在谷歌搜索解决方案并更改我的代码,但没有运气。

基本上,我已将翻译添加到我的应用中。除了这里工作正常:

QString MainWindow::getMessage(Messages msg) {
    static const char* const messages[] = {
        QT_TR_NOOP("Setting power on"),
        QT_TR_NOOP("Reading ID..."),
        QT_TR_NOOP("Programming..."),
        QT_TR_NOOP("Setting write-protect"),
        QT_TR_NOOP("Finished ok"),
        QT_TR_NOOP("PROGRAMMED OK"),
        QT_TR_NOOP("ERROR!"),
        QT_TR_NOOP("OK"),
        QT_TR_NOOP("The programmer is not connected.\nPlease check the connection."),
        QT_TR_NOOP("Disconnect the board, it is in short!!"),
        QT_TR_NOOP("ERROR: Supply voltage too low (1 Volt is required, Measured: 0.0 Volt)."),
        QT_TR_NOOP("Board is already programmed and write protected."),
        QT_TR_NOOP("Check device connection or there is short."),
        QT_TR_NOOP("Unknown error.")    
    };

    return tr(messages[msg]);
}

但是,我没有得到翻译。用于翻译的文件似乎没问题,UI翻译应用得很好。 我也试过这个:

static const char* const messages[] = {
    QT_TRANSLATE_NOOP("test", "Setting power on"),
    QT_TRANSLATE_NOOP("test", "Reading ID..."),
    QT_TRANSLATE_NOOP("test", "Programming..."),
    QT_TRANSLATE_NOOP("test", "Setting write-protect"),
    QT_TRANSLATE_NOOP("test", "Finished ok"),
    QT_TRANSLATE_NOOP("test", "PROGRAMMED OK"),
    QT_TRANSLATE_NOOP("test", "ERROR!"),
    QT_TRANSLATE_NOOP("test", "OK"),
    QT_TRANSLATE_NOOP("test", "The programmer is not connected.\nPlease check the connection."),
    QT_TRANSLATE_NOOP("test", "Disconnect the board, it is in short!!"),
    QT_TRANSLATE_NOOP("test", "ERROR: Supply voltage too low (1 Volt is required, Measured: 0.0 Volt)."),
    QT_TRANSLATE_NOOP("test", "Board is already programmed and write protected."),
    QT_TRANSLATE_NOOP("test", "Check device connection or there is short."),
    QT_TRANSLATE_NOOP("test", "Unknown error.")
};

然后,我有一个获取消息的方法:

QString MainWindow::getMessage(Messages msg) {
    return qApp->translate("test", messages[msg]); 
}

但它也不起作用。

任何提示或建议?

3 个答案:

答案 0 :(得分:2)

我在这里找到了真正的问题。 通常,翻译器安装在main.cpp中,因此翻译器对象保留在内存中。 但是,在我的情况下,我在用户在设置对话框中更改语言后使用局部变量切换翻译器,但void QCoreApplication::installTranslator ( QTranslator * translationFile ) [static]需要指针。函数退出后立即删除该局部变量。 所以,我在我的课上声明了一个QTranslator对象,所以它保留在内存中。

答案 1 :(得分:1)

在这种情况下可能不适用,但人们常常忘记将(DESCRIPTION_LIST= (DESCRIPTION= (CONNECT_TIMEOUT=10)(RETRY_COUNT=3)(RETRY_DELAY=3) (ADDRESS_LIST= (ADDRESS=(PROTOCOL=tcp)(HOST=myhost1)(PORT=1521)) (ADDRESS=(PROTOCOL=tcp)(HOST=myhost2)(PORT=1521))) (CONNECT_DATA=(SERVICE_NAME=example1.com))) (DESCRIPTION= (CONNECT_TIMEOUT=60)(RETRY_COUNT=1)(RETRY_DELAY=5) (ADDRESS_LIST= (ADDRESS=(PROTOCOL=tcp)(HOST=myhost3)(PORT=1521)) (ADDRESS=(PROTOCOL=tcp)(HOST=myhost4)(PORT=1521))) (CONNECT_DATA=(SERVICE_NAME=example2.com)))) 宏放在类声明中。导致(其中)Q_OBJECT无效。

答案 2 :(得分:0)

我正在准备一个更简单的来源上传到这里,但现在它工作正常!我昨天重新启动了我的电脑,你知道,有时重启会修复一切(?)。 无论如何,这是请求的一些来源。顺便说一句,我正在进行翻译,并允许用户选择语言(而不是通过检测语言环境):

这是main.cpp(我没有添加任何内容):

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

这是mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
#include <QTranslator>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);

    QDir d(":/translations/");
    QStringList files = d.entryList(QStringList("*.qm"));

    qDebug("There are %d files for translation", files.count());

    // Now let's fill the combobox
    this->ui->comboBox->clear();
    for (int i = 0; i < files.count(); ++i) {
        QTranslator translator;
        translator.load(files[i], ":/translations/");
        QString language = translator.translate("MainWindow",
                "English");
        this->ui->comboBox->addItem(language);
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_comboBox_currentIndexChanged(int index)
{
    // Now, based on the new itemindex, let's change the translator
    QString selectedLang = this->ui->comboBox->itemText(index);

    QString language;
    QDir d(":/translations/");
    QStringList files = d.entryList(QStringList("*.qm"));
    for (int i = 0; i < files.count(); ++i) {

        QTranslator translator;
        translator.load(files[i], ":/translations/");
        language = translator.translate("MainWindow",
                "English");

        if (language == selectedLang) {
            if (!qApp->installTranslator(&translator)) {
                qDebug("ERROR INSTALLING TRANSLATOR !!!");
            }
            this->uiTranslate();
            break;
        }
    }
}

/// This function translates all the UI texts:
void MainWindow::uiTranslate(void) {

    this->setWindowTitle(tr("MainWindow"));

    // Just for testing, also show all the messages
    for (int i = 0; i < MSG_LAST; ++i) {
        this->ui->textBrowser->append(this->getMessage((Messages)i));
    }
}

QString MainWindow::getMessage(Messages idx) {
    static const char* const messagesText[MSG_LAST] = {
        QT_TR_NOOP("Hello!"),
        QT_TR_NOOP("Bye bye"),
        QT_TR_NOOP("Nice to meet you")
    };

    return (tr(messagesText[idx]));
}

在这个测试应用程序中,UI只有一个组合框和一个文本浏览器。 组合框中填充了资源文件中包含的语言。 当我更改组合框时,主窗口标题会更改,并且消息将以正确的语言打印。

非常感谢! 最好的问候,