Add translator core, mailitem model, main.cpp, and en translation
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
#include "translator.h"
|
||||
#include <QFile>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QDebug>
|
||||
|
||||
Translator& Translator::instance()
|
||||
{
|
||||
static Translator instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
bool Translator::loadLanguage(const QString& langCode)
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
m_translations.clear();
|
||||
|
||||
QString filePath = QStringLiteral("resources/translations/%1/resources.json").arg(langCode);
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
qWarning() << "Failed to open translation file:" << filePath;
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray jsonData = file.readAll();
|
||||
file.close();
|
||||
|
||||
QJsonParseError parseError;
|
||||
QJsonDocument doc = QJsonDocument::fromJson(jsonData, &parseError);
|
||||
if (parseError.error != QJsonParseError::NoError) {
|
||||
qWarning() << "Failed to parse translation JSON:" << parseError.errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!doc.isObject()) {
|
||||
qWarning() << "Translation JSON is not an object";
|
||||
return false;
|
||||
}
|
||||
|
||||
QJsonObject obj = doc.object();
|
||||
for (auto it = obj.begin(); it != obj.end(); ++it) {
|
||||
if (it.value().isString()) {
|
||||
m_translations.insert(it.key(), it.value().toString());
|
||||
}
|
||||
}
|
||||
|
||||
m_currentLang = langCode;
|
||||
return true;
|
||||
}
|
||||
|
||||
QString Translator::tr(const QString& key) const
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
auto it = m_translations.find(key);
|
||||
if (it != m_translations.end()) {
|
||||
return it.value();
|
||||
}
|
||||
return key; // fallback to key
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QMap>
|
||||
#include <QMutex>
|
||||
|
||||
class Translator
|
||||
{
|
||||
public:
|
||||
static Translator& instance();
|
||||
~Translator() = default;
|
||||
|
||||
// Load translation file for given language code (e.g., "en_US")
|
||||
bool loadLanguage(const QString& langCode);
|
||||
|
||||
// Translate a key, returns translated string or key if not found
|
||||
QString tr(const QString& key) const;
|
||||
|
||||
// Get current language code
|
||||
QString currentLanguage() const { return m_currentLang; }
|
||||
|
||||
private:
|
||||
Translator() = default;
|
||||
Q_DISABLE_COPY(Translator)
|
||||
|
||||
QMap<QString, QString> m_translations;
|
||||
QString m_currentLang;
|
||||
mutable QMutex m_mutex;
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <QCoreApplication>
|
||||
#include <QDebug>
|
||||
#include "core/translator.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
// Load English translation
|
||||
Translator& translator = Translator::instance();
|
||||
if (!translator.loadLanguage("en_US")) {
|
||||
qWarning() << "Failed to load translation";
|
||||
}
|
||||
|
||||
qDebug() << translator.tr("appName");
|
||||
qDebug() << translator.tr("welcomeMessage");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user