Qt basic method for loading translation files

I am using Qt 4.8.3.

 Qt's translation is divided into three steps:

        1. Run lupdate or complete the operation by selecting the following picture (select Qt language home and then complete the update translation), extract all the strings identified by tr () from the application , and generate a file with the suffix * .ts .

        2. Use Qt Linguist to translate this * .ts . Then use it to generate * .qm .

        3. Use the QTranslator class to load * .qm files in the application .

        To use lupdate to generate a file with the suffix * .ts , add this sentence to the application project file * .pro :

TRANSLATIONS = Demo_zh_CN.ts

        The file name can be set by yourself. Then run lupdate -verbose project name.pro on the command line. After the program is executed, a * .ts file will be generated.

Open the * .ts file         with Qt Linguis . As shown:

From the picture, you can see the Context column, which is listed according to the class in your program. Then the Strings column next to it is the sentence to be translated. Pay attention to the Sources and Forms column when translating, and the column of Warnings will do. The source of the sentence to be translated can be seen in the previous column, and the warning message can be seen in the latter column. The warning message is still useful. After each sentence is translated, click the green tick icon on the toolbar to complete the translation and skip to the next sentence. A green checkmark will appear in the exact sentence, and a yellow checkmark will appear if there is a warning message, or a question mark if there is no translation.

        Note: 1. I found some places, I should add "\ n" according to the original format. However, after translation, "\ n" is also displayed in the Chinese sentence instead of carriage return and line feed. 2. For statements like "& Load", we can translate it into "& L" just like other software, so that there will be no warning about missing shortcut keys. 3. The format of the translated sentence must match the format of the original sentence, especially the punctuation. I found that, for example, I used the English period ".", And then I changed it to the Chinese period. "" There is no warning. However, when the original "!" In English is translated into "!", There is still a warning message. Strange ~

        After translation, click File-> Release to generate * .qm file. Use the following statement to load in main.cpp:

        QTranslatorqtTranslator;

        qtTranslator.load ("orbitEditor_zh_CN.qm", ": /");
        a.installTranslator (& qtTranslator);
        Of course, remember to add * .qm to the resource file * .qrc. According to the different paths, modify the load sentence above.

        After this translation, two problems may occur:

        1. Statements in Qt's internal classes such as QMessageBox, QDialog, etc. are not translated into Chinese. For this problem, just load another translation file. Qt is ready for us, in the "installation directory \ translations. I used the file qt_zh_CN.ts , Qt has translated it for us. Just export it as a * .qm file and load it.

        2. After being translated into Chinese, the Chinese text is very small, and you have to work hard to see clearly. . . The simplest method is to set the global text size.

        So, the final load statement in main.cpp is:

     QApplicationa(argc,argv);
    //加载中文翻译
    //加载本软件的翻译
    QTranslator qtTranslator;
    qtTranslator.load("orbitEditor_zh_CN.qm",":/");
    a.installTranslator(&qtTranslator);
    //加载Qt其他类,比如QDialog的翻译
    QTranslator qtGloble;
    qtGloble.load("qt_zh_CN.qm",":/");
    a.installTranslator(&qtGloble);
    //设置全局文字大小
    QFont font  = a.font();
    font.setPointSize(12);
    a.setFont(font);

Part of my code:

    #include <QTranslator>
   
    QTranslator trlQt, trlApp;
    const QString& strLocaleName = QLocale::system().name();
    const QString& strQtTrlPath = QLatin1String(":/translations");
    const QString& strQtTrlFile = QLatin1String("qt_") + strLocaleName;

    const QString& strAppTrlPath = QLatin1String(":/translations");
    const QString& strAppTrlFile = QLatin1String("Demo_") + strLocaleName;

    if (trlQt.load(strQtTrlFile, strQtTrlPath)) {
        a.installTranslator(&trlQt);
    }

    if (trlApp.load(strAppTrlFile, strAppTrlPath)) {
        a.installTranslator(&trlApp);
    }

Borrowed from: https://blog.csdn.net/yangxiao_0203/article/details/7488967

Published 27 original articles · 25 praises · 120,000 views

Guess you like

Origin blog.csdn.net/weixin_38293850/article/details/89680575