-
Notifications
You must be signed in to change notification settings - Fork 2
/
i18n.cpp
206 lines (172 loc) · 5.42 KB
/
i18n.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "i18n.h"
#include "globals.h"
#include <QFile>
#include <QDir>
#include <QJsonDocument>
#include <QSettings>
#include <QDir>
#include <QLocale>
#include <QDebug>
I18n* I18n::instance = nullptr;
void merge(QJsonObject&, const QJsonObject&);
I18n::I18n(QObject *parent) : QObject(parent)
{
QString defaultLocale;
instance = this;
QStringList files = QDir(ASSETS_PATH + "locales").entryList(QStringList() << "*.json", QDir::NoFilter, QDir::Name);
for (QString& file : files)
{
file.replace(".json", "");
if (file.indexOf('.') == -1)
locales << file;
}
defaultLocale = QSettings().value("locale", getSystemLocale()).toString();
if (locales.contains(defaultLocale))
currentLocale = defaultLocale;
else if (locales.size() > 0)
currentLocale = locales.first();
loadCurrentLocale();
connect(this, &I18n::currentLocaleChanged, this, &I18n::loadCurrentLocale);
}
QString I18n::getSystemLocale() const
{
QStringList uiLanguages = QLocale().uiLanguages();
qDebug() << "getSystemLocale" << uiLanguages;
for (const QString& locale : uiLanguages)
{
if (locales.contains(locale))
return locale;
}
return DEFAULT_LOCALE;
}
QString I18n::getSourceForLocale(const QString &locale)
{
return ASSETS_PATH + "locales/" + locale + ".json";
}
QString I18n::getSourceForLocale(const QString& translationFile, const QString &locale)
{
const auto base_path = ASSETS_PATH + "locales/" + translationFile;
if (*translationFile.rbegin() == '/')
return base_path + locale + ".json";
return base_path + '.' + locale + ".json";
}
static QStringList getSourcesForLocale(const QString& locale, const QString& basePath = ASSETS_PATH + "locales/")
{
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)
{
QStringList sources;
while (data.begin() != data.end()) data.erase(data.begin());
sources << getSourcesForLocale(locale, ":/assets/locales/")
<< getSourcesForLocale(locale);
for (const QString& filename : sources)
{
QFile file(filename);
qDebug() << "i18n: Loading file" << filename;
if (file.open(QIODevice::ReadOnly))
{
QJsonObject fileData = QJsonDocument::fromJson(file.readAll()).object();
merge(data, fileData);
}
else
qDebug() << "i18n: failed to open file" << filename;
}
}
void I18n::loadCurrentLocale()
{
qDebug() << "i18n: Loading locale" << currentLocale;
loadLocale(currentLocale, data);
QSettings().setValue("locale", currentLocale);
emit translationsChanged();
}
QUrl I18n::getFontPath(const QString& style, const QString& defaultPath) const
{
QJsonValue fontFile = getTranslation("fonts." + style + ".file");
if (fontFile.isUndefined() || fontFile.isNull())
return defaultPath;
return FILE_PROTOCOL + ASSETS_PATH + "fonts/" + fontFile.toString();
}
QVariantMap I18n::getFontMetrics(const QString& style, QVariantMap metrics) const
{
QJsonObject fontData = getTranslationGroupForKey("fonts." + style);
if (fontData.find("point-size") != fontData.end())
metrics.insert("point", fontData.find("point-size").value().toInt());
if (fontData.find("tiny-size") != fontData.end())
metrics.insert("tiny", fontData.find("tiny-size").value().toInt());
if (fontData.find("big-size") != fontData.end())
metrics.insert("big", fontData.find("big-size").value().toInt());
return metrics;
}
QUrl I18n::getConsoleFont() const
{
return getFontPath("console", "qrc:/assets/fonts/JH_FALLOUT.TTF");
}
QVariantMap I18n::getConsoleFontMetrics() const
{
return getFontMetrics("console", {{"point", 9}, {"tiny", 8}, {"big", 12}});
}
QUrl I18n::getTitleFont() const
{
return getFontPath("title", "qrc:/assets/fonts/fallout.ttf");
}
QVariantMap I18n::getTitleFontMetrics() const
{
return getFontMetrics("title", {{"point", 18}, {"tiny", 24}, {"big", 32}});
}
QJsonValue I18n::getTranslation(const QString& key) const
{
QJsonObject group = getTranslationGroupForKey(key);
return group[key.split('.').last()];
}
QString I18n::t(const QString &key) const
{
QJsonValue value = getTranslation(key);
if (value.isUndefined() || value.isNull())
return key;
return value.toString();
}
QString I18n::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 I18n::getTranslationGroupForKey(const QString &key) const
{
if (key.indexOf('.') > 0)
{
QStringList keys = key.split('.');
keys.removeLast();
return getTranslationGroup(keys);
}
return data;
}
QJsonObject I18n::getTranslationGroup(const QStringList &path) const
{
QJsonObject group = data;
for (const QString& key : path)
group = group[key].toObject();
return group;
}