QT itself provides an international solution, not the way to use INI configuration files.Use Qstring. The Qtranslator class and TR () functions can be easily completed to increase internationalization support.
(1) using the Qstring object to represent all the user-visible text, Qstring is implemented with an internal Unicode encoding.
(2) Use the TR () function to get all the text that needs to be translated.
(3) Organize dynamic text using Qstring::arg ().eg:qstring str = TR ("Mission status:%1 of%2 is completed". Arg (x). Arg (y));
(4) Use Qtranslator::load () and Qcoreapplication::installtranslator () to read the corresponding translated resource files. and install the Qtranslator object.
Detailed operations such as the following:
The first step in your Pro add translations = Xx.ts The second step with lupdate operation Pro will translate the extracted to TS file. The command is Lupdate Xx.pro The third step with linguist to open the TS file just now, Linugist is inside the folder of the Qt bin, is an interface tool to open the linguist after using the menu bar file->open Open the corresponding TS file as my Exec_zh.ts Open you will see the corresponding class to the right of the upper half of the corresponding class inside the TR inside the content, so in the program TR inside is the E-text here to use the next half is the language you want to translate the corresponding thing, is that you input Chinese places to pay attention to those punctuation It may be useful to enter the status as save as ... in e-text. It should be saved as .... Instead of saving as:
..
Note the difference in the following punctuation, in which the green representation is translated, the exclamation point is not translated, the cross represents no translation the fourth step is to use the linguist menu to save your translated TS file, and then use the Linguist Interface tool inside the menu file inside the release ..., click this will pop up a dialog box prompting you to enter the file name of the QM file. You can use the default. Press OK and you will see a QM suffix file in the folder where the TS file is located, which is the file that the program translator will use. Fifth step. Use the QM file you just obtained in your program, such as the following: int main (int argc,char **argv) { qapplication app (ARGC,ARGV); Qtranslator qttranslator (0); qttranslator.load ("TEST.QM", "."); //and name corresponding to app.installtranslator (&qttranslator); window window; Window.setfont (Qfont ("Unifont", 16));//Set Font window.setmaximumsize (0, 0); window.showminimized (); return App.exec ();} Note To install the translator in front of the interface.
for the need to dynamically change the language, assume that the first step has been installed in your Pro add translations = Xx.ts The second step with lupdate Operation Pro will be translated extract to ts file command is lupdate Xx.pro the third step with Linguist Open just the TS file, Linugist is inside the folder of the Qt bin, is an interface tool to open linguist after using the menu bar file->open Open the corresponding TS files as just Myexec_zh.ts After opening you will see the corresponding class to the right of the upper half of the corresponding class inside the TR inside the content, so in the program TR inside is the e-text to use the next half is the language you want to translate the corresponding things. Is the place where you enter the Chinese language to note is that the punctuation may be used in the E-text input state such as save as .... It should be saved as .... Instead of saving as .... Notice the difference between the punctuation in the back, and the green representation of the translation is good. The expression of the exclamation mark is not translated to, the cross represents no translation the fourth step is to first save your translated TS file with the linguist menu. Then use the Linguist Interface tool inside the menu file inside the release ..., click on this will pop up a dialog box prompts you to enter the QM file name, with the default will be able to.
Press OK after you see a file with a QM suffix name in the folder where the TS file is located. This is the file to be used by the program translator. Fifth step. Use the QM file method you just got inside your program such as the following int main (int argc,char **argv) { qapplication app (ARGC,ARGV); Qtranslator qttranslator (0); qttranslator.load ("TEST.QM", "."); //and name corresponding to app.installtranslator (&qttranslator); window window; Window.setfont (Qfont ("Unifont", 16));//Set Font window.setmaximumsize (0, 0); window.showminimized (); return App.exec ();} Note Be sure to install the translator before installing the interface.
For situations where the language needs to be changed dynamically. The ability to define a static Qtranslator object for easy removal and another installation.Reference code such as the following:
void Langswitch::changetr (const qstring& langcode) {static qtranslator* translator; if (translator! = NULL) {qapp->removetranslator (translator); Delete translator; translator = NULL; } translator = new Qtranslator; QString qmfilename = "Lang_" + langcode; if (Translator->load (QString ("d:/qtpro/ch14/ch1402/langswitch/") +qmfilename)) {qapp->installtranslator (tr Anslator); }}
QT Multi-national language internationalization