Qt internationalization and use of Qt Linguist

Source: Internet
Author: User

A few days ago, I accidentally saw a code from Guang Ge, which contains the following sentence:

 
 
  1. Action-> setText (tr ("START "));

In addition, the main function has the following code:

 

 
 
  1. int main(int argc, char *argv[]) 
  2.  
  3.  
  4.     QApplication a(argc, argv); 
  5.  
  6.     QTextCodec *codec = QTextCodec::codecForName("GB2312"); 
  7.  
  8.     QTextCodec::setCodecForLocale(codec); 
  9.  
  10.     QTextCodec::setCodecForCStrings(codec); 
  11.  
  12.     QTextCodec::setCodecForTr(codec); 
  13.  
  14.     MainWindow w; 
  15.  
  16.     w.show(); 
  17.  
  18.     return a.exec(); 
  19.  

Devbean and dbzhang800 have both criticized this writing for three reasons:

1. If you use GB2312 format to save this code, others use UTF-8 to open it, then will undoubtedly see garbled.

At that time I had this situation, Brother Guang sent the code to me, I found that I saw garbled code in the code, Because ubuntu 10.10 default encoding format is UTF-8, no matter whether I use Qt creator or gedit to open the source code, garbled characters are displayed.

2. If you publish a program to a country or region with non-simplified Chinese characters, the corresponding machine may not have any Chinese encoding format installed, in this case, the other party may fail to correctly display the source code or even run the program.

3. Qt provides a powerful multi-language tool Qt Linguist.

 

The recommended practices for devbean and dbzhang800 are as follows:

1. All the places in the code that need to use Chinese are temporarily replaced by an English section and marked with the tr () function.

2. Use Qt Linguist to translate all strings marked by the tr () function and publish a translation package.

3. Load the translation package in the program.

 

For details, see the blog of devbean:

Qt learning path (33): Globalization (I): http://devbean.blog.51cto.com/448512/244689

The path to Qt Learning (34): Globalization (II): http://devbean.blog.51cto.com/448512/245063

 

The full text is complete.

 

 

References:

The Qt international source with Chinese) Bit by bit analysis: http://blog.csdn.net/dbzhang800/archive/2011/04/19/6334852.aspx

First of all, I personally prefer to use Chinese in the source code, including expanding the Chinese with tr ).

The relationship between translate, tr and Chinese in Qt: http://hi.baidu.com/cyclone/blog/item/aa56e5dd1a79f7e176c638be.html

This article is from the "elephant_liu" blog, please be sure to keep this source http://elephantliu.blog.51cto.com/1107116/596958

Related Article

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.