Qt internationalization (3) -- some tips

Source: Internet
Author: User

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

 

I often encounter QT translation problems. Some experiences are summarized as follows:

If a string translation problem is found, check whether the string has been extracted to the. Ts file by lupdate. If not, check the. Pro file specified when lupdate is run. If. if the string can be found in the TS file, further check. whether the context settings in the TS file are consistent with the context when the tr () function is called, and after being processed by linguist. whether the string has been translated in the TS file. In addition, the problem may be that qtranslator has read the correct. QM file.

Some easy mistakes:

The common mistake is to get the ID corresponding to the string and use it at will. For example, store these IDs in a TXT file, the result is probably that these strings are not extracted by lupdate at all, because lupdate only pays attention to some keywords such as tr () during scanning. of course, there are no corresponding translation strings in the Qm file. The final result is that these strings cannot be correctly displayed and translated.

Another problem is to put the translation string into an array. Simply placing IDs in an array does not attract the attention of lupdate. The result is the same as above. In this case, we can use the macro qt_tr_noop () or qt_translate_noop () provided by QT to facilitate lupdate recognition. For example:

Static const char * greeting_strings [] = {

Qt_tr_noop ("hello "),

Qt_tr_noop ("goodbye ")

};

 

// Or

Static const char * greeting_strings [] = {

Qt_translate_noop ("friendlyconversation", "hello "),

Qt_translate_noop ("friendlyconversation", "goodbye ")

};

 

Note that the first parameter of the qt_translate_noop macro is const char * context, that is, context. This concept is introduced to improve the situation that the same string may need different translations in different contexts, but it also brings a lot of problems. Let's take a look at the implementation of qmetaobject: TR () in qmetaobject. cpp:

Qstring qmetaobject: TR (const char * s, const char * C) const

{

Return qcoreapplication: translate (D. stringdata, S,

C, qcoreapplication: codecfortr );

}

 

Here, the context value is set to D. stringdata. In the QT Object System, D. stringdata is actually the class name. However, if you place a string in another class, for example, the string txt_1 is added to an array in Class A and wrapped in the qt_tr_noop macro, but we only use tr () in Class B () if the function uses it, the context value obtained after lupdate scanning is A, and the context value is B When Tr () is called in Class B, such inconsistency may cause the translation to fail. If you do not understand the entire process and are lucky enough to use it, it is very difficult to find out this error.

 

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.