1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00
vcmi/launcher/languages.cpp

95 lines
2.9 KiB
C++
Raw Normal View History

/*
* languages.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "languages.h"
#include "../lib/CConfigHandler.h"
#include "../lib/Languages.h"
#include <QComboBox>
#include <QListWidget>
// list of language names, for generation of translations. Do not use directly, use Languages namespace instead
static const std::array<std::string, 11> languageTranslatedNamesGenerator = {
{
QT_TRANSLATE_NOOP("Language", "Chinese"),
QT_TRANSLATE_NOOP("Language", "English"),
QT_TRANSLATE_NOOP("Language", "French"),
QT_TRANSLATE_NOOP("Language", "German"),
QT_TRANSLATE_NOOP("Language", "Korean"),
QT_TRANSLATE_NOOP("Language", "Polish"),
QT_TRANSLATE_NOOP("Language", "Russian"),
QT_TRANSLATE_NOOP("Language", "Ukrainian"),
QT_TRANSLATE_NOOP("Language", "Other (East European)"),
QT_TRANSLATE_NOOP("Language", "Other (Cyrillic Script)"),
QT_TRANSLATE_NOOP("Language", "Other (West European)"),
}
};
static_assert(languageTranslatedNamesGenerator.size() == static_cast<size_t>(Languages::ELanguages::COUNT), "Languages array is missing a value!");
QString Languages::generateLanguageName(const Languages::Options & language)
{
std::string activeLanguage = settings["general"]["language"].String();
QString localizedName = QApplication::translate("Language", language.nameEnglish.c_str());
QString nativeName = language.nameNative.c_str();
if(!language.hasTranslation)
return localizedName;
2023-03-08 02:01:56 +02:00
if(activeLanguage == language.identifier)
return nativeName;
QString displayName = QString("%1 (%2)").arg(localizedName, nativeName);
return displayName;
}
void Languages::fillLanguages(QComboBox * widget)
{
widget->blockSignals(true); // we do not want calls caused by initialization
widget->clear();
std::string activeLanguage = settings["general"]["language"].String();
for(const auto & language : Languages::getLanguageList())
{
QString displayName = generateLanguageName(language);
QVariant userData = QString::fromStdString(language.identifier);
widget->addItem(displayName, userData);
if(activeLanguage == language.identifier)
widget->setCurrentIndex(widget->count() - 1);
}
widget->blockSignals(false);
}
void Languages::fillLanguages(QListWidget * widget)
{
widget->blockSignals(true); // we do not want calls caused by initialization
widget->clear();
std::string activeLanguage = settings["general"]["language"].String();
for(const auto & language : Languages::getLanguageList())
{
QString displayName = generateLanguageName(language);
QVariant userData = QString::fromStdString(language.identifier);
widget->addItem(displayName);
widget->item(widget->count() - 1)->setData(Qt::UserRole, userData);
if(activeLanguage == language.identifier)
widget->setCurrentRow(widget->count() - 1);
}
widget->blockSignals(false);
}