From f093025ff37e44e50b821d5ae3b933139ac75d4b Mon Sep 17 00:00:00 2001 From: Daniel Molkentin Date: Tue, 5 Jan 2010 16:33:30 +0100 Subject: [PATCH] Make it possible to override the language settings in Creator. Platform-dependent behavior is not good enough for a lot of people it seems. Reviewed-By: con --- src/app/main.cpp | 7 +++ src/plugins/coreplugin/generalsettings.cpp | 68 ++++++++++++++++++++++ src/plugins/coreplugin/generalsettings.h | 4 ++ src/plugins/coreplugin/generalsettings.ui | 65 +++++++++++++++++---- src/plugins/coreplugin/mainwindow.cpp | 1 + 5 files changed, 134 insertions(+), 11 deletions(-) diff --git a/src/app/main.cpp b/src/app/main.cpp index f4d17c4bf43..26c7e85d5bc 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include #include @@ -179,6 +180,12 @@ int main(int argc, char **argv) QTranslator translator; QTranslator qtTranslator; QString locale = QLocale::system().name(); + // keep this in sync with the MainWindow ctor in coreplugin/mainwindow.cpp + const QSettings settings(QSettings::IniFormat, QSettings::UserScope, + QLatin1String("Nokia"), QLatin1String("QtCreator")); + locale = settings.value("General/OverrideLanguage", locale).toString(); + + const QString &creatorTrPath = QCoreApplication::applicationDirPath() + QLatin1String(SHARE_PATH "/translations"); if (translator.load(QLatin1String("qtcreator_") + locale, creatorTrPath)) { diff --git a/src/plugins/coreplugin/generalsettings.cpp b/src/plugins/coreplugin/generalsettings.cpp index 97f9dc97fa1..203fda65fff 100644 --- a/src/plugins/coreplugin/generalsettings.cpp +++ b/src/plugins/coreplugin/generalsettings.cpp @@ -38,9 +38,13 @@ #include #include +#include #include #include +#include +#include +#include #include "ui_generalsettings.h" @@ -73,6 +77,38 @@ QString GeneralSettings::trCategory() const return QCoreApplication::translate("Core", Core::Constants::SETTINGS_TR_CATEGORY_CORE); } +static bool hasQmFilesForLocale(const QString &locale, const QString &creatorTrPath) +{ + static const QString qtTrPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath); + + const QString trFile = QLatin1String("qt_") + locale + QLatin1String(".qm"); + return QFile::exists(qtTrPath+'/'+trFile) || QFile::exists(creatorTrPath+'/'+trFile); +} + +void GeneralSettings::fillLanguageBox() const +{ + m_page->languageBox->addItem(tr(""), QString()); + + const QString creatorTrPath = + Core::ICore::instance()->resourcePath() + QLatin1String("/translations"); + const QStringList languageFiles = QDir(creatorTrPath).entryList(QStringList(QLatin1String("*.qm"))); + const QString currentLocale = language(); + + Q_FOREACH(const QString languageFile, languageFiles) + { + int start = languageFile.lastIndexOf(QLatin1Char('_'))+1; + int end = languageFile.lastIndexOf(QLatin1Char('.')); + const QString locale = languageFile.mid(start, end-start); + // no need to show a language that creator will not load anyway + if (hasQmFilesForLocale(locale, creatorTrPath)) { + m_page->languageBox->addItem(QLocale::languageToString(QLocale(locale).language()), locale); + if (locale == currentLocale) + m_page->languageBox->setCurrentIndex(m_page->languageBox->count() - 1); + + } + } +} + QWidget *GeneralSettings::createPage(QWidget *parent) { m_page = new Ui::GeneralSettings(); @@ -80,6 +116,8 @@ QWidget *GeneralSettings::createPage(QWidget *parent) m_page->setupUi(w); QSettings* settings = Core::ICore::instance()->settings(); + Q_UNUSED(settings) + fillLanguageBox(); m_page->colorButton->setColor(StyleHelper::baseColor()); m_page->externalEditorEdit->setText(EditorManager::instance()->externalEditor()); m_page->reloadBehavior->setCurrentIndex(EditorManager::instance()->reloadBehavior()); @@ -103,6 +141,8 @@ QWidget *GeneralSettings::createPage(QWidget *parent) this, SLOT(resetInterfaceColor())); connect(m_page->resetEditorButton, SIGNAL(clicked()), this, SLOT(resetExternalEditor())); + connect(m_page->resetLanguageButton, SIGNAL(clicked()), + this, SLOT(resetLanguage())); connect(m_page->helpExternalEditorButton, SIGNAL(clicked()), this, SLOT(showHelpForExternalEditor())); #ifdef Q_OS_UNIX @@ -132,6 +172,8 @@ bool GeneralSettings::matches(const QString &s) const void GeneralSettings::apply() { + int currentIndex = m_page->languageBox->currentIndex(); + setLanguage(m_page->languageBox->itemData(currentIndex, Qt::UserRole).toString()); // Apply the new base color if accepted StyleHelper::setBaseColor(m_page->colorButton->color()); EditorManager::instance()->setExternalEditor(m_page->externalEditorEdit->text()); @@ -205,3 +247,29 @@ void GeneralSettings::showHelpForFileBrowser() variableHelpDialogCreator(UnixUtils::fileBrowserHelpText()); } #endif + +void GeneralSettings::resetLanguage() +{ + // system language is default + m_page->languageBox->setCurrentIndex(0); +} + +QString GeneralSettings::language() const +{ + QSettings* settings = Core::ICore::instance()->settings(); + return settings->value(QLatin1String("General/OverrideLanguage")).toString(); +} + +void GeneralSettings::setLanguage(const QString &locale) +{ + QSettings* settings = Core::ICore::instance()->settings(); + if (settings->value(QLatin1String("General/OverrideLanguage")).toString() != locale) + { + QMessageBox::information(Core::ICore::instance()->mainWindow(), tr("Restart required"), + tr("The language change will take effect after a restart of Qt Creator.")); + } + if (locale.isEmpty()) + settings->remove(QLatin1String("General/OverrideLanguage")); + else + settings->setValue(QLatin1String("General/OverrideLanguage"), locale); +} diff --git a/src/plugins/coreplugin/generalsettings.h b/src/plugins/coreplugin/generalsettings.h index fb13a88ed43..cef5ef543c2 100644 --- a/src/plugins/coreplugin/generalsettings.h +++ b/src/plugins/coreplugin/generalsettings.h @@ -60,6 +60,7 @@ public: private slots: void resetInterfaceColor(); void resetExternalEditor(); + void resetLanguage(); void showHelpForExternalEditor(); #ifdef Q_OS_UNIX # ifndef Q_OS_MAC @@ -71,6 +72,9 @@ private slots: private: void variableHelpDialogCreator(const QString& helpText); + void fillLanguageBox() const; + QString language() const; + void setLanguage(const QString&); Ui::GeneralSettings *m_page; QString m_searchKeywords; QPointer m_dialog; diff --git a/src/plugins/coreplugin/generalsettings.ui b/src/plugins/coreplugin/generalsettings.ui index fad3f5222ae..de8299a3fc9 100644 --- a/src/plugins/coreplugin/generalsettings.ui +++ b/src/plugins/coreplugin/generalsettings.ui @@ -6,8 +6,8 @@ 0 0 - 363 - 296 + 408 + 295 @@ -23,7 +23,7 @@ - User &interface color: + User interface color: colorButton @@ -80,14 +80,14 @@ - + Terminal: - + @@ -108,14 +108,14 @@ - + External editor: - + @@ -143,14 +143,14 @@ - + External file browser: - + When files are externally modified: @@ -160,7 +160,7 @@ - + @@ -188,7 +188,7 @@ - + @@ -220,6 +220,34 @@ + + + + User interface language: + + + + + + + + + + + + Reset to default + + + R + + + + :/core/images/reset.png:/core/images/reset.png + + + + + @@ -245,6 +273,21 @@
utils/qtcolorbutton.h
+ + colorButton + resetButton + languageBox + resetLanguageButton + terminalEdit + resetTerminalButton + externalEditorEdit + resetEditorButton + helpExternalEditorButton + externalFileBrowserEdit + resetFileBrowserButton + helpExternalFileBrowserButton + reloadBehavior + diff --git a/src/plugins/coreplugin/mainwindow.cpp b/src/plugins/coreplugin/mainwindow.cpp index 54c79ec5a34..cdc3898ee34 100644 --- a/src/plugins/coreplugin/mainwindow.cpp +++ b/src/plugins/coreplugin/mainwindow.cpp @@ -114,6 +114,7 @@ MainWindow::MainWindow() : m_uniqueIDManager(new UniqueIDManager()), m_globalContext(QList() << Constants::C_GLOBAL_ID), m_additionalContexts(m_globalContext), + // keep this in sync with main() in app/main.cpp m_settings(new QSettings(QSettings::IniFormat, QSettings::UserScope, QLatin1String("Nokia"), QLatin1String("QtCreator"), this)), m_settingsDatabase(new SettingsDatabase(QFileInfo(m_settings->fileName()).path(),