Skip to content

Commit 4ddc47d

Browse files
committed
Rebase with main
1 parent 20ad0b9 commit 4ddc47d

File tree

6 files changed

+75
-41
lines changed

6 files changed

+75
-41
lines changed

src/kiwixapp.cpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,7 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
3535
m_server(m_library.getKiwixLibrary(), mp_nameMapper),
3636
mp_session(nullptr)
3737
{
38-
try {
39-
m_translation.setTranslation(QLocale());
40-
} catch (std::exception& e) {
41-
QMessageBox::critical(nullptr, "Translation error", e.what());
42-
return;
43-
}
44-
45-
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
46-
QString path = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
47-
#else
48-
QString path = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
49-
#endif
50-
loadAndInstallTranslations(m_qtTranslator, "qt", path);
51-
loadAndInstallTranslations(m_appTranslator, "kiwix-desktop", ":/i18n/");
52-
38+
setAppLanguage();
5339
QFontDatabase::addApplicationFont(":/fonts/Selawik/selawkb.ttf");
5440
QFontDatabase::addApplicationFont(":/fonts/Selawik/selawkl.ttf");
5541
QFontDatabase::addApplicationFont(":/fonts/Selawik/selawksb.ttf");
@@ -245,6 +231,28 @@ void KiwixApp::openZimFile(const QString &zimfile)
245231
openUrl(QUrl("zim://"+zimId+".zim/"));
246232
}
247233

234+
void KiwixApp::setAppLanguage()
235+
{
236+
const QList<QString> languageCodes = getSettingsManager()->getLanguageCodes();
237+
const QString code = languageCodes.at(getSettingsManager()->getAppLanguageIndex());
238+
QLocale appLanguage(code);
239+
try {
240+
m_translation.setTranslation(appLanguage);
241+
} catch (std::exception& e) {
242+
QMessageBox::critical(nullptr, "Translation error", e.what());
243+
return;
244+
}
245+
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
246+
QString qtTranslatorSuffix = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
247+
#else
248+
QString qtTranslatorSuffix = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
249+
#endif
250+
m_qtTranslator.load(appLanguage, "qt", "_", qtTranslatorSuffix);
251+
installTranslator(&m_qtTranslator);
252+
m_appTranslator.load(appLanguage, "kiwix-desktop", "_", ":/i18n/");
253+
installTranslator(&m_appTranslator);
254+
}
255+
248256
void KiwixApp::printPage()
249257
{
250258
if(!getTabWidget()->currentZimView())

src/kiwixapp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class KiwixApp : public QtSingleApplication
9292
public slots:
9393
void newTab();
9494
void openZimFile(const QString& zimfile="");
95+
void setAppLanguage();
9596
void openUrl(const QString& url, bool newTab=true);
9697
void openUrl(const QUrl& url, bool newTab=true);
9798
void printPage();

src/settingsmanager.cpp

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ SettingsView* SettingsManager::getView()
1919
{
2020
if (m_view == nullptr) {
2121
auto view = new SettingsView();
22-
view->init(m_zoomFactor * 100, m_downloadDir, m_monitorDir,
23-
m_moveToTrash, m_reopenTab);
22+
view->init(m_zoomFactor * 100, m_downloadDir, m_monitorDir, m_appLangIndex, m_moveToTrash, m_reopenTab);
2423
connect(view, &QObject::destroyed, this, [=]() { m_view = nullptr; });
2524
m_view = view;
2625
}
@@ -127,13 +126,20 @@ SettingsManager::FilterList SettingsManager::deducePair(QList<QVariant> variantL
127126
return pairList;
128127
}
129128

130-
void SettingsManager::setLanguage(FilterList langList)
129+
void SettingsManager::setLanguage(FilterList langList) // This regards the search filter languages
131130
{
132131
m_langList = flattenPair(langList);
133132
setSettings("language", m_langList);
134133
emit(languageChanged(m_langList));
135134
}
136135

136+
void SettingsManager::setAppLanguage(int languageIndex) // This regards the application language
137+
{
138+
m_appLangIndex = languageIndex;
139+
KiwixApp::instance()->setAppLanguage();
140+
m_settings.setValue("app/languageIndex", languageIndex);
141+
}
142+
137143
void SettingsManager::setCategory(FilterList categoryList)
138144
{
139145
m_categoryList = flattenPair(categoryList);
@@ -157,8 +163,30 @@ void SettingsManager::initSettings()
157163
m_monitorDir = m_settings.value("monitor/dir", QString("")).toString();
158164
m_moveToTrash = m_settings.value("moveToTrash", true).toBool();
159165
m_reopenTab = m_settings.value("reopenTab", false).toBool();
166+
// This regards the application language
167+
m_appLangCodes = {
168+
"ar", "bcl", "bn", "br", "ca", "cs", "da", "dag", "de", "diq",
169+
"en", "eo", "es", "fa", "fi", "fr", "gsw", "ha", "he", "hi",
170+
"hy", "ia", "id", "ie", "ig", "igl", "io", "it", "ja", "ko",
171+
"ku-latn", "lb", "mk", "ms", "my", "nb", "nl", "nqo", "or",
172+
"pl", "pt-br", "pt", "qqq", "ro", "roa-tara", "ru", "sc",
173+
"scn", "sdc", "se", "sk", "skr-arab", "sl", "sms", "sq",
174+
"sr-ec", "sro", "sv", "sw", "ta", "te", "th", "tn", "tr",
175+
"uk", "yo", "zgh", "zh-hans", "zh-hant"
176+
};
177+
int i = 0, defaultLanguage = -1;
178+
for (const QString& code : m_appLangCodes) {
179+
QString name = QLocale::languageToString(QLocale(code).language());
180+
if(name == "English") defaultLanguage = i;
181+
i++;
182+
}
183+
m_appLangIndex = m_settings.value("app/languageIndex", defaultLanguage).toInt();
184+
// Below regards the search filters
160185
QString defaultLang = QLocale::languageToString(QLocale().language()) + '|' + QLocale().name().split("_").at(0);
161-
186+
QList<QString> defaultLangList; // Qt5 QList doesn't support supplying a constructor list
187+
defaultLangList.append(defaultLang);
188+
QVariant defaultLangVariant(defaultLangList);
189+
m_langList = m_settings.value("language", defaultLangVariant).toList();
162190
/*
163191
* Qt5 & Qt6 have slightly different behaviors with regards to initializing QVariant.
164192
* The below approach is specifically chosen to work with both versions.
@@ -170,15 +198,6 @@ void SettingsManager::initSettings()
170198
*
171199
* QList(QVariant(QChar, 'E'), QVariant(QChar, 'n'), QVariant(QChar, 'g'), ...
172200
*/
173-
174-
QList<QString> defaultLangList; // Qt5 QList doesn't support supplying a constructor list
175-
defaultLangList.append(defaultLang);
176-
QVariant defaultLangVariant(defaultLangList);
177-
m_langList = m_settings.value("language", defaultLangVariant).toList();
178-
179-
180-
// ui->comboBoxLanguage->addItems(languageList);
181-
182201
m_categoryList = m_settings.value("category", {}).toList();
183202
m_contentTypeList = m_settings.value("contentType", {}).toList();
184203
}

src/settingsmanager.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class SettingsManager : public QObject
3030
QString getMonitorDir() const { return m_monitorDir; }
3131
bool getMoveToTrash() const { return m_moveToTrash; }
3232
bool getReopenTab() const { return m_reopenTab; }
33+
QList<QString> getLanguageCodes() { return m_appLangCodes; }
34+
int getAppLanguageIndex() { return m_appLangIndex; }
3335
FilterList getLanguageList() { return deducePair(m_langList); }
3436
FilterList getCategoryList() { return deducePair(m_categoryList); }
3537
FilterList getContentType() { return deducePair(m_contentTypeList); }
@@ -43,6 +45,7 @@ public slots:
4345
void setMoveToTrash(bool moveToTrash);
4446
void setReopenTab(bool reopenTab);
4547
void setLanguage(FilterList langList);
48+
void setAppLanguage(int languageIndex);
4649
void setCategory(FilterList categoryList);
4750
void setContentType(FilterList contentTypeList);
4851

@@ -72,9 +75,11 @@ public slots:
7275
QString m_monitorDir;
7376
bool m_moveToTrash;
7477
bool m_reopenTab;
78+
int m_appLangIndex;
7579
QList<QVariant> m_langList;
7680
QList<QVariant> m_categoryList;
7781
QList<QVariant> m_contentTypeList;
82+
QList<QString> m_appLangCodes;
7883
};
7984

8085
#endif // SETTINGSMANAGER_H

src/settingsview.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ SettingsView::SettingsView(QWidget *parent)
1515
connect(ui->reopenTabToggle, &QCheckBox::clicked, this, &SettingsView::setReopenTab);
1616
connect(ui->browseButton, &QPushButton::clicked, this, &SettingsView::browseDownloadDir);
1717
connect(ui->resetButton, &QPushButton::clicked, this, &SettingsView::resetDownloadDir);
18-
connect(ui->comboBoxLanguage, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SettingsView::languageSelected);
1918
connect(ui->monitorBrowse, &QPushButton::clicked, this, &SettingsView::browseMonitorDir);
2019
connect(ui->monitorClear, &QPushButton::clicked, this, &SettingsView::clearMonitorDir);
2120
connect(KiwixApp::instance()->getSettingsManager(), &SettingsManager::downloadDirChanged, this, &SettingsView::onDownloadDirChanged);
@@ -43,22 +42,28 @@ SettingsView::SettingsView(QWidget *parent)
4342

4443
}
4544

46-
void SettingsView::init(int zoomPercent, const QString &downloadDir,
47-
const QString &monitorDir, const bool moveToTrash,
48-
bool reopentab)
45+
46+
void SettingsView::init(int zoomPercent, const QString &downloadDir, const QString &monitorDir, const int &langIndex, const bool moveToTrash, bool reopentab)
4947
{
5048
ui->zoomPercentSpinBox->setValue(zoomPercent);
5149
ui->downloadDirPath->setText(downloadDir);
5250
if (monitorDir == QString()) {
5351
ui->monitorClear->hide();
5452
}
5553
ui->monitorDirPath->setText(monitorDir);
54+
// Application Language Code
55+
const QList<QString> languageCodes = KiwixApp::instance()->getSettingsManager()->getLanguageCodes();
5656
QStringList languageList;
57-
languageList << "English" << "Spanish" << "French" << "German" << "Chinese";
57+
for (const QString& code : languageCodes) {
58+
QString name = QLocale::languageToString(QLocale(code).language());
59+
languageList.append(name);
60+
}
5861
ui->comboBoxLanguage->addItems(languageList);
59-
ui->comboBoxLanguage->setCurrentText("Spanish");
62+
ui->comboBoxLanguage->setCurrentIndex(langIndex);
6063
ui->moveToTrashToggle->setChecked(moveToTrash);
6164
ui->reopenTabToggle->setChecked(reopentab);
65+
// Connect comboBox to change handler after initialization to avoid false calls
66+
connect(ui->comboBoxLanguage, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SettingsView::languageSelected);
6267
}
6368
bool SettingsView::confirmDialog( QString messageText, QString messageTitle)
6469
{
@@ -166,10 +171,8 @@ void SettingsView::onDownloadDirChanged(const QString &dir)
166171

167172
void SettingsView::languageSelected(const int &languageIndex)
168173
{
169-
qInfo() << languageIndex;
170-
// ui->comboBoxLanguage->setCurrentText(language);
171174
ui->comboBoxLanguage->setCurrentIndex(languageIndex);
172-
//KiwixApp::instance()->getSettingsManager()->setDownloadDir(dir);
175+
KiwixApp::instance()->getSettingsManager()->setAppLanguage(languageIndex);
173176
}
174177

175178
void SettingsView::onMonitorDirChanged(const QString &dir)

src/settingsview.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ class SettingsView : public QWidget
1111
public:
1212
SettingsView(QWidget *parent = nullptr);
1313
~SettingsView(){};
14-
void init(int zoomPercent, const QString &downloadDir,
15-
const QString &monitorDir, const bool moveToTrash,
16-
bool reopentab);
17-
public Q_SLOTS:
14+
void init(int zoomPercent, const QString &downloadDir, const QString &monitorDir, const int &langIndex, const bool moveToTrash, bool reopentab);
15+
public Q_SLOTS:
1816
void resetDownloadDir();
1917
void browseDownloadDir();
2018
void browseMonitorDir();

0 commit comments

Comments
 (0)