Skip to content

Commit d6b887d

Browse files
committed
Merge pull request sqlitebrowser#187 from samir-aguiar/master
Add language combobox to the Preferences menu
2 parents 823e51a + ce8f49d commit d6b887d

File tree

7 files changed

+185
-75
lines changed

7 files changed

+185
-75
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ set(SQLB_MISC
109109

110110
# Translation files
111111
set(SQLB_TSS
112-
"${CMAKE_SOURCE_DIR}/src/translations/sqlb_cn.ts"
112+
"${CMAKE_SOURCE_DIR}/src/translations/sqlb_zh.ts"
113113
"${CMAKE_SOURCE_DIR}/src/translations/sqlb_de.ts"
114114
"${CMAKE_SOURCE_DIR}/src/translations/sqlb_fr.ts"
115115
"${CMAKE_SOURCE_DIR}/src/translations/sqlb_ru.ts"

src/Application.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "Application.h"
1010
#include "MainWindow.h"
11+
#include "PreferencesDialog.h"
1112

1213
Application::Application(int& argc, char** argv) :
1314
QApplication(argc, argv)
@@ -25,12 +26,15 @@ Application::Application(int& argc, char** argv) :
2526

2627
// Load translations
2728
bool ok;
28-
QString name = QLocale::system().name();
29+
QString name = PreferencesDialog::getSettingsValue("General", "language").toString();
2930

3031
// First of all try to load the application translation file.
3132
m_translatorApp = new QTranslator(this);
32-
ok = m_translatorApp->load("sqlb_" + name, "translations");
33+
ok = m_translatorApp->load("sqlb_" + name,
34+
QCoreApplication::applicationDirPath() + "/translations");
35+
3336
if (ok == true) {
37+
PreferencesDialog::setSettingsValue("General", "language", name);
3438
installTranslator(m_translatorApp);
3539

3640
// The application translation file has been found and loaded.
@@ -47,6 +51,12 @@ Application::Application(int& argc, char** argv) :
4751
if (ok == true)
4852
installTranslator(m_translatorQt);
4953
}
54+
else {
55+
// Set the correct locale so that the program won't erroneously detect
56+
// a language change when the user toggles settings for the first time.
57+
// (it also prevents the program from always looking for a translation on launch)
58+
PreferencesDialog::setSettingsValue("General", "language", "en_US");
59+
}
5060

5161
// Parse command line
5262
QString fileToOpen;

src/PreferencesDialog.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <QSettings>
77
#include <QFileDialog>
88
#include <QColorDialog>
9+
#include <QMessageBox>
10+
#include <QSortFilterProxyModel>
911

1012
QHash<QString, QVariant> PreferencesDialog::m_hCache;
1113

@@ -16,6 +18,15 @@ PreferencesDialog::PreferencesDialog(QWidget* parent)
1618
ui->setupUi(this);
1719
ui->treeSyntaxHighlighting->setColumnHidden(0, true);
1820

21+
// Model to sort the languages in the language combo box
22+
QSortFilterProxyModel *proxy = new QSortFilterProxyModel(ui->languageComboBox);
23+
proxy->setSourceModel(ui->languageComboBox->model());
24+
25+
// Prevent setModel() from deleting the current model (now source of the proxy model)
26+
ui->languageComboBox->model()->setParent(proxy);
27+
28+
ui->languageComboBox->setModel(proxy);
29+
1930
loadSettings();
2031
}
2132

@@ -63,6 +74,7 @@ void PreferencesDialog::loadSettings()
6374
ui->spinLogFontSize->setValue(getSettingsValue("log", "fontsize").toInt());
6475

6576
ui->listExtensions->addItems(getSettingsValue("extensions", "list").toStringList());
77+
fillLanguageBox();
6678
}
6779

6880
void PreferencesDialog::saveSettings()
@@ -89,6 +101,14 @@ void PreferencesDialog::saveSettings()
89101
extList.append(item->text());
90102
setSettingsValue("extensions", "list", extList);
91103

104+
// Warn about restarting to change language
105+
QVariant newLanguage = ui->languageComboBox->itemData(ui->languageComboBox->currentIndex());
106+
if (newLanguage != getSettingsValue("General", "language"))
107+
QMessageBox::information(this, QApplication::applicationName(),
108+
tr("The language will change after you restart the application."));
109+
110+
setSettingsValue("General", "language", newLanguage);
111+
92112
accept();
93113
}
94114

@@ -160,6 +180,10 @@ QVariant PreferencesDialog::getSettingsDefaultValue(const QString& group, const
160180
if(group == "General" && name == "recentFileList")
161181
return QStringList();
162182

183+
// General/language?
184+
if(group == "General" && name == "language")
185+
return QLocale::system().name();
186+
163187
// syntaxhighlighter?
164188
if(group == "syntaxhighlighter")
165189
{
@@ -238,3 +262,39 @@ void PreferencesDialog::removeExtension()
238262
if(ui->listExtensions->currentIndex().isValid())
239263
ui->listExtensions->takeItem(ui->listExtensions->currentIndex().row());
240264
}
265+
266+
void PreferencesDialog::fillLanguageBox()
267+
{
268+
// Use the path relative to the main executable
269+
QDir translationsDir(QCoreApplication::applicationDirPath() + "/translations",
270+
"sqlb_*.qm");
271+
272+
// Add default language
273+
ui->languageComboBox->addItem("English (United States)", "en_US");
274+
275+
foreach(const QFileInfo &file, translationsDir.entryInfoList())
276+
{
277+
QLocale locale(file.baseName().remove("sqlb_"));
278+
279+
// Prevent invalid locales from being added to the box
280+
if(locale.name() == "C")
281+
continue;
282+
283+
QString language = QLocale::languageToString(locale.language()) + " (" +
284+
QLocale::countryToString(locale.country()) + ")";
285+
286+
ui->languageComboBox->addItem(language, locale.name());
287+
}
288+
289+
ui->languageComboBox->model()->sort(0);
290+
291+
// Try to select the language for the stored locale
292+
int index = ui->languageComboBox->findData(getSettingsValue("General", "language"),
293+
Qt::UserRole, Qt::MatchExactly);
294+
295+
// If there's no translation for the current locale, default to English
296+
if(index < 0)
297+
index = ui->languageComboBox->findData("en_US", Qt::UserRole, Qt::MatchExactly);
298+
299+
ui->languageComboBox->setCurrentIndex(index);
300+
}

src/PreferencesDialog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ private slots:
3838
// This works similar to getSettingsValue but returns the default value instead of the value set by the user
3939
static QVariant getSettingsDefaultValue(const QString& group, const QString& name);
4040

41+
void fillLanguageBox();
42+
4143
// Cache for storing the settings to avoid repeatedly reading the settings file all the time
4244
static QHash<QString, QVariant> m_hCache;
4345
};

0 commit comments

Comments
 (0)