preferencesdialog: New combobox to change application language

The behavior remains basically as before: when first launched
the application will try to load a translation for the user's
locale, and if one cannot be found it defaults to English.
The difference is that now this is remembered so that upon
further launches the program will go straight to the matching
locale.

See issue #182.
This commit is contained in:
Samir Aguiar
2015-01-18 01:57:42 +01:00
parent 823e51a917
commit 6d6544043d
4 changed files with 169 additions and 62 deletions

View File

@@ -8,6 +8,7 @@
#include "Application.h"
#include "MainWindow.h"
#include "PreferencesDialog.h"
Application::Application(int& argc, char** argv) :
QApplication(argc, argv)
@@ -25,12 +26,13 @@ Application::Application(int& argc, char** argv) :
// Load translations
bool ok;
QString name = QLocale::system().name();
QString name = PreferencesDialog::getSettingsValue("General", "language").toString();
// First of all try to load the application translation file.
m_translatorApp = new QTranslator(this);
ok = m_translatorApp->load("sqlb_" + name, "translations");
if (ok == true) {
PreferencesDialog::setSettingsValue("General", "language", name);
installTranslator(m_translatorApp);
// The application translation file has been found and loaded.
@@ -47,6 +49,12 @@ Application::Application(int& argc, char** argv) :
if (ok == true)
installTranslator(m_translatorQt);
}
else {
// Set the correct locale so that the program won't erroneously detect
// a language change when the user toggles settings for the first time.
// (it also prevents the program from always looking for a translation on launch)
PreferencesDialog::setSettingsValue("General", "language", "en_US");
}
// Parse command line
QString fileToOpen;

View File

@@ -6,6 +6,8 @@
#include <QSettings>
#include <QFileDialog>
#include <QColorDialog>
#include <QMessageBox>
#include <QSortFilterProxyModel>
QHash<QString, QVariant> PreferencesDialog::m_hCache;
@@ -16,6 +18,15 @@ PreferencesDialog::PreferencesDialog(QWidget* parent)
ui->setupUi(this);
ui->treeSyntaxHighlighting->setColumnHidden(0, true);
// Model to sort the languages in the language combo box
QSortFilterProxyModel *proxy = new QSortFilterProxyModel(ui->languageComboBox);
proxy->setSourceModel(ui->languageComboBox->model());
// Prevent setModel() from deleting the current model (now source of the proxy model)
ui->languageComboBox->model()->setParent(proxy);
ui->languageComboBox->setModel(proxy);
loadSettings();
}
@@ -63,6 +74,7 @@ void PreferencesDialog::loadSettings()
ui->spinLogFontSize->setValue(getSettingsValue("log", "fontsize").toInt());
ui->listExtensions->addItems(getSettingsValue("extensions", "list").toStringList());
fillLanguageBox();
}
void PreferencesDialog::saveSettings()
@@ -89,6 +101,14 @@ void PreferencesDialog::saveSettings()
extList.append(item->text());
setSettingsValue("extensions", "list", extList);
// Warn about restarting to change language
QVariant newLanguage = ui->languageComboBox->itemData(ui->languageComboBox->currentIndex());
if (newLanguage != getSettingsValue("General", "language"))
QMessageBox::information(this, QApplication::applicationName(),
tr("The language will change after you restart the application."));
setSettingsValue("General", "language", newLanguage);
accept();
}
@@ -160,6 +180,10 @@ QVariant PreferencesDialog::getSettingsDefaultValue(const QString& group, const
if(group == "General" && name == "recentFileList")
return QStringList();
// General/language?
if(group == "General" && name == "language")
return QLocale::system().name();
// syntaxhighlighter?
if(group == "syntaxhighlighter")
{
@@ -238,3 +262,37 @@ void PreferencesDialog::removeExtension()
if(ui->listExtensions->currentIndex().isValid())
ui->listExtensions->takeItem(ui->listExtensions->currentIndex().row());
}
void PreferencesDialog::fillLanguageBox()
{
QDir translationsDir("translations", "sqlb_*.qm");
// Add default language
ui->languageComboBox->addItem("English (United States)", "en_US");
foreach(const QFileInfo &file, translationsDir.entryInfoList())
{
QLocale locale(file.baseName().remove("sqlb_"));
// Prevent invalid locales from being added to the box
if(locale.name() == "C")
continue;
QString language = QLocale::languageToString(locale.language()) + " (" +
QLocale::countryToString(locale.country()) + ")";
ui->languageComboBox->addItem(language, locale.name());
}
ui->languageComboBox->model()->sort(0);
// Try to select the language for the stored locale
int index = ui->languageComboBox->findData(getSettingsValue("General", "language"),
Qt::UserRole, Qt::MatchExactly);
// If there's no translation for the current locale, default to English
if(index < 0)
index = ui->languageComboBox->findData("en_US", Qt::UserRole, Qt::MatchExactly);
ui->languageComboBox->setCurrentIndex(index);
}

View File

@@ -38,6 +38,8 @@ private:
// This works similar to getSettingsValue but returns the default value instead of the value set by the user
static QVariant getSettingsDefaultValue(const QString& group, const QString& name);
void fillLanguageBox();
// Cache for storing the settings to avoid repeatedly reading the settings file all the time
static QHash<QString, QVariant> m_hCache;
};

View File

@@ -22,6 +22,46 @@
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab_4">
<attribute name="title">
<string>General</string>
</attribute>
<widget class="QLabel" name="label_9">
<property name="geometry">
<rect>
<x>140</x>
<y>10</y>
<width>61</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>Language</string>
</property>
</widget>
<widget class="QComboBox" name="languageComboBox">
<property name="geometry">
<rect>
<x>210</x>
<y>10</y>
<width>211</width>
<height>26</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToMinimumContentsLength</enum>
</property>
<property name="minimumContentsLength">
<number>20</number>
</property>
</widget>
</widget>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>&amp;Database</string>
@@ -74,33 +114,18 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<item row="3" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Default &amp;location</string>
<string>Remove line breaks in schema view</string>
</property>
<property name="buddy">
<cstring>locationEdit</cstring>
<cstring>checkHideSchemaLinebreaks</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="locationEdit">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="setLocationButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
<item row="3" column="1">
<widget class="QCheckBox" name="checkHideSchemaLinebreaks"/>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_2">
@@ -122,19 +147,34 @@
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_8">
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="locationEdit">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="setLocationButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Remove line breaks in schema view</string>
<string>Default &amp;location</string>
</property>
<property name="buddy">
<cstring>checkHideSchemaLinebreaks</cstring>
<cstring>locationEdit</cstring>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QCheckBox" name="checkHideSchemaLinebreaks"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
@@ -420,7 +460,6 @@
</layout>
</widget>
<tabstops>
<tabstop>tabWidget</tabstop>
<tabstop>encodingComboBox</tabstop>
<tabstop>foreignKeysCheckBox</tabstop>
<tabstop>locationEdit</tabstop>
@@ -470,38 +509,6 @@
</hint>
</hints>
</connection>
<connection>
<sender>setLocationButton</sender>
<signal>clicked()</signal>
<receiver>PreferencesDialog</receiver>
<slot>chooseLocation()</slot>
<hints>
<hint type="sourcelabel">
<x>568</x>
<y>119</y>
</hint>
<hint type="destinationlabel">
<x>459</x>
<y>62</y>
</hint>
</hints>
</connection>
<connection>
<sender>treeSyntaxHighlighting</sender>
<signal>itemDoubleClicked(QTreeWidgetItem*,int)</signal>
<receiver>PreferencesDialog</receiver>
<slot>showColourDialog(QTreeWidgetItem*,int)</slot>
<hints>
<hint type="sourcelabel">
<x>103</x>
<y>53</y>
</hint>
<hint type="destinationlabel">
<x>395</x>
<y>-1</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonAddExtension</sender>
<signal>clicked()</signal>
@@ -534,6 +541,38 @@
</hint>
</hints>
</connection>
<connection>
<sender>treeSyntaxHighlighting</sender>
<signal>itemDoubleClicked(QTreeWidgetItem*,int)</signal>
<receiver>PreferencesDialog</receiver>
<slot>showColourDialog(QTreeWidgetItem*,int)</slot>
<hints>
<hint type="sourcelabel">
<x>103</x>
<y>53</y>
</hint>
<hint type="destinationlabel">
<x>395</x>
<y>-1</y>
</hint>
</hints>
</connection>
<connection>
<sender>setLocationButton</sender>
<signal>clicked()</signal>
<receiver>PreferencesDialog</receiver>
<slot>chooseLocation()</slot>
<hints>
<hint type="sourcelabel">
<x>568</x>
<y>119</y>
</hint>
<hint type="destinationlabel">
<x>459</x>
<y>62</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>saveSettings()</slot>