How to translate the buttons in qmessagebox?

前端 未结 6 843
轮回少年
轮回少年 2020-12-30 01:43

I have a QMessageBox like this:

QMessageBox::question(this, tr(\"Sure want to quit?\"), 
    tr(\"Sure to quit?\"), QMessageBox::Yes | QMessageB         


        
6条回答
  •  没有蜡笔的小新
    2020-12-30 02:30

    update: I found in D:\Qt\Qt5.7.0\5.7\Src\qttranslations\translations\qtbase_**.ts already has the QPlatformTheme's translation srouce file (Unfortunately, there is no qtbase_zh_CN.ts), you can also copy a qtbase_**.ts and modify it immediately. If you are a Chinese like me, thanks to wisaly(github), he has already translated the qtbase to Chinese, and here is my fork on github.


    After reading the Qt source code, I solved this issue. (My Qt's version is Qt 5.7.0, and installed in C:\Qt\Qt5.7.0 with Src)

    Open the C:\Qt\Qt5.7.0\5.7\Src\qtbase\src\gui\gui.pro and insert a line like below to generate a Chinese translation file:

    TRANSLATIONS    +=  gui_zh.ts
    

    Open the gui.pro project with Qt Creator and use lupdate to generate a new cute's translation source file named gui_zh.ts.

    Open the qui_zh.ts using Linguist and translate the QPlatformTheme item. Here only translated “&Yes” as a example:

    After translated, use lrelease to generate a binary translation file (gui_zh.qm).

    Lastly, load the translations files (gui_zh.qm) to your QApplication, the button's text of QMessageBox will be ok.

    My results are:

    QMessageBox::information(this,
        QString("警告"),
        QString("测试文本"),
        QMessageBox::Yes | QMessageBox::No
    );
    

    By the way, you can also use this method to sovle the right contextmenu's transtions issues of some QWidgets like QTextEdit by add translations to C:\Qt\Qt5.7.0\5.7\Src\qtbase\src\widgets\widgets.pro.

提交回复
热议问题