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
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
75 changes: 65 additions & 10 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 @@ -148,6 +154,42 @@ void SettingsManager::setContentType(FilterList contentTypeList)
emit(contentTypeChanged(m_contentTypeList));
}

QString SettingsManager::getLanguageName(QString fileName) // Get native language name based on filename
{
QString dirPath = "resources/i18n";
QFile file(dirPath + "/" + fileName);
file.open(QIODevice::ReadOnly);
QByteArray jsonData = file.readAll();
file.close();

QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(jsonData, &error);
if (error.error != QJsonParseError::NoError) {
qDebug() << "Error parsing JSON " << fileName << ":" << error.errorString();
return QString();
}

QJsonObject obj = doc.object();
if (obj.contains("name")) { return obj.value("name").toString(); }
else { qDebug() << "Cannot find element \"name\" in " << fileName; }

return QString();
}

void SettingsManager::loadAvailableLanguages() {
QString dirPath = "resources/i18n";
QDir directory(dirPath);
if (!directory.exists()) {
qDebug() << "Directory does not exist:" << dirPath;
return;
}
QStringList jsonFiles = directory.entryList(QDir::Files);
for(QString &fileName : jsonFiles) {
if(getLanguageName(fileName).isEmpty() || fileName == "qqq.json") { continue; }
m_appLangCodes.append(fileName.split(".")[0]);
}
}

void SettingsManager::initSettings()
{
m_kiwixServerPort = m_settings.value("localKiwixServer/port", 8080).toInt();
Expand All @@ -157,8 +199,26 @@ 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();
QString defaultLang = QLocale::languageToString(QLocale().language()) + '|' + QLocale().name().split("_").at(0);

// This regards the application language
loadAvailableLanguages();

int i = 0, defaultLanguage = -1;
for (const QString& code : m_appLangCodes) {
QString name = QLocale::languageToString(QLocale(code).language());
if(name == "English")
{
defaultLanguage = i;
break;
}
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); // TODO maybe needs fix
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 +230,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();
}
7 changes: 7 additions & 0 deletions src/settingsmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ 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; }
QString getLanguageName(QString fileName);
void loadAvailableLanguages();
FilterList getLanguageList() { return deducePair(m_langList); }
FilterList getCategoryList() { return deducePair(m_categoryList); }
FilterList getContentType() { return deducePair(m_contentTypeList); }
Expand All @@ -43,6 +47,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 +77,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
45 changes: 41 additions & 4 deletions src/settingsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,38 @@ 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 = KiwixApp::instance()->getSettingsManager()->getLanguageName(code + ".json");
QString qName = QLocale::languageToString(QLocale(code).language());
if("C" == qName) { continue; }
// qInfo() << name;
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 +172,30 @@ void SettingsView::onDownloadDirChanged(const QString &dir)
ui->downloadDirPath->setText(dir);
}

void SettingsView::languageSelected(const int &languageIndex)
{
// QMessageBox msgBox;
// msgBox.setIcon(QMessageBox::Question);
// msgBox.setText("Restart to apply language change?");
// msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
// msgBox.setDefaultButton(QMessageBox::Yes);
// int ret = msgBox.exec();
// if (ret == QMessageBox::Yes) {
// ui->comboBoxLanguage->setCurrentIndex(languageIndex);
// KiwixApp::instance()->getSettingsManager()->setAppLanguage(languageIndex);
// qInfo() << "restarting";
// qApp->quit();
// // QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
// exit(0);
// } else {
// int previousIndex = KiwixApp::instance()->getSettingsManager()->getAppLanguageIndex();
// ui->comboBoxLanguage->setCurrentIndex(previousIndex); // TODO causes issues with re emit
// }

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