6
6
#include < QSettings>
7
7
#include < QFileDialog>
8
8
#include < QColorDialog>
9
+ #include < QMessageBox>
10
+ #include < QSortFilterProxyModel>
9
11
10
12
QHash<QString, QVariant> PreferencesDialog::m_hCache;
11
13
@@ -16,6 +18,15 @@ PreferencesDialog::PreferencesDialog(QWidget* parent)
16
18
ui->setupUi (this );
17
19
ui->treeSyntaxHighlighting ->setColumnHidden (0 , true );
18
20
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
+
19
30
loadSettings ();
20
31
}
21
32
@@ -63,6 +74,7 @@ void PreferencesDialog::loadSettings()
63
74
ui->spinLogFontSize ->setValue (getSettingsValue (" log" , " fontsize" ).toInt ());
64
75
65
76
ui->listExtensions ->addItems (getSettingsValue (" extensions" , " list" ).toStringList ());
77
+ fillLanguageBox ();
66
78
}
67
79
68
80
void PreferencesDialog::saveSettings ()
@@ -89,6 +101,14 @@ void PreferencesDialog::saveSettings()
89
101
extList.append (item->text ());
90
102
setSettingsValue (" extensions" , " list" , extList);
91
103
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
+
92
112
accept ();
93
113
}
94
114
@@ -160,6 +180,10 @@ QVariant PreferencesDialog::getSettingsDefaultValue(const QString& group, const
160
180
if (group == " General" && name == " recentFileList" )
161
181
return QStringList ();
162
182
183
+ // General/language?
184
+ if (group == " General" && name == " language" )
185
+ return QLocale::system ().name ();
186
+
163
187
// syntaxhighlighter?
164
188
if (group == " syntaxhighlighter" )
165
189
{
@@ -238,3 +262,39 @@ void PreferencesDialog::removeExtension()
238
262
if (ui->listExtensions ->currentIndex ().isValid ())
239
263
ui->listExtensions ->takeItem (ui->listExtensions ->currentIndex ().row ());
240
264
}
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
+ }
0 commit comments