-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocales.cpp
244 lines (189 loc) · 6.61 KB
/
locales.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright (C) 2018-2025 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#include "locales.h"
#include <QCoreApplication>
#include <QTranslator>
#include <QLocale>
#include <QLibraryInfo>
#include <QRegularExpression>
#include <QLocale>
#include <QMap>
using namespace QuasarAppUtils;
bool QuasarAppUtils::Locales::findQmPrivate(const QString &prefix,
QList<QTranslator*> &qmFiles) {
for (const auto &location: std::as_const(_locations)) {
const auto availableFiles = QDir(location).entryInfoList(
{prefix + ".qm"}, QDir::Files);
for (const auto &file : availableFiles) {
auto qmFile = new QTranslator();
if(!qmFile->load(file.absoluteFilePath())) {
qWarning() << "Failed to load translation file : "
+ file.absoluteFilePath();
delete qmFile;
continue;
}
if (qmFile->isEmpty()) {
qDebug() << "Translation file is Empty: " +
file.absoluteFilePath();
delete qmFile;
continue;
}
auto language = qmFile->language();
if (language.size() && !language.contains(prefix, Qt::CaseInsensitive)) {
auto message = QString("The target language (%0) and a choosed qm file (%1) "
"is different, Loading will be skiped: ").
arg(language, file.absoluteFilePath());
qDebug() << message;
delete qmFile;
continue;
}
qmFiles += qmFile;
}
}
return qmFiles.size();
}
bool QuasarAppUtils::Locales::findQm(QString localePrefix,
QList<QTranslator *> &qmFiles) {
if (localePrefix.size() < 2) {
if (localePrefix.compare('c', Qt::CaseInsensitive) == 0) {
return findQmPrivate("en", qmFiles);
}
return false;
} else if (localePrefix.size() >= 4) {
return findQmPrivate(localePrefix.replace('-', '_'), qmFiles);
}
return findQmPrivate(localePrefix, qmFiles);
}
void QuasarAppUtils::Locales::installTranslations( QList<QTranslator *> &qmFiles)
{
for (const auto & translator: std::as_const(qmFiles)) {
if (!QCoreApplication::installTranslator(translator)) {
qWarning() << "Failed to install translation file : " + translator->filePath();
delete translator;
// we use a link of qmFiles so remove all invalid translations.
qmFiles.removeAll(translator);
continue;
}
}
}
QString Locales::translatePrivate(const char *source, const QLocale &locale) {
auto translations = _translations.value(locale);
for (const auto& tr : translations) {
auto result = tr->translate("QuasarAppUtils::Locales", source);
if (result.size()) {
return result;
}
}
return source;
}
bool Locales::setLocalePrivate(const QLocale &locale, bool force, bool install) {
if (force) {
clearCache(locale);
}
removeOldTranslation(_currentLocate);
// take a link to list of translations.
QList<QTranslator *> &qmFiles = _translations[locale];
if (qmFiles.isEmpty()) {
// fill list of translations
const auto list = locale.uiLanguages();
auto it = list.rbegin();
while (qmFiles.isEmpty() && it != list.rend() && !findQm(*it, qmFiles)) {
it++;
}
if (qmFiles.isEmpty())
return false;
}
if (install)
installTranslations(qmFiles);
emit sigTranslationChanged();
_currentLocate = locale;
return _translations[locale].size();
}
const QLocale &Locales::currentLocate() {
auto obj = instance();
return obj->currentLocatePrivate();
}
QString Locales::tr(const char *source, const QLocale &locale) {
auto obj = instance();
return obj->translatePrivate(source, locale);
}
bool Locales::setLocale(const QLocale &locale, bool force) {
auto obj = instance();
return obj->setLocalePrivate(locale, force);
}
bool Locales::init(const QList<QLocale> &locales, const QSet<QString> &location) {
auto obj = instance();
return obj->initPrivate(locales, location);
}
bool Locales::init(const QLocale &locale, const QSet<QString> & location) {
auto obj = instance();
return obj->initPrivate(locale, location);
}
bool Locales::initPrivate(const QLocale &locale, const QSet<QString> & locations) {
#if QT_VERSION <= QT_VERSION_CHECK(6, 0, 0)
auto defaultTr = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
#else
auto defaultTr = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
#endif
_locations = locations;
if (!_locations.contains(defaultTr)) {
_locations += defaultTr;
}
return setLocalePrivate(locale);
}
bool Locales::initPrivate(const QList<QLocale> &locales, const QSet<QString> &locations) {
#if QT_VERSION <= QT_VERSION_CHECK(6, 0, 0)
auto defaultTr = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
#else
auto defaultTr = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
#endif
_locations = locations;
if (!_locations.contains(defaultTr)) {
_locations += defaultTr;
}
for (const auto& locale: locales) {
if (!setLocalePrivate(locale, false, false)) {
return false;
}
}
return true;
}
void Locales::clearCache(const QLocale &locale) {
for (const auto & tr :std::as_const(_translations[locale])) {
QCoreApplication::removeTranslator(tr);
delete tr;
}
_translations[locale].clear();
}
void Locales::clearCache() {
for (auto it = _translations.keyBegin(); it != _translations.keyEnd(); it = std::next(it)) {
clearCache(*it);
}
_translations.clear();
}
Locales *Locales::instance() {
static auto instance = new Locales();
return instance;
}
void Locales::removeOldTranslation(const QLocale &locale) {
for (const auto & tr :std::as_const(_translations[locale])) {
QCoreApplication::removeTranslator(tr);
}
}
void Locales::addLocationPrivate(const QString &location) {
_locations += location;
}
const QLocale &Locales::currentLocatePrivate() const {
return _currentLocate;
}
void Locales::addLocation(const QString &location) {
auto obj = instance();
obj->addLocationPrivate(location);
}
Locales::~Locales() {
clearCache();
}