QtCanpool 3.3.0
载入中...
搜索中...
未找到
QxCore::QxTranslator类 参考

Loads, installs and switches the translations of an application at run time. 更多...

#include <qxcore/qxtranslator.h>

类 QxCore::QxTranslator 继承关系图:

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 translator;
translator.setTranslationPath(dir);
connect(&translator, &QxTranslator::languageChanged, &settings, [&settings](const QString &language) {
settings.setValue("ui/language", language);
});
translator.setLanguage(settings.stringValue("ui/language", QLocale::system().name()));
Loads, installs and switches the translations of an application at run time.
void languageChanged(const QString &language)
Emitted once language is installed, so a slot may call translate() immediately.
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.
void setTranslationPath(const QString &path)
Points the loader at path.

在文件 qxtranslator.h 第 42 行定义.

构造及析构函数说明

◆ QxTranslator()

QxCore::QxTranslator::QxTranslator ( QObject *  parent = nullptr)
explicit

在文件 qxtranslator.cpp 第 72 行定义.

◆ ~QxTranslator()

QxCore::QxTranslator::~QxTranslator ( )
override

在文件 qxtranslator.cpp 第 78 行定义.

成员函数说明

◆ availableLanguages()

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 行定义.

◆ language()

QString QxCore::QxTranslator::language ( ) const

The language currently applied, or empty while none is.

在文件 qxtranslator.cpp 第 122 行定义.

◆ languageChanged()

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.

◆ loadedFiles()

QStringList QxCore::QxTranslator::loadedFiles ( ) const

The files installed by the last successful setLanguage() call.

在文件 qxtranslator.cpp 第 202 行定义.

◆ setLanguage()

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 行定义.

◆ setTranslationPath()

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 行定义.

◆ translationPath()

QString QxCore::QxTranslator::translationPath ( ) const

Directory scanned for "\<prefix\>_\<language\>.qm"; empty by default.

在文件 qxtranslator.cpp 第 85 行定义.


该类的文档由以下文件生成: