Locale language manager class for QT
Language management in a multilingual Qt application made easier.
|
LlmQtLocaleLanguageManager is an utilitarian class that simplifies language choice management in a multilingual Qt application. More...
#include <LlmQtLocaleLanguageManager.h>
Public Member Functions | |
bool | analyseAvailableLanguages (QString *sReturnErrMsg=nullptr) |
Analyse which are the available languages/translations for the application. More... | |
QString | currentLanguageNativeName () |
Return the name of the current language, in native form. More... | |
bool | isSystemLanguageAvailable () |
Indicate if one of the system languages is available among the qm files. More... | |
bool | isSystemLanguageChosenByUser () |
Indicate if the Language choice is "System language". More... | |
bool | isUsable () |
Indicate if the class is usable. More... | |
QStringList | languageNameNativeList (bool bIntegrateSystemLanguageFlag) |
List of the available languages, in native form. More... | |
LlmQtLocaleLanguageManager (const LlmQtLocaleLanguageManager &)=delete | |
LlmQtLocaleLanguageManager & | operator= (const LlmQtLocaleLanguageManager &)=delete |
void | setKeySettingsTranslateLanguage (const QString &sKey) |
Modify the key to access "Locale-language" value in user's settings. More... | |
void | setQmFilesDirectory (const QString &sQmFilesDirectory) |
Set qm files directory. More... | |
void | setSettings (const std::shared_ptr< QSettings > &settings) |
Set a user QSettings object to the language manager class (Use of that feature is optional). More... | |
bool | setSourceLanguage (QLocale::Language languageQtFlag, QLocale::Country countryQtFlag=QLocale::AnyCountry, QString *sReturnErrMsg=nullptr) |
Set the source's/primary languages informations. More... | |
void | setSourceLanguageFromQAppLocalSettings () |
Guess source's interface language, from the default QLocal's values. More... | |
QString | sourceLanguageNativeName () |
The native name of the interface (dialog panels) language used in sources. More... | |
QString | systemLanguageNativeName () |
Return the name of the available system language name, in this native form (available among qm files). More... | |
bool | translateAppToLanguage (const QString &sLanguageNativeOrEnglishNameWithCountry, QString *sReturnErrMsg=nullptr) |
Translate the application to the the chosen language. More... | |
bool | translateAppToSystemLanguage (QString *sReturnErrMsg=nullptr) |
Translate the application to an available system language. More... | |
bool | translateAppToUserSettingsLanguage (QString *sReturnErrMsg=nullptr) |
Translate the application to the language indicated in the settings m_settings, under the language key. More... | |
LlmQtLocaleLanguageManager is an utilitarian class that simplifies language choice management in a multilingual Qt application.
It provide methods to deal with .qm files, app's active language, system language, language switch, choice storing in user settings... It handle all the possible error during .qm files analysis. Dialog's straight translation at running time is possible.
How to use explanations and use examples are in main folder's README.md .
bool LlmQtLocaleLanguageManager::analyseAvailableLanguages | ( | QString * | sReturnErrMsg = nullptr | ) |
Analyse which are the available languages/translations for the application.
The analysis is done according the .qm files found under m_qmFilesDirectory. LlmQtLocaleLanguageManager::setQmFilesDirectory() has to be called before. If .qm file is added/removed during running time, this method update the analysis.
Also, it look to the system languages, and if our app is translatable to a system language, that language is stored in m_systemLanguage.
In case of error, the method has 2 behaviours:
[out] | sReturnErrMsg | Description of error |
runtime_error | Exceptions are raised only if sReturnErrMsg is not used. Throws an exception if:
|
QString LlmQtLocaleLanguageManager::currentLanguageNativeName | ( | ) |
Return the name of the current language, in native form.
bool LlmQtLocaleLanguageManager::isSystemLanguageAvailable | ( | ) |
Indicate if one of the system languages is available among the qm files.
An analysis need to be done before calling this method, with LlmQtLocaleLanguageManager::analyseAvailableLanguages().
bool LlmQtLocaleLanguageManager::isSystemLanguageChosenByUser | ( | ) |
Indicate if the Language choice is "System language".
bool LlmQtLocaleLanguageManager::isUsable | ( | ) |
Indicate if the class is usable.
= qm files available + source language known.
QStringList LlmQtLocaleLanguageManager::languageNameNativeList | ( | bool | bIntegrateSystemLanguageFlag | ) |
List of the available languages, in native form.
The list include the source language, and a SYSTEM flag if asked (and if the system language's translation is available). If a language has a country specification, the language name will be: "language name (Country code)".
[in] | bIntegrateSystemLanguageFlag | Indicate if a SYSTEM flag is needed in the returned list. |
void LlmQtLocaleLanguageManager::setKeySettingsTranslateLanguage | ( | const QString & | sKey | ) |
Modify the key to access "Locale-language" value in user's settings.
If not set, the default key is "Locale/Language"
[in] | sKey | The new key |
void LlmQtLocaleLanguageManager::setQmFilesDirectory | ( | const QString & | sQmFilesDirectory | ) |
Set qm files directory.
sQmFilesDirectory | Full path |
void LlmQtLocaleLanguageManager::setSettings | ( | const std::shared_ptr< QSettings > & | settings | ) |
Set a user QSettings object to the language manager class (Use of that feature is optional).
If set, that QSettings object will be used to store the chosen languages. If not set, the language changes are just not saved, without any warning.
[in] | settings | Settings object to be used |
bool LlmQtLocaleLanguageManager::setSourceLanguage | ( | QLocale::Language | languageQtFlag, |
QLocale::Country | countryQtFlag = QLocale::AnyCountry , |
||
QString * | sReturnErrMsg = nullptr |
||
) |
Set the source's/primary languages informations.
The method is an alternative to LlmQtLocaleLanguageManager::setSourceLanguageFromQAppLocalSettings(), and is a better solution. This method is more "precise", and has to be privileged. It permit to set countryQtFlag to QLocale::AnyCountry.
[in] | languageQtFlag | Languages Qt flag, from the QLocale::Language enum |
[in] | countryQtFlag | Country Qt flag, from the QLocale::Country enum |
[out] | sReturnErrMsg | description of error |
runtime_error | Exceptions are raised only if sReturnErrMsg is not used. Throws an exception if the language/country couple is not compatible. |
void LlmQtLocaleLanguageManager::setSourceLanguageFromQAppLocalSettings | ( | ) |
Guess source's interface language, from the default QLocal's values.
The method is an alternative to LlmQtLocaleLanguageManager::setSourceLanguage(), but prefer use LlmQtLocaleLanguageManager::setSourceLanguage() instead of that one.
QString LlmQtLocaleLanguageManager::sourceLanguageNativeName | ( | ) |
The native name of the interface (dialog panels) language used in sources.
QString LlmQtLocaleLanguageManager::systemLanguageNativeName | ( | ) |
Return the name of the available system language name, in this native form (available among qm files).
An analysis need to be done before calling this method, with LlmQtLocaleLanguageManager::analyseAvailableLanguages().
bool LlmQtLocaleLanguageManager::translateAppToLanguage | ( | const QString & | sLanguageNativeOrEnglishNameWithCountry, |
QString * | sReturnErrMsg = nullptr |
||
) |
Translate the application to the the chosen language.
The default QLocale is also modified according the new language. The language name can be either in it's Native form or in it's English form (Français or French), it doesn't matter. It can contain the country code in brackets. (as given by LlmQtLocaleLanguageManager::languageNameNativeList()).
If a QSetting has been set, (thanks to LlmQtLocaleLanguageManager::setSettings() ), saves the changes in user's settings.
In case of error, the method has 2 behaviours:
[in] | sLanguageNativeOrEnglishNameWithCountry | The wanted language name in this Native form or in this English form (Français or French). |
[out] | sReturnErrMsg | description of error |
runtime_error | Exceptions are raised only if sReturnErrMsg is not used. Throws an exception if:
|
bool LlmQtLocaleLanguageManager::translateAppToSystemLanguage | ( | QString * | sReturnErrMsg = nullptr | ) |
Translate the application to an available system language.
(Available among the qm files) Before using that method, make sure a system language translation is available thanks LlmQtLocaleLanguageManager::isSystemLanguageAvailable(). If a QSetting has been set, (thanks to LlmQtLocaleLanguageManager::setSettings() ), saves the changes in user's settings.
In case of error, the method has 2 behaviours:
[out] | sReturnErrMsg | description of error |
runtime_error | Exceptions are raised only if sReturnErrMsg is not used. Throws an exception if :
|
bool LlmQtLocaleLanguageManager::translateAppToUserSettingsLanguage | ( | QString * | sReturnErrMsg = nullptr | ) |
Translate the application to the language indicated in the settings m_settings, under the language key.
The key is indicated thanks to LlmQtLocaleLanguageManager::setKeySettingsTranslateLanguage(). A QSetting has to be set first, thanks to LlmQtLocaleLanguageManager::setSettings().
In case of error, the method has 2 behaviours:
[out] | sReturnErrMsg | description of error |
runtime_error | Exceptions are raised only if sReturnErrMsg is not used. Throws an exception if: |