Merge pull request #1345 from Trial97/time2

feat:Added option to use system locale
This commit is contained in:
Tayou 2023-07-13 01:56:12 +02:00 committed by GitHub
commit 94d4d12ee0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 58 deletions

View File

@ -568,6 +568,7 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv)
// Language // Language
m_settings->registerSetting("Language", QString()); m_settings->registerSetting("Language", QString());
m_settings->registerSetting("UseSystemLocale", false);
// Console // Console
m_settings->registerSetting("ShowConsole", false); m_settings->registerSetting("ShowConsole", false);
@ -918,12 +919,7 @@ bool Application::createSetupWizard()
} }
return false; return false;
}(); }();
bool languageRequired = [&]() bool languageRequired = settings()->get("Language").toString().isEmpty();
{
if (settings()->get("Language").toString().isEmpty())
return true;
return false;
}();
bool pasteInterventionRequired = settings()->get("PastebinURL") != ""; bool pasteInterventionRequired = settings()->get("PastebinURL") != "";
bool themeInterventionRequired = settings()->get("ApplicationTheme") == ""; bool themeInterventionRequired = settings()->get("ApplicationTheme") == "";
bool wizardRequired = javaRequired || languageRequired || pasteInterventionRequired || themeInterventionRequired; bool wizardRequired = javaRequired || languageRequired || pasteInterventionRequired || themeInterventionRequired;

View File

@ -42,6 +42,7 @@
#include <QDir> #include <QDir>
#include <QLibraryInfo> #include <QLibraryInfo>
#include <QDebug> #include <QDebug>
#include <locale>
#include "FileSystem.h" #include "FileSystem.h"
#include "net/NetJob.h" #include "net/NetJob.h"
@ -527,34 +528,34 @@ Language * TranslationsModel::findLanguage(const QString& key)
} }
} }
void TranslationsModel::setUseSystemLocale(bool useSystemLocale)
{
APPLICATION->settings()->set("UseSystemLocale", useSystemLocale);
QLocale::setDefault(QLocale(useSystemLocale ? QString::fromStdString(std::locale().name()) : defaultLangCode));
}
bool TranslationsModel::selectLanguage(QString key) bool TranslationsModel::selectLanguage(QString key)
{ {
QString &langCode = key; QString& langCode = key;
auto langPtr = findLanguage(key); auto langPtr = findLanguage(key);
if (langCode.isEmpty()) if (langCode.isEmpty()) {
{
d->no_language_set = true; d->no_language_set = true;
} }
if(!langPtr) if (!langPtr) {
{
qWarning() << "Selected invalid language" << key << ", defaulting to" << defaultLangCode; qWarning() << "Selected invalid language" << key << ", defaulting to" << defaultLangCode;
langCode = defaultLangCode; langCode = defaultLangCode;
} } else {
else
{
langCode = langPtr->key; langCode = langPtr->key;
} }
// uninstall existing translators if there are any // uninstall existing translators if there are any
if (d->m_app_translator) if (d->m_app_translator) {
{
QCoreApplication::removeTranslator(d->m_app_translator.get()); QCoreApplication::removeTranslator(d->m_app_translator.get());
d->m_app_translator.reset(); d->m_app_translator.reset();
} }
if (d->m_qt_translator) if (d->m_qt_translator) {
{
QCoreApplication::removeTranslator(d->m_qt_translator.get()); QCoreApplication::removeTranslator(d->m_qt_translator.get());
d->m_qt_translator.reset(); d->m_qt_translator.reset();
} }
@ -564,8 +565,9 @@ bool TranslationsModel::selectLanguage(QString key)
* In a multithreaded application, the default locale should be set at application startup, before any non-GUI threads are created. * In a multithreaded application, the default locale should be set at application startup, before any non-GUI threads are created.
* This function is not reentrant. * This function is not reentrant.
*/ */
QLocale locale = QLocale(langCode); QLocale::setDefault(
QLocale::setDefault(locale); QLocale(APPLICATION->settings()->get("UseSystemLocale").toBool() ? QString::fromStdString(std::locale().name()) : langCode));
// if it's the default UI language, finish // if it's the default UI language, finish
if(langCode == defaultLangCode) if(langCode == defaultLangCode)

View File

@ -20,17 +20,16 @@
struct Language; struct Language;
class TranslationsModel : public QAbstractListModel class TranslationsModel : public QAbstractListModel {
{
Q_OBJECT Q_OBJECT
public: public:
explicit TranslationsModel(QString path, QObject *parent = 0); explicit TranslationsModel(QString path, QObject* parent = 0);
virtual ~TranslationsModel(); virtual ~TranslationsModel();
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override; QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex & parent) const override; int columnCount(const QModelIndex& parent) const override;
bool selectLanguage(QString key); bool selectLanguage(QString key);
void updateLanguage(QString key); void updateLanguage(QString key);
@ -38,27 +37,27 @@ public:
QString selectedLanguage(); QString selectedLanguage();
void downloadIndex(); void downloadIndex();
void setUseSystemLocale(bool useSystemLocale);
private: private:
Language *findLanguage(const QString & key); Language* findLanguage(const QString& key);
void reloadLocalFiles(); void reloadLocalFiles();
void downloadTranslation(QString key); void downloadTranslation(QString key);
void downloadNext(); void downloadNext();
// hide copy constructor // hide copy constructor
TranslationsModel(const TranslationsModel &) = delete; TranslationsModel(const TranslationsModel&) = delete;
// hide assign op // hide assign op
TranslationsModel &operator=(const TranslationsModel &) = delete; TranslationsModel& operator=(const TranslationsModel&) = delete;
private slots: private slots:
void indexReceived(); void indexReceived();
void indexFailed(QString reason); void indexFailed(QString reason);
void dlFailed(QString reason); void dlFailed(QString reason);
void dlGood(); void dlGood();
void translationDirChanged(const QString &path); void translationDirChanged(const QString& path);
private: /* data */
private: /* data */
struct Private; struct Private;
std::unique_ptr<Private> d; std::unique_ptr<Private> d;
}; };

View File

@ -1,16 +1,16 @@
#include "LanguageSelectionWidget.h" #include "LanguageSelectionWidget.h"
#include <QVBoxLayout> #include <QCheckBox>
#include <QTreeView>
#include <QHeaderView> #include <QHeaderView>
#include <QLabel> #include <QLabel>
#include <QTreeView>
#include <QVBoxLayout>
#include "Application.h" #include "Application.h"
#include "BuildConfig.h" #include "BuildConfig.h"
#include "translations/TranslationsModel.h"
#include "settings/Setting.h" #include "settings/Setting.h"
#include "translations/TranslationsModel.h"
LanguageSelectionWidget::LanguageSelectionWidget(QWidget *parent) : LanguageSelectionWidget::LanguageSelectionWidget(QWidget* parent) : QWidget(parent)
QWidget(parent)
{ {
verticalLayout = new QVBoxLayout(this); verticalLayout = new QVBoxLayout(this);
verticalLayout->setObjectName(QStringLiteral("verticalLayout")); verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
@ -31,6 +31,13 @@ LanguageSelectionWidget::LanguageSelectionWidget(QWidget *parent) :
helpUsLabel->setWordWrap(true); helpUsLabel->setWordWrap(true);
verticalLayout->addWidget(helpUsLabel); verticalLayout->addWidget(helpUsLabel);
formatCheckbox = new QCheckBox(this);
formatCheckbox->setObjectName(QStringLiteral("formatCheckbox"));
formatCheckbox->setCheckState(APPLICATION->settings()->get("UseSystemLocale").toBool() ? Qt::Checked : Qt::Unchecked);
connect(formatCheckbox, &QCheckBox::stateChanged,
[this]() { APPLICATION->translations()->setUseSystemLocale(formatCheckbox->isChecked()); });
verticalLayout->addWidget(formatCheckbox);
auto translations = APPLICATION->translations(); auto translations = APPLICATION->translations();
auto index = translations->selectedIndex(); auto index = translations->selectedIndex();
languageView->setModel(translations.get()); languageView->setModel(translations.get());
@ -38,7 +45,7 @@ LanguageSelectionWidget::LanguageSelectionWidget(QWidget *parent) :
languageView->header()->setSectionResizeMode(QHeaderView::ResizeToContents); languageView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
languageView->header()->setSectionResizeMode(0, QHeaderView::Stretch); languageView->header()->setSectionResizeMode(0, QHeaderView::Stretch);
connect(languageView->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &LanguageSelectionWidget::languageRowChanged); connect(languageView->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &LanguageSelectionWidget::languageRowChanged);
verticalLayout->setContentsMargins(0,0,0,0); verticalLayout->setContentsMargins(0, 0, 0, 0);
auto language_setting = APPLICATION->settings()->getSetting("Language"); auto language_setting = APPLICATION->settings()->getSetting("Language");
connect(language_setting.get(), &Setting::SettingChanged, this, &LanguageSelectionWidget::languageSettingChanged); connect(language_setting.get(), &Setting::SettingChanged, this, &LanguageSelectionWidget::languageSettingChanged);
@ -53,15 +60,14 @@ QString LanguageSelectionWidget::getSelectedLanguageKey() const
void LanguageSelectionWidget::retranslate() void LanguageSelectionWidget::retranslate()
{ {
QString text = tr("Don't see your language or the quality is poor?<br/><a href=\"%1\">Help us with translations!</a>") QString text = tr("Don't see your language or the quality is poor?<br/><a href=\"%1\">Help us with translations!</a>")
.arg(BuildConfig.TRANSLATIONS_URL); .arg(BuildConfig.TRANSLATIONS_URL);
helpUsLabel->setText(text); helpUsLabel->setText(text);
formatCheckbox->setText(tr("Use system locales"));
} }
void LanguageSelectionWidget::languageRowChanged(const QModelIndex& current, const QModelIndex& previous) void LanguageSelectionWidget::languageRowChanged(const QModelIndex& current, const QModelIndex& previous)
{ {
if (current == previous) if (current == previous) {
{
return; return;
} }
auto translations = APPLICATION->translations(); auto translations = APPLICATION->translations();
@ -70,7 +76,7 @@ void LanguageSelectionWidget::languageRowChanged(const QModelIndex& current, con
translations->updateLanguage(key); translations->updateLanguage(key);
} }
void LanguageSelectionWidget::languageSettingChanged(const Setting &, const QVariant) void LanguageSelectionWidget::languageSettingChanged(const Setting&, const QVariant)
{ {
auto translations = APPLICATION->translations(); auto translations = APPLICATION->translations();
auto index = translations->selectedIndex(); auto index = translations->selectedIndex();

View File

@ -21,23 +21,24 @@ class QVBoxLayout;
class QTreeView; class QTreeView;
class QLabel; class QLabel;
class Setting; class Setting;
class QCheckBox;
class LanguageSelectionWidget: public QWidget class LanguageSelectionWidget : public QWidget {
{
Q_OBJECT Q_OBJECT
public: public:
explicit LanguageSelectionWidget(QWidget *parent = 0); explicit LanguageSelectionWidget(QWidget* parent = 0);
virtual ~LanguageSelectionWidget() { }; virtual ~LanguageSelectionWidget(){};
QString getSelectedLanguageKey() const; QString getSelectedLanguageKey() const;
void retranslate(); void retranslate();
protected slots: protected slots:
void languageRowChanged(const QModelIndex &current, const QModelIndex &previous); void languageRowChanged(const QModelIndex& current, const QModelIndex& previous);
void languageSettingChanged(const Setting &, const QVariant); void languageSettingChanged(const Setting&, const QVariant);
private: private:
QVBoxLayout *verticalLayout = nullptr; QVBoxLayout* verticalLayout = nullptr;
QTreeView *languageView = nullptr; QTreeView* languageView = nullptr;
QLabel *helpUsLabel = nullptr; QLabel* helpUsLabel = nullptr;
QCheckBox* formatCheckbox = nullptr;
}; };