QT for multi-language switching

Source: Internet
Author: User

 

Functional Requirements:

Network disk Client to be able to achieve multi-language switching, the first version to support the Chinese and English switching. In the implementation process to feel QT to multi-language support is very good, the production of multi-language package is very convenient, switching logic is very simple. Here's how to make a multi-language package in Qt.

Implementation method:

The most critical place to support internationalization is the creation of multi-language packs, which are then dynamically switched. QT can be used in both command line and QT Creator interface operation to generate, here we use QT Creator to generate multi-language packs. The basic flow is, Mr. TS file, then generate QM file, finally through the Qtranslator class to load QM file, implement multi-language package switch. Note: In order to more fully implement the multi-language package, all the buttons in the program that involve the interface or the text display in the menu, you must use the Qobject::tr ("...") method to wrap the contents of the display, the reason is: only add qobject::tr (), When you generate TS files, the program will assume that the QOBJECT::TR () parcel is in a multi-lingual location, so you can place it in TS.

1> Generating TS Files

Added in QT project file (. Pro): Translations = Xxx.ts Yyy.ts (if multiple lines, use \ Split)

Then execute "Tools"-"external"-"QT Language Home"-"Update translation (Lupdate)", as shown, after the execution of the project will generate corresponding xxx.ts, yyy.ts file.

Note: If the project contains UI files, such as: FORMS + = Zzz.ui, the character display on all UI files is included in the generated TS file, and the text display on the interface is wrapped by Qobejct::tr ("") by default when making the UI file. Because there is one property for all controls in the UI by default: Translatable, which means that the default is nested with Qobject::tr ().

2. Generate QM File

Using QT language home: Linguist open the generated TS file and write the corresponding language in the place where it needs to be translated, as shown in:

After the TS file translation is complete, the "tools"-"external"-"QT Language Home"-"deploy translation (Lrelease)" in Qt Creator will generate the QM file that is ultimately used, as shown in:

3. Loading the QM Language pack

When the QM language pack is finished, the work is finished more than half. The next task is to implement the loading of the language packs in the code, which is to load different language packs based on different choices.

[CPP]View Plaincopy
  1. void Lhsyncclientprivate::inituibylanguage (const QString strlanguage)
  2. {
  3. if (Strlanguage.isempty ())
  4. {
  5. return;
  6. }
  7. QString Strlanguagefile;
  8. if (Strlanguage.compare ("en") = = 0)
  9. {
  10. Strlanguagefile = Qapp->applicationdirpath () + QString ("/languages/%1/%2"). Arg (Lht_syncclient_version_  prodocutname). Arg (lht_syncclient_en_file);
  11. }
  12. Else if (Strlanguage.compare ("zh") = = 0)
  13. {
  14. Strlanguagefile = Qapp->applicationdirpath () + QString ("/languages/%1/%2"). Arg (Lht_syncclient_version_  prodocutname). Arg (lht_syncclient_zh_file);
  15. }
  16. if (QFile (strlanguagefile). Exists ())
  17. {
  18. M_translator->load (Strlanguagefile);
  19. Qapp->installtranslator (M_translator);
  20. }
  21. Else
  22. {
  23. Qdebug () << "[HOUQD] authclient language file does not exists ...";
  24. }
  25. }
Where M_translator is the Qtranslator instance, it is assigned a value in the constructor of the class: M_translator = new Qtranslator; The implementation process is simply to get the absolute path of the language pack and then use Qtranslator to load it And finally installed using Qapp->installtranslator (m_translator).

4. Reset interface Display

Reset the display of the interface, this step is a lot of people easy to forget a step, if it is ignored, often can not complete the language switch, that is: re-set the need to display the elements, the process is done after the above inituibylanguage process, the relevant code is as follows:

[CPP]View Plaincopy
  1. void Lhsyncclientprivate::retranslateui ()
  2. {
  3. M_wgtsync->setwindowtitle (tr ("drive Client"));
  4. //! left sync information
  5. M_btnsynchronizing->settext (tr ("Synchronizing"));
  6. M_btnsynchronized->settext (tr ("Synchronized"));
  7. //! Right Panel
  8. M_lblsyncstatus->settext (tr ("Looking for files changes ..."));
  9. M_lblshowrecordsnum->settext (TR ("There is N Records ..."));
  10. M_btnsyncorpause->settext (tr ("Sync Or Pause"));
  11. M_btnclearall->settext (tr ("Clear All"));
  12. M_btnsyncorpause->settext (tr ("Sync Or Pause"));
  13. M_btnclose->settext (tr ("Close"));
  14. M_actsync->settext (tr ("Sync Info"));
  15. }
Implementation results:

Well, the whole process of making the language pack is basically done. Here also need to mention a bit in the network disk client implementation of the design idea: because the network disk in the login and after login can operate menu is not the same, to involve a dynamic loading way, you can implement a Uiloader plug-in, it implements all the form loading (i.e. Loadui () return qwidget handle), menu loading, language switching, in which the Uiloader plugin may not complete the specific work, but only to achieve the signal forwarding function. Similarly, it is necessary to implement a logic control plug-in that is responsible for all the menu logic, while for each form it is done by its own form plug-in. The implementation of multi-language switching is as follows:

Switch to Chinese:

Post-Toggle Effects:

Refueling, adhere to the daily study!!

QT for multi-language switching

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.