I talked about the internationalization process last time. Now let's take a look at the specific Code related to internationalization. In the code, we use tr () to mark the strings to be translated. The lupdate tool extracts the relevant strings from the tr () function. The tr () function is a static function of the QObject class whose signature is as follows: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif"/> static QString tr (const char * sourceText, const char * comment = 0, int n =-1 ); although we only pass one parameter, the tr () function actually accepts three parameters. The first parameter is the text to be translated. If the qm file has a corresponding string, replace it with the corresponding string. Otherwise, the string specified by the sourceText parameter is displayed. The second parameter is a comment used to explain the meaning of sourceText. For example, the word table can be used as both table translation and table translation. In this case, you need to provide this comment. Maybe you will ask, isn't the source code available when using the translation tool? The problem is that some people may not use this translation tool, but use other tools, so there is no guarantee that the source code will be previewed; and your program may not have to publish the source code; translators often only get the ts files we export. If you add comments, the translators can translate the files conveniently. The last parameter n is used to specify whether the string is a complex number. We know that many languages, such as English, have different singular and plural nouns. To solve this problem, Qt provides a parameter n in the tr () function. Please refer to the following code: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> int n = messages. count ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif"/> showMessage (tr ("% n message (s) saved", "", n )); for different values of n, Qt is translated into different texts, for example:
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. If your class does not inherit from the QObject function, you cannot directly use the tr () function. For example, in the main () function, we want to add the code for setting the title of MainWindow: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> W. setWindowTitle (tr ("MyApp"); writing directly in this way cannot be compiled. Because the main () function is a global function, this tr () cannot be found. Solution 1: explicitly call the QObject function: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> W. setWindowTitle (QObject: tr (" MyApp "); or, you can use the translate () function of QCoreApplication. You must remember that the first sentence of our main () function is always QApplication app. In fact, QApplication is a subclass of QCoreApplication. Therefore, we can also write as follows: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> W. setWindowTitle (app. translate ("MyApp"); since QCoreApplication is a singleton class in the Qt program, Qt provides a macro qApp, this example is used to conveniently access QCoreApplication. Therefore, in other files, we can also directly call qApp. translate () to replace tr (), but this is not necessary. If your translation text contains data that needs to be dynamically displayed, such as 650 in our previous Code) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> QMessageBox: information (NULL, tr (" Path "), tr (" You selected \ n % 1 "). arg (path); of course you can write this sentence as 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> QMessageBox: information (NULL, tr (" Path ")," You selected \ n "+ path );
Not AllowedUse the tr () function! Therefore, if you need to format the output and translate it like the printf () function in C language, you must use the % 1 + arg () function in our example! If you want to translate strings outside the function, you need to use two macros QT_TR_NOOP () and QT_TRANSLATE_NOOP (). The former is used to translate a string, and the latter can translate multiple strings. They are used as follows: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> QString FriendlyConversation: greeting (int type)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> static const char * greeting_strings [] = {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> QT_TR_NOOP (" Hello "),
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> QT_TR_NOOP (" Goodbye ")
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> };
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> return tr (greeting_strings [type]);
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif"/>} 650) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> static const char * greeting_strings [] = {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> QT_TRANSLATE_NOOP (" FriendlyConversation "," Hello "),
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> QT_TRANSLATE_NOOP (" FriendlyConversation "," Goodbye ")
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> };
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> QString FriendlyConversation: greeting (int type)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> return tr (greeting_strings [type]);
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> QString global_greeting (int type)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> return qApp-> translate (" FriendlyConversation ",
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> greeting_strings [type]);
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/>} Well, the above is most of the functions and macros we use. In addition, if we run the previous example, we will find that, in fact, we only translate the menu and other content, and the file dialog box is not translated. The reason is that we did not provide international information. So how can we make Qt translate these built-in texts? We want to add a few words in the main () function: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> int main (int argc, char * argv [])
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> QApplication a (argc, argv );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> QTranslator qtTranslator;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> qtTranslator. load (" myapp. qm ");
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> a. installTranslator (& qtTranslator );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> QTranslator qtTranslator2;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> qtTranslator2.load (" qt_zh_CN.qm ");
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> a. installTranslator (& qtTranslator2 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> MainWindow w;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> w. resize (800,600 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> w. show ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/> return a.exe c ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R42120W-0.gif "/>} we added a QTranslator object. Qt actually provides built-in string translation for the qm file. We need to find qt_zh_CN.qm in the translations folder under the Qt installation directory, and copy it to the directory where the exe is located as before. Run the program again: Haha has completely become Chinese! Now, the International Translation of our Qt program is over!
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/245063