Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented application language change behavior and setting #1069

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions src/contentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ void ContentManager::setLanguages()
for (auto language : languageData) {
auto langCode = QString::fromStdString(language);
auto selfName = QString::fromStdString(kiwix::getLanguageSelfName(language));
// qDebug() << langCode;
// qDebug() << selfName << "!";
languages.push_back({langCode, selfName});
}
m_languages = languages;
Expand Down
4 changes: 4 additions & 0 deletions src/contentmanagerside.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,9 @@ void ContentManagerSide::setCategories(QStringList categories)

void ContentManagerSide::setLanguages(ContentManager::LanguageList langList)
{
qDebug() << "List of languages:";
for (const QPair<QString, QString>& languagePair : langList) {
qDebug() << languagePair.first;
}
mp_languages->setSelections(langList, KiwixApp::instance()->getSettingsManager()->getLanguageList());
}
38 changes: 23 additions & 15 deletions src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,7 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
m_server(m_library.getKiwixLibrary(), mp_nameMapper),
mp_session(nullptr)
{
try {
m_translation.setTranslation(QLocale());
} catch (std::exception& e) {
QMessageBox::critical(nullptr, "Translation error", e.what());
return;
}

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QString path = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
#else
QString path = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
#endif
loadAndInstallTranslations(m_qtTranslator, "qt", path);
loadAndInstallTranslations(m_appTranslator, "kiwix-desktop", ":/i18n/");

setAppLanguage();
QFontDatabase::addApplicationFont(":/fonts/Selawik/selawkb.ttf");
QFontDatabase::addApplicationFont(":/fonts/Selawik/selawkl.ttf");
QFontDatabase::addApplicationFont(":/fonts/Selawik/selawksb.ttf");
Expand Down Expand Up @@ -250,6 +236,28 @@ void KiwixApp::openZimFile(const QString &zimfile)
openUrl(QUrl("zim://"+zimId+".zim/"));
}

void KiwixApp::setAppLanguage()
{
const QList<QString> languageCodes = getSettingsManager()->getLanguageCodes();
const QString code = languageCodes.at(getSettingsManager()->getAppLanguageIndex());
QLocale appLanguage(code);
try {
m_translation.setTranslation(appLanguage);
} catch (std::exception& e) {
QMessageBox::critical(nullptr, "Translation error", e.what());
return;
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QString qtTranslatorSuffix = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
#else
QString qtTranslatorSuffix = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
#endif
m_qtTranslator.load(appLanguage, "qt", "_", qtTranslatorSuffix);
installTranslator(&m_qtTranslator);
m_appTranslator.load(appLanguage, "kiwix-desktop", "_", ":/i18n/");
installTranslator(&m_appTranslator);
}

void KiwixApp::printPage()
{
if(!getTabWidget()->currentZimView())
Expand Down
1 change: 1 addition & 0 deletions src/kiwixapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class KiwixApp : public QtSingleApplication
public slots:
void newTab();
void openZimFile(const QString& zimfile="");
void setAppLanguage();
void openUrl(const QString& url, bool newTab=true);
void openUrl(const QUrl& url, bool newTab=true);
void printPage();
Expand Down
41 changes: 32 additions & 9 deletions src/settingsmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ SettingsView* SettingsManager::getView()
{
if (m_view == nullptr) {
auto view = new SettingsView();
view->init(m_zoomFactor * 100, m_downloadDir, m_monitorDir,
m_moveToTrash, m_reopenTab);
view->init(m_zoomFactor * 100, m_downloadDir, m_monitorDir, m_appLangIndex, m_moveToTrash, m_reopenTab);
connect(view, &QObject::destroyed, this, [=]() { m_view = nullptr; });
m_view = view;
}
Expand Down Expand Up @@ -127,13 +126,20 @@ SettingsManager::FilterList SettingsManager::deducePair(QList<QVariant> variantL
return pairList;
}

void SettingsManager::setLanguage(FilterList langList)
void SettingsManager::setLanguage(FilterList langList) // This regards the search filter languages
{
m_langList = flattenPair(langList);
setSettings("language", m_langList);
emit(languageChanged(m_langList));
}

void SettingsManager::setAppLanguage(int languageIndex) // This regards the application language
{
m_appLangIndex = languageIndex;
KiwixApp::instance()->setAppLanguage();
m_settings.setValue("app/languageIndex", languageIndex);
}

void SettingsManager::setCategory(FilterList categoryList)
{
m_categoryList = flattenPair(categoryList);
Expand All @@ -157,8 +163,30 @@ void SettingsManager::initSettings()
m_monitorDir = m_settings.value("monitor/dir", QString("")).toString();
m_moveToTrash = m_settings.value("moveToTrash", true).toBool();
m_reopenTab = m_settings.value("reopenTab", false).toBool();
// This regards the application language
m_appLangCodes = {
"ar", "bcl", "bn", "br", "ca", "cs", "da", "dag", "de", "diq",
"en", "eo", "es", "fa", "fi", "fr", "gsw", "ha", "he", "hi",
"hy", "ia", "id", "ie", "ig", "igl", "io", "it", "ja", "ko",
"ku-latn", "lb", "mk", "ms", "my", "nb", "nl", "nqo", "or",
"pl", "pt-br", "pt", "qqq", "ro", "roa-tara", "ru", "sc",
"scn", "sdc", "se", "sk", "skr-arab", "sl", "sms", "sq",
"sr-ec", "sro", "sv", "sw", "ta", "te", "th", "tn", "tr",
"uk", "yo", "zgh", "zh-hans", "zh-hant"
};
int i = 0, defaultLanguage = -1;
for (const QString& code : m_appLangCodes) {
QString name = QLocale::languageToString(QLocale(code).language());
if(name == "English") defaultLanguage = i;
i++;
}
m_appLangIndex = m_settings.value("app/languageIndex", defaultLanguage).toInt();
// Below regards the search filters
QString defaultLang = QLocale::languageToString(QLocale().language()) + '|' + QLocale().name().split("_").at(0);

QList<QString> defaultLangList; // Qt5 QList doesn't support supplying a constructor list
defaultLangList.append(defaultLang);
QVariant defaultLangVariant(defaultLangList);
m_langList = m_settings.value("language", defaultLangVariant).toList();
/*
* Qt5 & Qt6 have slightly different behaviors with regards to initializing QVariant.
* The below approach is specifically chosen to work with both versions.
Expand All @@ -170,11 +198,6 @@ void SettingsManager::initSettings()
*
* QList(QVariant(QChar, 'E'), QVariant(QChar, 'n'), QVariant(QChar, 'g'), ...
*/
QList<QString> defaultLangList; // Qt5 QList doesn't support supplying a constructor list
defaultLangList.append(defaultLang);
QVariant defaultLangVariant(defaultLangList);
m_langList = m_settings.value("language", defaultLangVariant).toList();

m_categoryList = m_settings.value("category", {}).toList();
m_contentTypeList = m_settings.value("contentType", {}).toList();
}
5 changes: 5 additions & 0 deletions src/settingsmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class SettingsManager : public QObject
QString getMonitorDir() const { return m_monitorDir; }
bool getMoveToTrash() const { return m_moveToTrash; }
bool getReopenTab() const { return m_reopenTab; }
QList<QString> getLanguageCodes() { return m_appLangCodes; }
int getAppLanguageIndex() { return m_appLangIndex; }
FilterList getLanguageList() { return deducePair(m_langList); }
FilterList getCategoryList() { return deducePair(m_categoryList); }
FilterList getContentType() { return deducePair(m_contentTypeList); }
Expand All @@ -43,6 +45,7 @@ public slots:
void setMoveToTrash(bool moveToTrash);
void setReopenTab(bool reopenTab);
void setLanguage(FilterList langList);
void setAppLanguage(int languageIndex);
void setCategory(FilterList categoryList);
void setContentType(FilterList contentTypeList);

Expand Down Expand Up @@ -72,9 +75,11 @@ public slots:
QString m_monitorDir;
bool m_moveToTrash;
bool m_reopenTab;
int m_appLangIndex;
QList<QVariant> m_langList;
QList<QVariant> m_categoryList;
QList<QVariant> m_contentTypeList;
QList<QString> m_appLangCodes;
};

#endif // SETTINGSMANAGER_H
24 changes: 20 additions & 4 deletions src/settingsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,35 @@ SettingsView::SettingsView(QWidget *parent)
ui->moveToTrashLabel->setText(gt("move-files-to-trash"));
ui->reopenTabLabel->setText(gt("open-previous-tabs-at-startup"));
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
ui->line_5->hide();
ui->lineMonitor->hide();
ui->moveToTrashLabel->hide();
ui->moveToTrashToggle->hide();
#endif

}

void SettingsView::init(int zoomPercent, const QString &downloadDir,
const QString &monitorDir, const bool moveToTrash,
bool reopentab)

void SettingsView::init(int zoomPercent, const QString &downloadDir, const QString &monitorDir, const int &langIndex, const bool moveToTrash, bool reopentab)
{
ui->zoomPercentSpinBox->setValue(zoomPercent);
ui->downloadDirPath->setText(downloadDir);
if (monitorDir == QString()) {
ui->monitorClear->hide();
}
ui->monitorDirPath->setText(monitorDir);
// Application Language Code
const QList<QString> languageCodes = KiwixApp::instance()->getSettingsManager()->getLanguageCodes();
QStringList languageList;
for (const QString& code : languageCodes) {
QString name = QLocale::languageToString(QLocale(code).language());
languageList.append(name);
}
ui->comboBoxLanguage->addItems(languageList);
ui->comboBoxLanguage->setCurrentIndex(langIndex);
ui->moveToTrashToggle->setChecked(moveToTrash);
ui->reopenTabToggle->setChecked(reopentab);
// Connect comboBox to change handler after initialization to avoid false calls
connect(ui->comboBoxLanguage, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SettingsView::languageSelected);
}
bool SettingsView::confirmDialog( QString messageText, QString messageTitle)
{
Expand Down Expand Up @@ -159,6 +169,12 @@ void SettingsView::onDownloadDirChanged(const QString &dir)
ui->downloadDirPath->setText(dir);
}

void SettingsView::languageSelected(const int &languageIndex)
{
ui->comboBoxLanguage->setCurrentIndex(languageIndex);
KiwixApp::instance()->getSettingsManager()->setAppLanguage(languageIndex);
}

void SettingsView::onMonitorDirChanged(const QString &dir)
{
if (dir == "") {
Expand Down
7 changes: 3 additions & 4 deletions src/settingsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ class SettingsView : public QWidget
public:
SettingsView(QWidget *parent = nullptr);
~SettingsView(){};
void init(int zoomPercent, const QString &downloadDir,
const QString &monitorDir, const bool moveToTrash,
bool reopentab);
public Q_SLOTS:
void init(int zoomPercent, const QString &downloadDir, const QString &monitorDir, const int &langIndex, const bool moveToTrash, bool reopentab);
public Q_SLOTS:
void resetDownloadDir();
void browseDownloadDir();
void browseMonitorDir();
Expand All @@ -23,6 +21,7 @@ class SettingsView : public QWidget
void setMoveToTrash(bool moveToTrash);
void setReopenTab(bool reopen);
void onDownloadDirChanged(const QString &dir);
void languageSelected(const int &languageIndex);
void onMonitorDirChanged(const QString &dir);
void onZoomChanged(qreal zoomFactor);
void onMoveToTrashChanged(bool moveToTrash);
Expand Down
Loading
Loading