|
QtCanpool 3.3.0
|
Loads, installs and switches the translations of an application at run time. 更多...
#include <qxcore/qxtranslator.h>

Public 成员函数 | |
| QxTranslator (QObject *parent=nullptr) | |
| ~QxTranslator () override | |
| QString | translationPath () const |
| Directory scanned for "\<prefix\>_\<language\>.qm"; empty by default. | |
| void | setTranslationPath (const QString &path) |
| Points the loader at path. | |
| QStringList | availableLanguages () const |
| The languages present in translationPath(), read from the file names: "app_zh_CN.qm" contributes "zh_CN". | |
| QString | language () const |
| The language currently applied, or empty while none is. | |
| bool | setLanguage (const QString &language) |
| Installs the translations for language and returns true. | |
| QStringList | loadedFiles () const |
| The files installed by the last successful setLanguage() call. | |
| void | languageChanged (const QString &language) |
| Emitted once language is installed, so a slot may call translate() immediately. | |
Loads, installs and switches the translations of an application at run time.
Qt already owns the hard part: install a QTranslator and every top-level widget receives a QEvent::LanguageChange, which is what translated widgets retranslate themselves on. What is missing around that is the bookkeeping - which languages exist, which files were really loaded, and how to reach the consumers that are not widgets at all. A model or a controller never sees QEvent::LanguageChange, and languageChanged() exists for exactly those; connecting a slot to it is how a non-widget retranslates.
Persisting the chosen language is left to the caller on purpose. It is a single value and an application already has a QxSettings to hold it, so the two-line recipe in the example below beats a dependency here:
在文件 qxtranslator.h 第 42 行定义.
|
explicit |
在文件 qxtranslator.cpp 第 72 行定义.
|
override |
在文件 qxtranslator.cpp 第 78 行定义.
| QStringList QxCore::QxTranslator::availableLanguages | ( | ) | const |
The languages present in translationPath(), read from the file names: "app_zh_CN.qm" contributes "zh_CN".
The prefix is whatever the files carry, so an application's own translations and the ones copied next to them are enumerated together. Sorted and without duplicates.
在文件 qxtranslator.cpp 第 97 行定义.
| QString QxCore::QxTranslator::language | ( | ) | const |
The language currently applied, or empty while none is.
在文件 qxtranslator.cpp 第 122 行定义.
| void QxCore::QxTranslator::languageChanged | ( | const QString & | language | ) |
Emitted once language is installed, so a slot may call translate() immediately.
Widgets do not need it: Qt sends them QEvent::LanguageChange.
| QStringList QxCore::QxTranslator::loadedFiles | ( | ) | const |
The files installed by the last successful setLanguage() call.
在文件 qxtranslator.cpp 第 202 行定义.
| bool QxCore::QxTranslator::setLanguage | ( | const QString & | language | ) |
Installs the translations for language and returns true.
One of the application's own catalogues has to load - every "\<prefix\>_\<language\>.qm" in translationPath() is tried, which is exactly the set availableLanguages() reports, so the two stay in step. "qtbase_\<language\>.qm" from Qt's own translations directory is then loaded on top when it is there, which is how a build that does not deploy Qt's translations still gets Qt's built-in strings. Qt's catalogues are a supplement: their absence is not a failure, and their presence alone is not a success.
When nothing can be loaded nothing changes, so a language that is not installed leaves the application in the one it was already using. An empty language is rejected the same way.
在文件 qxtranslator.cpp 第 128 行定义.
| void QxCore::QxTranslator::setTranslationPath | ( | const QString & | path | ) |
Points the loader at path.
The language in use is not reloaded - the next setLanguage() call is what reads the new directory.
在文件 qxtranslator.cpp 第 91 行定义.
| QString QxCore::QxTranslator::translationPath | ( | ) | const |
Directory scanned for "\<prefix\>_\<language\>.qm"; empty by default.
在文件 qxtranslator.cpp 第 85 行定义.