After a few days of effort, from the beginning can only system static modification of the language, to a layer of interface modification, and then to the multi-layer interface dynamic switching, here to record the process
1. Considerations when writing code
First in the creation of the UI as far as possible in English name, although later my Chinese name is not a problem, but sometimes there will be garbled problems. Then the text in the UI is modified well, just keep "translatable" on the line,
If there is code to write the SetText, then must be added to the TR prefix,
Such as:
Label->settext (TR ("Hello World again!"));
All text needed to be translated must be wrapped in tr.
2. Create a language file
Open a. Pro file and add the following:
Translations + = cn.ts \
En.ts
Tools, external->QT language home, updated translations, will generate translation files Cn.ts
3. Edit the translation file
Start linguist (QT language), open cn.ts, and then edit, mainly to the source text corresponding to fill in, and finally save
4. Deploying translation Files
Tools, external->QT language home, deployment translation, will generate the language file QM
5 Adding QM files in the res of the project
.
6, if the need for static direct start translation, then directly add the following command in the main function
Qtranslator Translator;
Translator.load (QString (":/language/cn.qm");
A.installtranslator (&translator);
7. Dynamic translation
For example, create a button and then click it to translate directly, then create a slot in the button and enter
void dialog_language::on_btn_lan_chinese_clicked ()
{
trans.load (":/language/zh_cn.qm");
Qapp->installtranslator (&trans);
Ui->retranslateui (this);
}
Now you can complete the translation of the current interface, and then if you need each page, you need to add
void Dialog_language::changeevent (qevent *e)
{
qwidget::changeevent (e);
Switch (E->type ()) {case
Qevent::languagechange:
Ui->retranslateui (this);
break;
Default: Break
;
}
}
Note: If there is a label in the program SetText way to display the text, you need to write a function to refresh the UI, and then put in the inside of translate, the reason is currently unknown, but not put the words will cause the display is not, and only need to put in the first interface refresh function
Like what
void Dialog_language::updateui ()
{
ui->lbl_text_info->settext (tr ("Write com_port, Baud rate and SW Version as photos. Then click ' Check '. "));
And then
void dialog_language::on_btn_lan_chinese_clicked ()
{
trans.load (":/language/zh_cn.qm");
Qapp->installtranslator (&trans);
Ui->retranslateui (this);
UpdateUI ();
}