-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqi18n.cpp
157 lines (133 loc) · 4.3 KB
/
qi18n.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "qi18n.h"
#include <QFile>
#include <QDir>
#include <QJsonDocument>
#include <QSettings>
#include <QDir>
#include <QDebug>
QI18n* QI18n::instance = nullptr;
static void merge(QJsonObject& target, const QJsonObject& source)
{
for (auto it = source.constBegin() ; it != source.constEnd() ; ++it)
{
QJsonObject::iterator target_iterator = target.find(it.key());
if (target_iterator != target.end())
{
if (target_iterator->isObject() && it->isObject())
{
QJsonObject newObject = target_iterator->toObject();
merge(newObject, it->toObject());
target.insert(it.key(), newObject);
}
else
qDebug() << "QI18n: merge(QJsonObject,QJsonObject): duplicate key cannot be merged:" << it.key();
}
else
target.insert(it.key(), it.value());
}
}
QI18n::QI18n(const QString& rootPath, const QString& defaultLocaleName, QObject *parent) : QObject(parent), rootPath(rootPath)
{
QString defaultLocale = QSettings().value("locale", defaultLocaleName).toString();
instance = this;
QStringList files = QDir(rootPath).entryList(QStringList() << "*.json", QDir::NoFilter, QDir::Name);
for (QString& file : files)
locales << file.replace(".json", "");
if (locales.contains(defaultLocale))
currentLocale = defaultLocale;
else if (locales.size() > 0)
currentLocale = locales.first();
loadCurrentLocale();
connect(this, &QI18n::currentLocaleChanged, this, &QI18n::loadCurrentLocale);
}
QString QI18n::getSourceForLocale(const QString &locale)
{
return QI18n::get()->rootPath + '/' + locale + ".json";
}
QString QI18n::getSourceForLocale(const QString& translationFile, const QString &locale)
{
QI18n* i18n = QI18n::get();
const auto base_path = i18n->rootPath + '/' + translationFile;
if (*translationFile.rbegin() == '/')
return base_path + locale + ".json";
return base_path + '.' + locale + ".json";
}
static QStringList getSourcesForLocale(const QString& locale, const QString& basePath)
{
QDir directory(basePath);
QString filter = "*." + locale + ".json";
QFileInfoList list;
QStringList files;
QStringList bannedFolder{".", ".."};
directory.setFilter(QDir::Dirs);
list = directory.entryInfoList();
for (int i = 0 ; i < list.size() ; ++i)
{
if (bannedFolder.indexOf(list.at(i).fileName()) >= 0) continue ;
files << getSourcesForLocale(locale, basePath + '/' + list.at(i).fileName() + '/');
}
directory.setNameFilters(QStringList() << filter << (locale + ".json"));
directory.setFilter(QDir::Files);
list = directory.entryInfoList();
for (int i = 0 ; i < list.size() ; ++i)
files << (basePath + '/' + list.at(i).fileName());
return files;
}
static void loadLocale(const QString& locale, QJsonObject& data, const QString& rootPath)
{
while (data.begin() != data.end()) data.erase(data.begin());
for (const QString& filename : getSourcesForLocale(locale, rootPath))
{
QFile file(filename);
qDebug() << "QI18n: Loading file" << filename;
if (file.open(QIODevice::ReadOnly))
{
QJsonObject fileData = QJsonDocument::fromJson(file.readAll()).object();
merge(data, fileData);
}
else
qDebug() << "QI18n: failed to open file" << filename;
}
}
void QI18n::loadCurrentLocale()
{
qDebug() << "QI18n: Loading locale" << currentLocale;
loadLocale(currentLocale, data, rootPath);
QSettings().setValue("locale", currentLocale);
emit translationsChanged();
}
QString QI18n::t(const QString &key) const
{
QJsonObject group = getTranslationGroupForKey(key);
QJsonValue value = group[key.split('.').last()];
if (value.isUndefined() || value.isNull())
return key;
return value.toString();
}
QString QI18n::t(const QString& key, const QVariantMap& variables) const
{
QString str = t(key);
for (const QString& varname : variables.keys())
{
QString hint("{{" + varname + "}}");
str.replace(hint, variables[varname].toString());
}
return str;
}
QJsonObject QI18n::getTranslationGroupForKey(const QString &key) const
{
if (key.indexOf('.') > 0)
{
QStringList keys = key.split('.');
keys.removeLast();
return getTranslationGroup(keys);
}
return data;
}
QJsonObject QI18n::getTranslationGroup(const QStringList &path) const
{
QJsonObject group = data;
for (const QString& key : path)
group = group[key].toObject();
return group;
}