PYQT Multi-lingual

Source: Internet
Author: User
Tags string find

QT is designed to take into account how the multi-lingual language should be handled, the principle and the use of the method is very simple. As follows:

The first step: When writing code, for the need to translate the words, with TR () wrapped up, such as:

Qpushbutton Hello (qpushbutton::tr ("Hello world!"));

In this way, QT knows that these words need to be translated. Then QT needs to take these words out and put them in a file, so that we can do the next translation work.

Describe the name of this file in your Pro file:

Translations    = Clabel_zh_cn.ts

This allows you to use the Lupdate tool to take out the words that need to be translated:

Lupdate-qt4 Clabel.pro

Then we can begin the translation work. QT offers a matching translation tool: linguist:

Linguist-qt4 clabel_zh_cn.ts

In fact, TS file is the format of XML, you want to edit the text directly is also possible.

When we finish the translation work, we need to compile the TS file to make it easy for the program to use:

Lrelease-qt4 clabel_zh_cn.ts

This generates CLABEL_ZH_CN.QM. Finally, add the code for the selected language in the code:

Qtranslator Trans;
Trans.load ("CLABEL_ZH_CN");
App.installtranslator (&trans);

Qpushbutton Hello (qpushbutton::tr ("Hello world!"));
Hello.show ();

App.exec ();

QT Multi-language more detailed introduction in: http://doc.qt.nokia.com/4.6/i18n-source-translation.html pyqt below How to implement multi-lingual

PYQT inside the implementation of the process and QT inside similar, just because the first step to scan is not C + + code, so need to use a PYQT tool: Pylupdate4, usage and lupdate.

Write the program first

trans = Qtranslator ()
trans.load (' plabel_zh_cn ')
app.installtranslator (trans)

button = Qpushbutton (tr ( "Hello world!"))
Button.show ()

Wait, what is the TR inside?

def tr (msg):
    return Qcoreapplication.translate ("@default", msg)

Qt Translation is based on the name of the class to walk, call what kind of TR, take this class inside the translation set.

QT inside is can use QOBJECT::TR to translate, but pyqt inside can't, pylupdate4 just do string find, see what string is behind TR, and then according to TR caller to classify this word, if there is no caller, it is classified into "@default Inside. So I had to use the above method to do a circumvention. A bit disgusting, see if there will be a better way to deal with it later.

For translations within a class, you don't need to be so troublesome:

Class Input (Qwidget):
    def __init__ (self):
        super (input, self). __init__ ()
        label = Qlabel (self.tr ("Input: "))

The TR in this can be translated correctly.

Then run Pylupdate, because we are a Python program, no pro, you can only manually specify the file

Pylupdate4 Main.py-ts Plabel_zh_cn.ts

Well, the following steps are the same as the C + + method

Linguist-qt4 clabel_zh_cn.ts
Lrelease-qt4 plabel_zh_cn.ts

Then execute the code, and the program is translated in the way we want it to be, isn't it simple?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.