/** \file LanguagesManager.cpp \brief Define the class to manage and load the languages \author alpha_one_x86 \licence GPL3, see the file COPYING */ #include #include #include "LanguagesManager.h" #include "FacilityEngine.h" #include "cpp11addition.h" /// \brief Create the manager and load the defaults variables LanguagesManager::LanguagesManager() { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start"); //load the rest std::vector resourcesPaths=ResourcesManager::resourcesManager->getReadPath(); unsigned int index=0; while(indexlockPluginListEdition(); connect(this,&LanguagesManager::previouslyPluginAdded, this, &LanguagesManager::onePluginAdded,Qt::QueuedConnection); connect(PluginsManager::pluginsManager,&PluginsManager::onePluginAdded,this, &LanguagesManager::onePluginAdded,Qt::QueuedConnection); #ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE connect(PluginsManager::pluginsManager,&PluginsManager::onePluginWillBeRemoved, this, &LanguagesManager::onePluginWillBeRemoved,Qt::DirectConnection); #endif connect(PluginsManager::pluginsManager,&PluginsManager::pluginListingIsfinish, this, &LanguagesManager::allPluginIsLoaded,Qt::QueuedConnection); std::vector list=PluginsManager::pluginsManager->getPluginsByCategory(PluginType_Languages); foreach(PluginsAvailable currentPlugin,list) emit previouslyPluginAdded(currentPlugin); PluginsManager::pluginsManager->unlockPluginListEdition(); } else ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"plugin manager null"); //load the GUI option if(OptionEngine::optionEngine!=NULL) { std::vector > KeysList; KeysList.push_back(std::pair("Language","en")); KeysList.push_back(std::pair("Language_force","false")); OptionEngine::optionEngine->addOptionGroup("Language",KeysList); // connect(this, &LanguagesManager::newLanguageLoaded, plugins,&PluginsManager::refreshPluginList); // connect(this, &LanguagesManager::newLanguageLoaded, this,&LanguagesManager::retranslateTheUI); connect(OptionEngine::optionEngine,&OptionEngine::newOptionValue, this, &LanguagesManager::newOptionValue,Qt::QueuedConnection); } else ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"option engine null"); connect(this, &LanguagesManager::newLanguageLoaded, PluginsManager::pluginsManager,&PluginsManager::newLanguageLoaded,Qt::QueuedConnection); } /// \brief Destroy the language manager LanguagesManager::~LanguagesManager() { } /// \brief load the language selected, return the main short code like en, fr, .. std::string LanguagesManager::getTheRightLanguage() const { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start"); if(LanguagesAvailableList.size()==0) { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"empty combobox list, failing back to english"); return "en"; } else { if(!stringtobool(OptionEngine::optionEngine->getOptionValue("Language","Language_force"))) { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"language auto-detection, QLocale::system().name(): "+QLocale::system().name().toStdString()+", QLocale::languageToString(QLocale::system().language()): "+QLocale::languageToString(QLocale::system().language()).toStdString()); std::string tempLanguage=getMainShortName(QLocale::languageToString(QLocale::system().language()).toStdString()); if(!tempLanguage.empty()) return tempLanguage; else { tempLanguage=getMainShortName(QLocale::system().name().toStdString()); if(!tempLanguage.empty()) return tempLanguage; else { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Autodetection of the language failed, QLocale::languageToString(QLocale::system().language()): "+QLocale::languageToString(QLocale::system().language()).toStdString()+", QLocale::system().name(): "+QLocale::system().name().toStdString()+", failing back to english"); return OptionEngine::optionEngine->getOptionValue("Language","Language"); } } } else return OptionEngine::optionEngine->getOptionValue("Language","Language"); } } /* \brief To set the current language \param newLanguage Should be short name code found into informations.xml of language file */ void LanguagesManager::setCurrentLanguage(const std::string &newLanguage) { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start: "+newLanguage); //protection for re-set the same language if(currentLanguage==newLanguage) return; //store the language PluginsManager::pluginsManager->setLanguage(newLanguage); //unload the old language if(currentLanguage!="en") { unsigned int indexTranslator=0; while(indexTranslator fileToLoad; //load the language main if(newLanguage=="en") fileToLoad.push_back(":/Languages/en/translation.qm"); else fileToLoad.push_back(LanguagesAvailableList.at(index).path+"translation.qm"); //load the language plugin std::vector listLoadedPlugins=PluginsManager::pluginsManager->getPlugins(); unsigned int indexPluginIndex=0; while(indexPluginIndexload(QString::fromStdString(fileToLoad.at(indexTranslationFile))) || temp->isEmpty()) { delete temp; ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Unable to load the translation file: "+fileToLoad.at(indexTranslationFile)); } else { QCoreApplication::installTranslator(temp); installedTranslator.push_back(temp); } indexTranslationFile++; } temp=new QTranslator(); if(temp->load(QString("qt_")+QString::fromStdString(newLanguage), QLibraryInfo::location(QLibraryInfo::TranslationsPath)) && !temp->isEmpty()) { QCoreApplication::installTranslator(temp); installedTranslator.push_back(temp); } else { if(!temp->load(QString::fromStdString(LanguagesAvailableList.at(index).path)+"qt.qm") || temp->isEmpty()) { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Unable to load the translation file: qt.qm, into: "+LanguagesAvailableList.at(index).path); delete temp; } else { QCoreApplication::installTranslator(temp); installedTranslator.push_back(temp); } } } currentLanguage=newLanguage; FacilityEngine::facilityEngine.retranslate(); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"emit newLanguageLoaded()"); emit newLanguageLoaded(currentLanguage); return; } index++; } ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"unable to found language: "+newLanguage+", LanguagesAvailableList.size(): "+std::to_string(LanguagesAvailableList.size())); } /// \brief check if short name is found into language std::string LanguagesManager::getMainShortName(const std::string &shortName) const { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start"); unsigned int index=0; while(indexallPluginHaveBeenLoaded()) setCurrentLanguage(getTheRightLanguage()); } #ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE void LanguagesManager::onePluginWillBeRemoved(const PluginsAvailable &plugin) { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start"); unsigned int index=0; while(index