mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-24 04:32:15 -04:00
Translation class
This commit is contained in:
parent
0c64f615bf
commit
7251c89c7d
@ -46,6 +46,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
||||
|
||||
SOURCES += \
|
||||
src/findinpagebar.cpp \
|
||||
src/translation.cpp \
|
||||
src/main.cpp \
|
||||
src/mainwindow.cpp \
|
||||
src/kiwixapp.cpp \
|
||||
@ -75,6 +76,7 @@ SOURCES += \
|
||||
|
||||
HEADERS += \
|
||||
src/findinpagebar.h \
|
||||
src/translation.h \
|
||||
src/mainwindow.h \
|
||||
src/kiwixapp.h \
|
||||
src/kprofile.h \
|
||||
|
@ -24,6 +24,12 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
|
||||
appendToDirectory(m_libraryDirectory.toStdString(),"library.xml"),
|
||||
m_settingsManager.getKiwixServerPort()))
|
||||
{
|
||||
try {
|
||||
m_translation.setTranslation(QLocale());
|
||||
} catch (exception& e) {
|
||||
QMessageBox::critical(nullptr, "Translation error", e.what());
|
||||
return;
|
||||
}
|
||||
m_qtTranslator.load(QLocale(), "qt", "_",
|
||||
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
|
||||
installTranslator(&m_qtTranslator);
|
||||
|
51
src/translation.cpp
Normal file
51
src/translation.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
#include "translation.h"
|
||||
#include <QFile>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QDebug>
|
||||
|
||||
Translation::Translation()
|
||||
{
|
||||
}
|
||||
|
||||
void Translation::setTranslation(QLocale locale)
|
||||
{
|
||||
auto defaultText = JsonFileToQMap(":/i18n/en.json");
|
||||
if (defaultText.isEmpty()) {
|
||||
throw std::runtime_error("Invalid translation file");
|
||||
}
|
||||
if (locale.bcp47Name() == "en") {
|
||||
m_translations = defaultText;
|
||||
return;
|
||||
}
|
||||
m_translations = JsonFileToQMap(":/i18n/" + locale.bcp47Name() + ".json");
|
||||
for (auto &key : defaultText.keys()) {
|
||||
if (!m_translations.contains(key) || m_translations.value(key).isEmpty()) {
|
||||
m_translations.insert(key, defaultText.value(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QMap<QString, QString> Translation::JsonFileToQMap(const QString &filePath)
|
||||
{
|
||||
QMap<QString, QString> translations;
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
return translations;
|
||||
}
|
||||
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
|
||||
file.close();
|
||||
if (doc.isNull()) {
|
||||
return translations;
|
||||
}
|
||||
auto jsonObj = doc.object();
|
||||
for (auto &key : jsonObj.keys()) {
|
||||
translations.insert(key, jsonObj.value(key).toString());
|
||||
}
|
||||
return translations;
|
||||
}
|
||||
|
||||
QString Translation::getText(const QString &key)
|
||||
{
|
||||
return (m_translations.contains(key)) ? m_translations.value(key) : key;
|
||||
}
|
23
src/translation.h
Normal file
23
src/translation.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef TRANSLATION_H
|
||||
#define TRANSLATION_H
|
||||
|
||||
#include <QString>
|
||||
#include <QMap>
|
||||
#include <QLocale>
|
||||
|
||||
class Translation
|
||||
{
|
||||
public:
|
||||
Translation();
|
||||
|
||||
void setTranslation(QLocale locale);
|
||||
QString getText(const QString &key);
|
||||
|
||||
private:
|
||||
QMap<QString, QString> JsonFileToQMap(const QString &filePath);
|
||||
|
||||
private:
|
||||
QMap<QString, QString> m_translations;
|
||||
};
|
||||
|
||||
#endif // TRANSLATION_H
|
Loading…
x
Reference in New Issue
Block a user