Qt internationalization (1) -- work

Source: Internet
Author: User

Http://www.top-e.org/jiaoshi/html? 160. html

 

The internationalization method in QT is similar to GNU gettext. It provides the tr () function and the gettext () function, and the translated resource file uses. QM, and its internationalization mechanism is closely related to its object system.

In the process of supporting internationalization, programmers generally add internationalization support to the code they write. In this case, qstring, qtranslator, and other classes and TR () are used in QT () functions can be easily completed. Programmers need to do the following:

1. Use qstringObject To represent all user-visible text.Since qstring is implemented using Unicode encoding, it can be used to represent all texts to be presented to users. Of course, only the visible text of the programmer does not need to be converted into a qstring object. You can use the qcstring or the original char * provided by QT *.

2. Use tr ()Function to get all the text to be translated.In the QT translation mechanism, the qobject: TR () function can help us get the translated text. For Classes inherited from qobject, The qobject: TR () function is ultimately implemented by qmetaobject: TR (). We can refer 11.2.1 The definition of the q_object macro mentioned in and the implementation of qmetaobject: TR () in qmetaobject. cpp. In some cases, if the qobject: TR () function cannot be used, we can also directly call qcoreapplication: translate () to obtain the translated string.

3. Use qstring: Arg ()To organize dynamic text.Sometimes a piece of text needs to be combined by static text and dynamic variables, for example, printf ("the value of I is: % d", I ). Due to language habits, the translation of dynamic text may cause some problems if you simply use this method to connect strings. For example, the following string is used to indicate the task completion:

Qstring M = tr (" Mission Status: ") + x + Tr (" of ") + Y + Tr (" are completed ");

X and Y are dynamic variables, and the three strings are separated by X and Y. They cannot be well translated. "X of Y" is the expression of scores in English, for example, if 4 of 5 is a score of 4/5, the Split points and denominator locations in different languages may be reversed, in this case, numbers 4 and 5 cannot be correctly placed during translation. It can be seen that the isolated translation strings cannot be used. The improved method is to use qstring :: arg () method:

Qstring M = tr (" Mission Status: % 1 Of % 2 Are completed "). Arg (x). Arg (y );

In this way, the interpreter can translate the entire string and put the parameters % 1 and % 2 in the correct position.

4. Use qtranslator: load () and qcoreapplication: installtranslator () to read the corresponding translated resource files.The translator will provide resource files containing translated strings *. QM, the programmer also needs to define the qtranslator object and use the load () function to read the corresponding. then, use the qcoreapplication: installtranslator () function to install the qtranslator object.

For translators, we mainly use the tools lupdate, linguist, and lrelease provided by QT (they can be found in the bin folder of the QT installation directory) to assist in translation and generate the final requirements. the QM file contains the following content:

1. Use lupdateThe tool scans and extracts the strings to be translated from the source code to generate. TsFile.Similar to the qmake used in compilation, we also need to specify one when running lupdate. pro file, this. the Pro file can be created separately or used during compilation. in the pro file, you only need to define the variable translations. For detailed usage, see the following small example.

2.ExploitationLinguistTools to help complete the translation work, that is, open the previous lupdateGenerated. TsFile to translate and save the strings one by one.Because the. Ts file adopts the XML format, we can also use other editors to open the. Ts file and translate it.

3.ExploitationLreleaseTool to process translated. TsFile to generate a more compact. QMFile.This is the resource file that translators ultimately need to provide to programmers. It occupies less space than the. Ts file, but is basically not readable. Only qtranslator can correctly identify it.

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.