From 4e7a183360afe2298c04da45893685d39c7078f3 Mon Sep 17 00:00:00 2001 From: dubslow Date: Fri, 17 Oct 2014 18:06:30 -0500 Subject: [PATCH] add choose translations (restart required, dynamic changing is lots of work) --- src/main.cpp | 13 ----- src/misc/settings.cpp | 12 ++--- src/misc/settings.h | 6 +-- src/widget/form/settings/generalform.cpp | 14 ++++-- src/widget/form/settings/generalform.h | 2 +- src/widget/form/settings/generalsettings.ui | 30 ++++++++++-- src/widget/widget.cpp | 53 ++++++++++++++------- src/widget/widget.h | 5 +- 8 files changed, 85 insertions(+), 50 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 1d447dbf9..e02db84a1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,7 +18,6 @@ #include "misc/settings.h" #include #include -#include #include #include @@ -32,18 +31,6 @@ int main(int argc, char *argv[]) QCoreApplication::addLibraryPath(QCoreApplication::applicationDirPath()); a.addLibraryPath("platforms"); - // Load translations - QTranslator translator; - if (Settings::getInstance().getUseTranslations()) - { - QString locale = QLocale::system().name().section('_', 0, 0); - if (locale=="en" || translator.load(locale,":translations/")) - qDebug() << "Loaded translation "+locale; - else - qDebug() << "Error loading translation "+locale; - a.installTranslator(&translator); - } - // Install Unicode 6.1 supporting font QFontDatabase::addApplicationFont("://DejaVuSans.ttf"); diff --git a/src/misc/settings.cpp b/src/misc/settings.cpp index 037b4cbb6..773ec5457 100644 --- a/src/misc/settings.cpp +++ b/src/misc/settings.cpp @@ -108,7 +108,7 @@ void Settings::load() s.beginGroup("General"); enableIPv6 = s.value("enableIPv6", true).toBool(); - useTranslations = s.value("useTranslations", true).toBool(); + translation = s.value("translation", "").toString(); makeToxPortable = s.value("makeToxPortable", false).toBool(); autostartInTray = s.value("autostartInTray", false).toBool(); forceTCP = s.value("forceTCP", false).toBool(); @@ -216,7 +216,7 @@ void Settings::save(QString path) s.beginGroup("General"); s.setValue("enableIPv6", enableIPv6); - s.setValue("useTranslations",useTranslations); + s.setValue("translation",translation); s.setValue("makeToxPortable",makeToxPortable); s.setValue("autostartInTray",autostartInTray); s.setValue("useProxy", useProxy); @@ -389,14 +389,14 @@ void Settings::setStatusChangeNotificationEnabled(bool newValue) statusChangeNotificationEnabled = newValue; } -bool Settings::getUseTranslations() const +QString Settings::getTranslation() const { - return useTranslations; + return translation; } -void Settings::setUseTranslations(bool newValue) +void Settings::setTranslation(QString newValue) { - useTranslations = newValue; + translation = newValue; } bool Settings::getForceTCP() const diff --git a/src/misc/settings.h b/src/misc/settings.h index 79675fbd8..ee85b4177 100644 --- a/src/misc/settings.h +++ b/src/misc/settings.h @@ -58,8 +58,8 @@ public: QString getCurrentProfile() const; void setCurrentProfile(QString profile); - bool getUseTranslations() const; - void setUseTranslations(bool newValue); + QString getTranslation() const; + void setTranslation(QString newValue); bool getForceTCP() const; void setForceTCP(bool newValue); @@ -179,7 +179,7 @@ private: bool dontShowDhtDialog; bool enableIPv6; - bool useTranslations; + QString translation; static bool makeToxPortable; bool autostartInTray; diff --git a/src/widget/form/settings/generalform.cpp b/src/widget/form/settings/generalform.cpp index e5f2ed23b..c766dd5b9 100644 --- a/src/widget/form/settings/generalform.cpp +++ b/src/widget/form/settings/generalform.cpp @@ -23,6 +23,9 @@ #include #include +static QStringList locales = {"de", "en", "fr", "it", "mannol", "pirate", "pl", "ru", "fi", "uk"}; +static QStringList langs = {"Deustch", "English", "Français", "Italiano", "mannol", "Pirate", "Polski", "Русский", "Suomi", "Українська"}; + GeneralForm::GeneralForm(SettingsWidget *myParent) : GenericForm(tr("General"), QPixmap(":/img/settings/general.png")) { @@ -32,7 +35,9 @@ GeneralForm::GeneralForm(SettingsWidget *myParent) : bodyUI->setupUi(this); bodyUI->cbEnableIPv6->setChecked(Settings::getInstance().getEnableIPv6()); - bodyUI->cbUseTranslations->setChecked(Settings::getInstance().getUseTranslations()); + for (int i = 0; i < langs.size(); i++) + bodyUI->transComboBox->insertItem(i, langs[i]); + bodyUI->transComboBox->setCurrentIndex(locales.indexOf(Settings::getInstance().getTranslation())); bodyUI->cbMakeToxPortable->setChecked(Settings::getInstance().getMakeToxPortable()); bodyUI->startInTray->setChecked(Settings::getInstance().getAutostartInTray()); bodyUI->statusChangesCheckbox->setChecked(Settings::getInstance().getStatusChangeNotificationEnabled()); @@ -64,7 +69,7 @@ GeneralForm::GeneralForm(SettingsWidget *myParent) : onUseProxyUpdated(); connect(bodyUI->cbEnableIPv6, &QCheckBox::stateChanged, this, &GeneralForm::onEnableIPv6Updated); - connect(bodyUI->cbUseTranslations, &QCheckBox::stateChanged, this, &GeneralForm::onUseTranslationUpdated); + connect(bodyUI->transComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onTranslationUpdated())); connect(bodyUI->cbMakeToxPortable, &QCheckBox::stateChanged, this, &GeneralForm::onMakeToxPortableUpdated); connect(bodyUI->startInTray, &QCheckBox::stateChanged, this, &GeneralForm::onSetAutostartInTray); connect(bodyUI->statusChangesCheckbox, &QCheckBox::stateChanged, this, &GeneralForm::onSetStatusChange); @@ -88,9 +93,10 @@ void GeneralForm::onEnableIPv6Updated() Settings::getInstance().setEnableIPv6(bodyUI->cbEnableIPv6->isChecked()); } -void GeneralForm::onUseTranslationUpdated() +void GeneralForm::onTranslationUpdated() { - Settings::getInstance().setUseTranslations(bodyUI->cbUseTranslations->isChecked()); + Settings::getInstance().setTranslation(locales[bodyUI->transComboBox->currentIndex()]); + Widget::getInstance()->setTranslation(); } void GeneralForm::onMakeToxPortableUpdated() diff --git a/src/widget/form/settings/generalform.h b/src/widget/form/settings/generalform.h index 31a73eb32..bae1eb8d0 100644 --- a/src/widget/form/settings/generalform.h +++ b/src/widget/form/settings/generalform.h @@ -32,7 +32,7 @@ public: private slots: void onEnableIPv6Updated(); - void onUseTranslationUpdated(); + void onTranslationUpdated(); void onMakeToxPortableUpdated(); void onSetAutostartInTray(); void onSmileyBrowserIndexChanged(int index); diff --git a/src/widget/form/settings/generalsettings.ui b/src/widget/form/settings/generalsettings.ui index 7201a2bc6..924bc057b 100644 --- a/src/widget/form/settings/generalsettings.ui +++ b/src/widget/form/settings/generalsettings.ui @@ -30,11 +30,31 @@ - - - Use translations - - + + + + + The translation may not load until qTox restarts. + + + Translation: + + + + + + + + 0 + 0 + + + + The translation may not load until qTox restarts. + + + + diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 1f73036b4..742433f88 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -43,8 +43,10 @@ #include #include #include -#include #include +#include +#include + Widget *Widget::instance{nullptr}; @@ -53,7 +55,8 @@ Widget::Widget(QWidget *parent) ui(new Ui::MainWindow), activeChatroomWidget{nullptr} { - + translator = new QTranslator; + setTranslation(); } void Widget::init() @@ -312,7 +315,7 @@ QString Widget::getUsername() void Widget::onAvatarClicked() { QString filename = QFileDialog::getOpenFileName(this, tr("Choose a profile picture"), QDir::homePath()); - if (filename == "") + if (filename.isEmpty()) return; QFile file(filename); file.open(QIODevice::ReadOnly); @@ -546,22 +549,24 @@ void Widget::onFriendStatusChanged(int friendId, Status status) f->friendStatus = status; f->widget->updateStatusLight(); - QString fStatus = ""; - switch(f->friendStatus){ - case Status::Away: - fStatus = tr("away", "contact status"); break; - case Status::Busy: - fStatus = tr("busy", "contact status"); break; - case Status::Offline: - fStatus = tr("offline", "contact status"); break; - default: - fStatus = tr("online", "contact status"); break; - } - - //won't print the message if there were no messages before + //won't print the message if there were no messages before if(f->chatForm->getNumberOfMessages() != 0 && Settings::getInstance().getStatusChangeNotificationEnabled() == true) - f->chatForm->addSystemInfoMessage(tr("%1 is now %2", "e.g. \"Dubslow is now online\"").arg(f->getName()).arg(fStatus), "white"); + { + QString fStatus = ""; + switch(f->friendStatus){ + case Status::Away: + fStatus = tr("away", "contact status"); break; + case Status::Busy: + fStatus = tr("busy", "contact status"); break; + case Status::Offline: + fStatus = tr("offline", "contact status"); break; + default: + fStatus = tr("online", "contact status"); break; + } + f->chatForm->addSystemInfoMessage(tr("%1 is now %2", "e.g. \"Dubslow is now online\"").arg(f->getName()).arg(fStatus), + "white", QDateTime::currentDateTime()); + } } void Widget::onFriendStatusMessageChanged(int friendId, const QString& message) @@ -902,3 +907,17 @@ void Widget::onGroupSendResult(int groupId, const QString& message, int result) if (result == -1) g->chatForm->addSystemInfoMessage("Message failed to send", "red"); } + +void Widget::setTranslation() +{ + // Load translations + QCoreApplication::removeTranslator(translator); + QString locale; + if ((locale = Settings::getInstance().getTranslation()) == "") + locale = QLocale::system().name().section('_', 0, 0); + if (translator->load(locale,":translations/")) + qDebug() << "Loaded translation" << locale; + else + qDebug() << "Error loading translation" << locale; + QCoreApplication::installTranslator(translator); +} diff --git a/src/widget/widget.h b/src/widget/widget.h index 2166d6613..6e1f1a8d4 100644 --- a/src/widget/widget.h +++ b/src/widget/widget.h @@ -41,6 +41,7 @@ class Camera; class FriendListWidget; class MaskablePixmapWidget; class QTimer; +class QTranslator; class Widget : public QMainWindow { @@ -59,7 +60,8 @@ public: bool getIsWindowMinimized(); static QList searchProfiles(); void clearContactsList(); - void setIdleTimer(int minutes); + void setIdleTimer(int minutes); + void setTranslation(); ~Widget(); virtual void closeEvent(QCloseEvent *event); @@ -138,6 +140,7 @@ private: bool notify(QObject *receiver, QEvent *event); bool autoAwayActive = false; QTimer* idleTimer; + QTranslator* translator; }; #endif // WIDGET_H