A few days ago, I accidentally saw a code from Guang Ge, which contains the following sentence:
- Action-> setText (tr ("START "));
In addition, the main function has the following code:
- int main(int argc, char *argv[])
-
- {
-
- QApplication a(argc, argv);
-
- QTextCodec *codec = QTextCodec::codecForName("GB2312");
-
- QTextCodec::setCodecForLocale(codec);
-
- QTextCodec::setCodecForCStrings(codec);
-
- QTextCodec::setCodecForTr(codec);
-
- MainWindow w;
-
- w.show();
-
- return a.exec();
-
- }
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