QT Learning Pathway (34): Internationalization (Next)

Source: Internet
Author: User

Last time I said the internationalization process, now look at the specific internationalization of the relevant code.

In the code, we use TR () to mark the strings that need to be translated. The Lupdate tool is to extract the relevant strings from the TR () function. The TR () function is a static function of the Qobject class, and its signature is as follows:

Static QString tr (const char *sourcetext, const char *comment = 0, int n =-1);

Although we have passed only one parameter, the TR () function actually accepts 3 parameters. The first parameter is the text that we need to translate, and if the QM file has a corresponding string, the corresponding string is substituted, otherwise the string specified by the SourceText parameter is displayed. The second argument is a comment that explains the meaning of the previous sourcetext, such as the Word table can be translated as a table or a form, and you need to provide this annotation. Perhaps you will ask, the use of translation tools is not a source code? The problem is, it's possible that people don't use this translation tool, with other tools, there is no guarantee that the source code will be previewed, and your program does not necessarily have to publish the source code, the translator often only get our export TS files, if you add comments, you can facilitate translators to translate. The last parameter, N, is used to specify whether the string is a complex number. We know that in many languages, such as English, the single plural form of many nouns is not the same, and in order to solve this problem, QT provides an argument n in the TR () function. Please see the following code:

int n = messages.count();
showMessage(tr("%n message(s) saved", "", n));

For different values of N, QT translates into different text, such as:

N Translation results
0 0 Message saved
1 1 Message saved
2 2 Messages saved
5 5 Messages saved

The TR () function is a qobject function, and you cannot use the TR () function directly if your class is not inherited from Qobject. For example, in the main () function, we want to add a phrase that sets the title of MainWindow:

W.setwindowtitle (tr ("MyApp"));

This can not be compiled directly, because the main () function is a global function, so this TR () is not found. Workaround one is to explicitly call the Qobject function:

W.setwindowtitle (Qobject::tr ("MyApp"));

Alternatively, you can use the Qcoreapplication translate () function. You must remember that the first sentence of our main () function is always qapplication app; in fact, Qapplication is qcoreapplication subclass. So, we can also write this:

W.setwindowtitle (App.translate ("MyApp"));

Since Qcoreapplication is a single class in the QT program, QT provides a macro Qapp for easy access to Qcoreapplication's single example. So, in other files, we can also call Qapp.translate () directly to replace TR (), but that's not necessary.

If your translated text contains data that needs to be displayed dynamically, such as the one in our last code

Qmessagebox::information (NULL, TR ("path"), tr ("You selected\n%1"). Arg (path));

Of course you can write it.

Qmessagebox::information (NULL, TR ("path"), "you selected\n" + path);

But this way of connecting strings is not able to use the TR () function! So, if you need to format the output like the C language (printf) function and need to translate it, you must use the%1 arg () function in our example!

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.