Qt's internationalization (System Text - QMessageBox button, QLineEdit right-click menu, etc.)

Original: https://blog.csdn.net/liang19890820/article/details/50320153

Brief introduction

When using Qt, English often encounter problems, such as: QMessageBox the button, QLineEdit, QSpinBox, QScrollBar in the right-click menu and so on. Under normal circumstances, our software will not be plain English, how to deal with these problems? In fact, this is also part of internationalization.

Write pictures described here Write pictures described here Write pictures described here

Now, switching to English as an example for everyone to share, other similar language switching.

| Copyright: a go, two or three miles, shall not be reproduced without the bloggers allowed.

Find the translation of documents

  • qt_zh_CN.qm

See the name to know righteousness, which is handling Chinese translation file.

We can find from the installation directory Qt in it, in my directory, for example: D: \ Qt \ Qt5.5.1 \ 5.5 \ msvc2013 \ translations.

Without this file, do not worry, we can find the translation of the source file corresponding qt_zh_CN.ts. Then lrelease, qt_zh_CN.qm generate the corresponding file.

Load the translation file

QTranslator translator;  
translator.load(":/qm/qt_zh_CN.qm");  
qApp->installTranslator(&translator); 
  
  
  • 1
  • 2
  • 3

A bit mean, so soon to deal with for the Chinese.

Write pictures described here Write pictures described here Write pictures described here

Switch language

After loading for the Chinese, we assume that there are multiple language switching problem, then when I switched to Chinese language, before qt_zh_CN.qm has been loaded, how then switch back?

Well, since there is load, so not surprisingly, then there will unload it. Sorry, complete turn all of the API we did not find this interface!

Continue to analyze, installTranslator is installed translation files meant, then we can find a way to uninstall it, it is easy to find by looking removeTranslator interfaces.

Explained the official website of this interface is as follows:

Removes the translation file translationFile from the list of translation files used by this application. (It does not delete the translation file from the file system.)

In other words: After removing the translation of documents from the file list translation, translation is not deleted from the file system.

So, by calling this interface, you can remove translated files. Once the translation files are removed, then the previously loaded Languages ​​also invalid.

if (language != MyApp::English)
{
    m_systemTranslator.load(QString(":/qm/qt_zh_CN.qm"));
    qApp->installTranslator(&translator);
}
else
{
    qApp->removeTranslator(&translator);
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

As above, when the language switching determination current language, if Chinese qt_zh_CN.qm translation file is loaded, and then install the translator. Otherwise, uninstall!

In this way, the loading time corresponding to the language, whether it is a custom text, or the text system, we are free to switch up.

Guess you like

Origin blog.csdn.net/a844651990/article/details/84667413