From 479ec6f1df91c8472b2748430993b0d46e223d37 Mon Sep 17 00:00:00 2001 From: Volker Fischer Date: Sun, 5 Jul 2020 20:49:43 +0200 Subject: [PATCH] store language selection in settings file; add translation help functions in util.h/.cpp --- ChangeLog | 4 +++ src/main.cpp | 31 +++++++++-------------- src/resources.qrc | 42 +++++++------------------------ src/settings.cpp | 16 ++++++++++++ src/settings.h | 5 ++++ src/util.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 11 +++++++-- 7 files changed, 118 insertions(+), 54 deletions(-) diff --git a/ChangeLog b/ChangeLog index 91948b6a..a265811d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -20,6 +20,9 @@ - bug fix: grouping faders in the client should be proportional (see discussion in #202, #419) +TODO add support to change language (#297) + TODO add language selector also in the server GUI + TODO bug fix: incorrect selection of UI language (#408) !!!!!!!!!!!!!!!!!!!!!!!!!!!!! -> note that for the 3.5.8 bug fix release we went back to the original translation code (e.g. no pt_BR!) @@ -28,6 +31,7 @@ TODO add new register message which contains version and, e.g., max number of cl TODO Inconsistency between Input meter and Audio mixer meter #423 TODO improve settings management -> move settings class in client/server classes, move actual settings variables +TODO store recorder settings (#313) diff --git a/src/main.cpp b/src/main.cpp index 74908784..274a08d4 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,8 +25,6 @@ #include #include #include -#include -#include #include "global.h" #ifndef HEADLESS # include @@ -582,23 +580,6 @@ int main ( int argc, char** argv ) // init resources Q_INIT_RESOURCE(resources); - // load translations - QTranslator myappTranslator, myqtTranslator; - - if ( bUseGUI && bUseTranslation ) - { - if ( myappTranslator.load ( QLocale(), "translation", "_", ":/translations" ) ) - { - pApp->installTranslator ( &myappTranslator ); - } - - // allows the Qt messages to be translated in the application - if ( myqtTranslator.load ( QLocale(), "qt", "_", QLibraryInfo::location ( QLibraryInfo::TranslationsPath ) ) ) - { - pApp->installTranslator ( &myqtTranslator ); - } - } - // TEST -> activate the following line to activate the test bench, //CTestbench Testbench ( "127.0.0.1", DEFAULT_PORT_NUMBER ); @@ -620,6 +601,12 @@ int main ( int argc, char** argv ) CClientSettings Settings ( &Client, strIniFileName ); Settings.Load(); + // load translation + if ( bUseGUI && bUseTranslation ) + { + CLocale::LoadTranslation ( Settings.strLanguage, pApp ); + } + #ifndef HEADLESS if ( bUseGUI ) { @@ -673,6 +660,12 @@ int main ( int argc, char** argv ) CServerSettings Settings ( &Server, strIniFileName ); Settings.Load(); + // load translation + if ( bUseGUI && bUseTranslation ) + { + CLocale::LoadTranslation ( Settings.strLanguage, pApp ); + } + // update server list AFTER restoring the settings from the // settings file Server.UpdateServerList(); diff --git a/src/resources.qrc b/src/resources.qrc index 881760dd..c9bba96e 100755 --- a/src/resources.qrc +++ b/src/resources.qrc @@ -1,54 +1,30 @@ - res/translation/translation_de_DE.qm + res/translation/translation_de_DE.qm - res/translation/translation_de_DE.qm + res/translation/translation_fr_FR.qm - res/translation/translation_fr_FR.qm + res/translation/translation_pt_PT.qm - res/translation/translation_fr_FR.qm + res/translation/translation_pt_BR.qm - res/translation/translation_pt_PT.qm + res/translation/translation_es_ES.qm - res/translation/translation_pt_BR.qm + res/translation/translation_nl_NL.qm - res/translation/translation_pt_PT.qm + res/translation/translation_it_IT.qm - res/translation/translation_es_ES.qm + res/translation/translation_pl_PL.qm - res/translation/translation_es_ES.qm - - - res/translation/translation_nl_NL.qm - - - res/translation/translation_nl_NL.qm - - - res/translation/translation_it_IT.qm - - - res/translation/translation_it_IT.qm - - - res/translation/translation_pl_PL.qm - - - res/translation/translation_pl_PL.qm - - - res/translation/translation_sv_SE.qm - - - res/translation/translation_sv_SE.qm + res/translation/translation_sv_SE.qm diff --git a/src/settings.cpp b/src/settings.cpp index 12082c54..0253688e 100755 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -303,6 +303,10 @@ void CClientSettings::ReadFromXML ( const QDomDocument& IniXMLDocument ) bConnectDlgShowAllMusicians = bValue; } + // language + strLanguage = GetIniSetting ( IniXMLDocument, "client", "language", + CLocale::FindSysLangTransFileName ( CLocale::GetAvailableTranslations() ).first ); + // name pClient->ChannelInfo.strName = FromBase64ToString ( GetIniSetting ( IniXMLDocument, "client", "name_base64", @@ -613,6 +617,10 @@ void CClientSettings::WriteToXML ( QDomDocument& IniXMLDocument ) SetFlagIniSet ( IniXMLDocument, "client", "connectdlgshowallmusicians", bConnectDlgShowAllMusicians ); + // language + PutIniSetting ( IniXMLDocument, "client", "language", + strLanguage ); + // name PutIniSetting ( IniXMLDocument, "client", "name_base64", ToBase64 ( pClient->ChannelInfo.strName ) ); @@ -787,6 +795,10 @@ if ( GetFlagIniSet ( IniXMLDocument, "server", "defcentservaddr", bValue ) ) pServer->SetServerListEnabled ( bValue ); } + // language + strLanguage = GetIniSetting ( IniXMLDocument, "server", "language", + CLocale::FindSysLangTransFileName ( CLocale::GetAvailableTranslations() ).first ); + // name pServer->SetServerName ( GetIniSetting ( IniXMLDocument, "server", "name" ) ); @@ -832,6 +844,10 @@ void CServerSettings::WriteToXML ( QDomDocument& IniXMLDocument ) SetFlagIniSet ( IniXMLDocument, "server", "servlistenabled", pServer->GetServerListEnabled() ); + // language + PutIniSetting ( IniXMLDocument, "server", "language", + strLanguage ); + // name PutIniSetting ( IniXMLDocument, "server", "name", pServer->GetServerName() ); diff --git a/src/settings.h b/src/settings.h index 751a4922..a4d1adac 100755 --- a/src/settings.h +++ b/src/settings.h @@ -120,6 +120,7 @@ public: vstrIPAddress ( MAX_NUM_SERVER_ADDR_ITEMS, "" ), iNewClientFaderLevel ( 100 ), bConnectDlgShowAllMusicians ( true ), + strLanguage ( "" ), vecWindowPosMain ( ), // empty array vecWindowPosSettings ( ), // empty array vecWindowPosChat ( ), // empty array @@ -141,6 +142,7 @@ public: CVector vstrIPAddress; int iNewClientFaderLevel; bool bConnectDlgShowAllMusicians; + QString strLanguage; // window position/state settings QByteArray vecWindowPosMain; @@ -165,10 +167,13 @@ class CServerSettings : public CSettings { public: CServerSettings ( CServer* pNSerP, const QString& sNFiName ) : + strLanguage ( "" ), vecWindowPosMain ( ), // empty array pServer ( pNSerP ) { SetFileName ( sNFiName, DEFAULT_INI_FILE_NAME_SERVER); } + QString strLanguage; + // window position/state settings QByteArray vecWindowPosMain; diff --git a/src/util.cpp b/src/util.cpp index f194f53f..4c38900f 100755 --- a/src/util.cpp +++ b/src/util.cpp @@ -1375,6 +1375,69 @@ ECSAddType CLocale::GetCentralServerAddressType ( const QLocale::Country eCountr } } +QMap CLocale::GetAvailableTranslations() +{ + QMap TranslMap; + QDirIterator DirIter ( ":/translations" ); + + while ( DirIter.hasNext() ) + { + // get alias of translation file + const QString strCurFileName = DirIter.next(); + + // extract only language code (must be at the end, separated with a "_") + const QString strLoc = strCurFileName.right ( strCurFileName.length() - strCurFileName.indexOf ( "_" ) - 1 ); + + TranslMap[strLoc] = strCurFileName; + } + + return TranslMap; +} + +QPair CLocale::FindSysLangTransFileName ( const QMap& TranslMap ) +{ + QPair PairSysLang ( "", "" ); + QStringList slUiLang = QLocale().uiLanguages(); + + if ( !slUiLang.isEmpty() ) + { + // only extract two first characters to identify language (ignoring + // location for getting a simpler implementation -> if the language + // is not correct, the user can change it in the GUI anyway) + const QString strUiLang = QLocale().uiLanguages().at ( 0 ); + + if ( strUiLang.length() >= 2 ) + { + PairSysLang.first = strUiLang.left ( 2 ); + PairSysLang.second = TranslMap[PairSysLang.first]; + } + } + + return PairSysLang; +} + +void CLocale::LoadTranslation ( const QString strLanguage, + QCoreApplication* pApp ) +{ + // The translator objects must be static! + static QTranslator myappTranslator; + static QTranslator myqtTranslator; + + QMap TranslMap = CLocale::GetAvailableTranslations(); + const QString strTranslationFileName = TranslMap[strLanguage]; + + if ( myappTranslator.load ( strTranslationFileName ) ) + { + pApp->installTranslator ( &myappTranslator ); + } + + // allows the Qt messages to be translated in the application + if ( myqtTranslator.load ( QLocale ( strLanguage ), "qt", "_", QLibraryInfo::location ( QLibraryInfo::TranslationsPath ) ) ) + { + pApp->installTranslator ( &myqtTranslator ); + } +} + // Console writer factory ------------------------------------------------------ QTextStream* ConsoleWriterFactory::get() diff --git a/src/util.h b/src/util.h index 7fe41410..177202f8 100755 --- a/src/util.h +++ b/src/util.h @@ -41,6 +41,9 @@ # include "ui_aboutdlgbase.h" #endif #include +#include +#include +#include #include #include #include @@ -870,8 +873,12 @@ protected: class CLocale { public: - static QString GetCountryFlagIconsResourceReference ( const QLocale::Country eCountry ); - static ECSAddType GetCentralServerAddressType ( const QLocale::Country eCountry ); + static QString GetCountryFlagIconsResourceReference ( const QLocale::Country eCountry ); + static ECSAddType GetCentralServerAddressType ( const QLocale::Country eCountry ); + static QMap GetAvailableTranslations(); + static QPair FindSysLangTransFileName ( const QMap& TranslMap ); + static void LoadTranslation ( const QString strLanguage, + QCoreApplication* pApp ); };