mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2026-07-25 06:54:43 +00:00
feat: cache localizations
This commit is contained in:
@@ -6,10 +6,14 @@
|
||||
// Copyright @Radolyn, 2025
|
||||
#include "ayu_lang.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
|
||||
#include "qjsondocument.h"
|
||||
#include "core/application.h"
|
||||
#include "core/core_settings.h"
|
||||
#include "lang/lang_instance.h"
|
||||
#include "storage/localstorage.h"
|
||||
|
||||
// hard-coded languages
|
||||
std::map<QString, QString> langMapping = {
|
||||
@@ -35,14 +39,72 @@ AyuLanguage::AyuLanguage() = default;
|
||||
|
||||
void AyuLanguage::init() {
|
||||
if (!instance) instance = new AyuLanguage;
|
||||
instance->loadCachedLanguage();
|
||||
}
|
||||
|
||||
AyuLanguage *AyuLanguage::currentInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
QString AyuLanguage::getCacheDir() const {
|
||||
return cWorkingDir() + u"tdata/ayu/languages/"_q;
|
||||
}
|
||||
|
||||
QString AyuLanguage::getCachePath(const QString &langId) const {
|
||||
return getCacheDir() + langId + u".json"_q;
|
||||
}
|
||||
|
||||
void AyuLanguage::loadCachedLanguage() {
|
||||
const auto langPackId = Lang::GetInstance().id();
|
||||
const auto langPackBaseId = Lang::GetInstance().baseId();
|
||||
auto finalLangPackId = langMapping.contains(langPackId) ? langMapping[langPackId] : langPackId;
|
||||
|
||||
if (finalLangPackId.isEmpty()) {
|
||||
finalLangPackId = langPackBaseId;
|
||||
}
|
||||
if (finalLangPackId.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto cachePath = getCachePath(finalLangPackId);
|
||||
QFile file(cachePath);
|
||||
if (!file.exists()) {
|
||||
const auto basePath = getCachePath(langPackBaseId);
|
||||
if (!QFile::exists(basePath)) {
|
||||
return;
|
||||
}
|
||||
file.setFileName(basePath);
|
||||
}
|
||||
|
||||
if (file.open(QIODevice::ReadOnly)) {
|
||||
const auto data = file.readAll();
|
||||
file.close();
|
||||
|
||||
QJsonParseError error{};
|
||||
const auto doc = QJsonDocument::fromJson(data, &error);
|
||||
if (error.error == QJsonParseError::NoError) {
|
||||
LOG(("Loading cached AyuGram language: %1").arg(finalLangPackId));
|
||||
applyLanguageJson(doc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AyuLanguage::saveCachedLanguage(const QByteArray &json, const QString &langId) {
|
||||
const auto cacheDir = getCacheDir();
|
||||
QDir().mkpath(cacheDir);
|
||||
|
||||
const auto cachePath = getCachePath(langId);
|
||||
QFile file(cachePath);
|
||||
if (file.open(QIODevice::WriteOnly)) {
|
||||
file.write(json);
|
||||
file.close();
|
||||
LOG(("Cached AyuGram language: %1").arg(langId));
|
||||
}
|
||||
}
|
||||
|
||||
void AyuLanguage::fetchLanguage(const QString &id, const QString &baseId) {
|
||||
auto finalLangPackId = langMapping.contains(id) ? langMapping[id] : id;
|
||||
_currentLangId = finalLangPackId.isEmpty() ? baseId : finalLangPackId;
|
||||
|
||||
if (Core::App().settings().proxy().isEnabled()) {
|
||||
const auto proxy = Core::App().settings().proxy().selected();
|
||||
@@ -84,6 +146,7 @@ void AyuLanguage::fetchFinished() {
|
||||
QJsonParseError error{};
|
||||
const auto doc = QJsonDocument::fromJson(result, &error);
|
||||
if (error.error == QJsonParseError::NoError) {
|
||||
saveCachedLanguage(result, _currentLangId);
|
||||
applyLanguageJson(doc);
|
||||
} else {
|
||||
LOG(("Incorrect language JSON File."));
|
||||
|
||||
@@ -30,7 +30,13 @@ private:
|
||||
AyuLanguage();
|
||||
~AyuLanguage() override = default;
|
||||
|
||||
void loadCachedLanguage();
|
||||
void saveCachedLanguage(const QByteArray &json, const QString &langId);
|
||||
[[nodiscard]] QString getCacheDir() const;
|
||||
[[nodiscard]] QString getCachePath(const QString &langId) const;
|
||||
|
||||
QNetworkAccessManager networkManager;
|
||||
QNetworkReply *_chkReply = nullptr;
|
||||
bool needFallback = false;
|
||||
QString _currentLangId;
|
||||
};
|
||||
|
||||
@@ -25,8 +25,6 @@ using json = nlohmann::json;
|
||||
|
||||
namespace AyuSettings {
|
||||
|
||||
const std::string filename = "tdata/ayu_settings.json";
|
||||
|
||||
std::optional<AyuGramSettings> settings = std::nullopt;
|
||||
|
||||
rpl::variable<bool> sendReadMessagesReactive;
|
||||
@@ -155,8 +153,12 @@ AyuGramSettings &getInstance() {
|
||||
return settings.value();
|
||||
}
|
||||
|
||||
std::string getSettingsPath() {
|
||||
return (cWorkingDir() + u"tdata/ayu_settings.json"_q).toStdString();
|
||||
}
|
||||
|
||||
void load() {
|
||||
std::ifstream file(filename);
|
||||
std::ifstream file(getSettingsPath());
|
||||
if (!file.good()) {
|
||||
return;
|
||||
}
|
||||
@@ -194,7 +196,7 @@ void save() {
|
||||
json p = settings.value();
|
||||
|
||||
std::ofstream file;
|
||||
file.open(filename);
|
||||
file.open(getSettingsPath());
|
||||
file << p.dump(4);
|
||||
file.close();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user