如何让翻译在课外工作? [英] How to get translation to work outside the class?

查看:42
本文介绍了如何让翻译在课外工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 API,用于创建工作中的接口.API 允许用户从一组预先构建的小部件和布局中进行选择,以便可以在短时间内为不同的单元创建多个界面.会有两个翻译文件;一个用于 API 小部件(随库一起提供),另一个用于开发人员将为界面中的自定义数据创建.为了让开发人员更轻松,我希望 API 通过将数据名称传递给 API 来处理所有翻译,但我遇到了一个绊脚石;我无法让翻译器识别发送给它的翻译文本,但它会识别本地文字字符串.

I'm writing a API to be used in the creation of Interfaces at work. The API allows the user to pick from a set of pre-built widgets and layouts so that multiple interfaces can be created for different units in a short period of time. There will be two translation files; one for the API widgets (which come with the library) and another one that the developer will create for custom data in the interface. To make it easier on the developer I wanted the API to handle all translation by passing the name of the data to the API, but I've come to a stumbling block; I can't get the translator to recognize translated text sent to it, but it will recognize local literal strings.

这是我所谈论内容的一个简短示例.

Here's a short example of what I'm talking about.

class Object
{
    public:
        Object(QString name) { m_Name = name; };
        QString name() { return m_Name; };
    private:
        QString m_Name;
};

class MyWidget : public QPushButton, public Object
{
    Q_OBJECT

    public:
        MyWidget(QString name);
        ~MyWidget();
        void retranslate();

    protected slots:
        void buttonPressed();
        void changeEvent(QEvent* event);

    private:
        enum Language { ENGLISH, JAPANESE };
        Language m_Language;
        QTranslator* m_pTranslator;
};

MyWidget::MyWidget(QString name)
:Object(name) // this does not work, but :Object(tr("TEST")) does
{
    m_pTranslator = new QTranslator();
    m_Language = ENGLISH;
    connect(this, SIGNAL(pressed()), this, SLOT(buttonPressed()));
    retranslate();
}

MyWidget::~MyWidget()
{
    delete m_pTranslator();
}

void MyWidget::buttonPressed()
{
    std::string qm;

    m_Language == ENGLISH ? m_Language = JAPANESE : m_Language = ENGLISH;
    m_Language == ENGLISH ? qm = "lang_en" : qm = "lang_jp";

    qApp->removeTranslator(m_pTranslator);

    if(!m_pTranslator->load(qm.c_str()))
        std::cout << "Error loading translation file\n";

    qApp->installTranslator(m_pTranslator);
}

void MyWidget::retranslate()
{
    setText(tr(name().toStdString().c_str()));
}

void MyWidget::changeEvent(QEvent* event)
{
    if(event->type() == QEvent::LanguageChange)
        retranslate();
    else
        QWidget::changeEvent(event);
}


class MainWindow : public QMainWindow
{
    Q_OBJECT

    public:
        MainWindow();
        ~MainWindow();

    private:
        MyWidget* m_pButton;
};

MainWindow::MainWindow()
{
    m_pButton = new MyWidget(tr("TEST")); // this is what I want to do, but this will not translate
    setCentralWidget(m_pButton);
}

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

// main.cpp
int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    MainWindow window;
    window.show();
    return app.exec();
 }

我是手工输入的,所以可能会有几个错别字,但这个概念仍然成立 - 你必须在你设置文字字符串的同一个类中调用 setText .如果您将文字传递给类,就像我在这里所做的那样,它将被忽略.如果我在课堂上制作文字,它就可以正常工作.这是一个问题,因为我希望开发人员将文字传递给类,然后让它进行翻译.开发人员仍然需要自己进行翻译,但我不希望他们担心处理翻译.

I typed this out by hand so there might be a couple of typos, but the concept still holds true - you have to call setText in the same class your set your literal string. If you pass a literal to the class, like I'm doing here, it will be ignored. If I make the literal in the class it works fine. This is a problem because I want the developer to pass a literal to the class and then let it do the translation. The developer will still need to make their own translations, but I don't want them to worry about handling the translations.

我做错了什么还是这是 Qt 的限制?

Am I doing something wrong or is this a Qt limitation?

推荐答案

我怀疑这是因为:

m_pButton = new MyWidget(tr("TEST"));

MainWindow 的上下文中定义一个字符串,您尝试在MyWidget 的上下文中翻译TEST.您可以通过使用 QObject 上的静态 tr() 方法来避免这种情况.这会将 TEST 定义为全局 QObject 上下文中的翻译.这可以通过调整按钮小部件创建代码来完成,如下所示:

defines a string in the context of MainWindow, and you try to translate TEST in the context of MyWidget. You can circumvent this by using the static tr() method on QObject. This will define TEST as a translation in the global QObject context. This can be done by adapting the button widget creation code as follows:

m_pButton = new MyWidget(QObject::tr("TEST"));

并在 MyWidget::retranslate() 中:

setText(QObject::tr(name().toStdString().c_str()));

请注意,您需要重新生成翻译文件才能使其正常工作!

Note that you need to regenerate your translation files for this to work!

这篇关于如何让翻译在课外工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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