Drift in the forum, often encounter someone encounter TR related problems. There are two types of people using tr:
(1) Because found Chinese old problem, and then search, found a lot of people with TR, so he also began to use TR
(2) Another kind of person, really is out of internationalization needs, will need to display in the interface of the documents are used TR wrapped up, there are two kinds:
(2a) in English with TR (the most recommended usage, source English, and then provide English to other languages translation package)
(2b) Use TR to cover Chinese (source code in Chinese, then provide Chinese to other languages translation package)
Note that if you are wrapping the Chinese characters with TR, but not (2b), then this is a signal:
You're misusing tr.
What you need is a qstring, not a tr.
If you do belong to (2b), please be prepared, you may also encounter a lot of difficulties, please consider the QT internationalization (source contains Chinese time) of the bit analysis
What does TR do? Here's what the difference is.
QString Text1 = qobject::tr ("Hello"); QString Text2 = QString ("Hello");
TR is used to achieve internationalization, if you provide a Chinese translation package for this program (where Hello is translated into Chinese "hello"), then Text1 content will be Chinese "hello", if you provide the program and use Japanese translation package, then Text1 content will be in Japanese.
TR is a multi-level function call to achieve the translation operation, there is a price, so it should not be used when the best not to use.
Objects of concern
This article focuses on the case where a TR or translate contains a Chinese string:
QOBJECT::TR ()
Qcoreapplication::translate ()
Qtextcodec::setcodecfortr
This question is much to be said. Because of the coding problems involved and qstring is exactly the same as the Chinese problem, but one is used setcodecforcstrings a setcodecfortr.
Brief review of Qstring's Chinese question
QString Unicode, no problem with Chinese support
"I am Chinese" This is the narrow string of the traditional const char *
When assigning a narrow string to qstring, we need to tell it what encoding (GBK, utf-8?) is used for our narrow strings.
Exactly what kind of encoding depends mainly on the encoding of our source code (generally GBK on Windows, other platforms generally utf-8)
Example:
QString S1 = "I am Chinese"; QString S2 ("I am Chinese"); QString S3; S3 = "I am Chinese"
S1, S2 with qstring constructor qstring (const char * str)
If you do not specify the encoding, S1,S2,S3 will be all (what most of the domestic people call) garbled. Because qstring will interpret these const char * According to Latin1, not the GBK or UTF8 that the user expects.
Qtextcodec::setcodecforcstrings (Qtextcodec::codecforname ("GB2312")); Qtextcodec::setcodecforcstrings (Qtextcodec::codecforname ("UTF-8"))
One of these two statements solves the problem, and as for how to choose, no longer repeats here.
Qobject::tr
To tell the truth, using Chinese in TR is not a good idea. But since there is always someone to use (whether (1) or (2b)), and there are always some problems, so simply tidy it up.
Compared to qcoreapplication::translate, we should use a lot of TR, although many people do not know what TR is to do ^_^
TR ("I am Chinese");
This is called the following function (at least we can think of it as).
QString qobject::tr (const char * sourcetext, const char * disambiguation = 0, int n =-1)
Exactly like qstring ("I am Chinese"), you have to tell tr what encoding this narrow string is. You don't tell it, it uses latin1. So the so-called garbled problem came out.
How do you tell tr what encoding you have written for these characters on disk? This is
QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("GB2312")); QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("UTF-8"));
To do. The principles of these two choices are not repeated here, as they are exactly the same as before.
If your code uses the UTF8, you can use TrUtf8 without setting the SETCODECFORTR.
If you're only interested in garbled characters, that's all you can do (no longer focus on coding). If you want to learn more about TR, you may wish to: Go on..
Qcoreapplication::translate
We know that TR is used to implement the internationalization of the program (or multi-lingual translation), to see the QT-related information, we know that the implementation of this function is also the following function:
QString qcoreapplication::translate (const char * context, const char * sourcetext, const char * disambiguation, Encoding encoding, int n)
In fact, this is the real function of the translation operation, the previous we mentioned the TR finally by invoking the function to implement the translation function (we will see how TR is called translate).
There are more detailed explanations for TR and this function in manual. Let's take a look at some of its parameters here:
Context, typically the name of the class in which the string needs to be translated
SourceText the string to be translated. (The code we're looking at is actually coding it.)
Disambiguation to eliminate ambiguity. (for example, there are two "close" in our class, one meaning closed and the other meaning intimate.) Obviously need to let the translator know this difference)
encoding specifies the encoding. It has a value of two
CODECFORTR uses the encoding of the SETCODECFORTR () setting to interpret the SourceText
UnicodeUTF8 uses UTF8 encoding to explain sourcetext
Actually, these two correspond to TR and TrUtf8 respectively.
n Processing a single complex number (for Chinese, this problem does not exist)
TR and translate
The descriptions of these two functions, one in Qobject manual and the other in Qcoreapplication manual.
Introduce the relationship between TR and translate. As mentioned earlier, TR calls the translate. It's hard to convince people that there is no evidence if it is just that. All right, go ahead.
where TR is defined
You might say, "It's not nonsense, manual, it's a static member function of Qobject." And there are source code for the certificate:
From src/corelib/kernel/qobject.h #ifdef qdoc static QString tr (const char *sourcetext,const char *comment = 0, int n = 1 ); Static QString TrUtf8 (const char *sourcetext, Constchar *comment = 0, int n =-1); #endif
Hey, almost cheated, found no: they were pre-processed statements wrapped.
What does that mean? It shows that this code is only used to generate QT's beautiful document (QDOC3 extracts information from the code, generating a series of HTML-formatted manual).
Ah, that is to say, this is false. So what is the real definition? In a place where everyone is familiar, guess.
This is
Q_object
The definition of the macro is in Src/corelib/kernel/qobjectdefs.h
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.