|
34 | 34 | #include <stdexcept> |
35 | 35 | #include <vector> |
36 | 36 |
|
37 | | -#include <QCoreApplication> |
38 | 37 | #include <QDateTime> |
39 | 38 | #include <QDebug> |
40 | 39 | #include <QFile> |
@@ -91,46 +90,6 @@ namespace |
91 | 90 | return ret; |
92 | 91 | } |
93 | 92 |
|
94 | | - void translateDocument(const QString &locale, QString &data) |
95 | | - { |
96 | | - const QRegularExpression regex("QBT_TR\\((([^\\)]|\\)(?!QBT_TR))+)\\)QBT_TR(\\[CONTEXT=([a-zA-Z_][a-zA-Z0-9_]*)\\])"); |
97 | | - const QRegularExpression mnemonic("\\(?&([a-zA-Z]?\\))?"); |
98 | | - |
99 | | - const bool isTranslationNeeded = !locale.startsWith("en") |
100 | | - || locale.startsWith("en_AU") || locale.startsWith("en_GB"); |
101 | | - |
102 | | - int i = 0; |
103 | | - bool found = true; |
104 | | - while (i < data.size() && found) { |
105 | | - QRegularExpressionMatch regexMatch; |
106 | | - i = data.indexOf(regex, i, ®exMatch); |
107 | | - if (i >= 0) { |
108 | | - const QString word = regexMatch.captured(1); |
109 | | - const QString context = regexMatch.captured(4); |
110 | | - |
111 | | - QString translation = isTranslationNeeded |
112 | | - ? qApp->translate(context.toUtf8().constData(), word.toUtf8().constData(), nullptr, 1) |
113 | | - : word; |
114 | | - |
115 | | - // Remove keyboard shortcuts |
116 | | - translation.remove(mnemonic); |
117 | | - |
118 | | - // Use HTML code for quotes to prevent issues with JS |
119 | | - translation.replace('\'', "'"); |
120 | | - translation.replace('\"', """); |
121 | | - |
122 | | - data.replace(i, regexMatch.capturedLength(), translation); |
123 | | - i += translation.length(); |
124 | | - } |
125 | | - else { |
126 | | - found = false; // no more translatable strings |
127 | | - } |
128 | | - |
129 | | - data.replace(QLatin1String("${LANG}"), locale.left(2)); |
130 | | - data.replace(QLatin1String("${VERSION}"), QBT_VERSION); |
131 | | - } |
132 | | - } |
133 | | - |
134 | 93 | inline QUrl urlFromHostHeader(const QString &hostHeader) |
135 | 94 | { |
136 | 95 | if (!hostHeader.contains(QLatin1String("://"))) |
@@ -234,6 +193,43 @@ void WebApplication::sendWebUIFile() |
234 | 193 | sendFile(localPath); |
235 | 194 | } |
236 | 195 |
|
| 196 | +void WebApplication::translateDocument(QString &data) |
| 197 | +{ |
| 198 | + const QRegularExpression regex("QBT_TR\\((([^\\)]|\\)(?!QBT_TR))+)\\)QBT_TR\\[CONTEXT=([a-zA-Z_][a-zA-Z0-9_]*)\\]"); |
| 199 | + |
| 200 | + const bool isTranslationNeeded = !m_currentLocale.startsWith("en") |
| 201 | + || m_currentLocale.startsWith("en_AU") || m_currentLocale.startsWith("en_GB") |
| 202 | + || !m_translator.isEmpty(); |
| 203 | + |
| 204 | + int i = 0; |
| 205 | + bool found = true; |
| 206 | + while (i < data.size() && found) { |
| 207 | + QRegularExpressionMatch regexMatch; |
| 208 | + i = data.indexOf(regex, i, ®exMatch); |
| 209 | + if (i >= 0) { |
| 210 | + const QString word = regexMatch.captured(1); |
| 211 | + const QString context = regexMatch.captured(3); |
| 212 | + |
| 213 | + QString translation = isTranslationNeeded |
| 214 | + ? m_translator.translate(context.toUtf8().constData(), word.toUtf8().constData(), nullptr, 1) |
| 215 | + : word; |
| 216 | + |
| 217 | + // Use HTML code for quotes to prevent issues with JS |
| 218 | + translation.replace('\'', "'"); |
| 219 | + translation.replace('\"', """); |
| 220 | + |
| 221 | + data.replace(i, regexMatch.capturedLength(), translation); |
| 222 | + i += translation.length(); |
| 223 | + } |
| 224 | + else { |
| 225 | + found = false; // no more translatable strings |
| 226 | + } |
| 227 | + |
| 228 | + data.replace(QLatin1String("${LANG}"), m_currentLocale.left(2)); |
| 229 | + data.replace(QLatin1String("${VERSION}"), QBT_VERSION); |
| 230 | + } |
| 231 | +} |
| 232 | + |
237 | 233 | WebSession *WebApplication::session() |
238 | 234 | { |
239 | 235 | return m_currentSession; |
@@ -429,6 +425,14 @@ void WebApplication::configure() |
429 | 425 | if (m_currentLocale != newLocale) { |
430 | 426 | m_currentLocale = newLocale; |
431 | 427 | m_translatedFiles.clear(); |
| 428 | + if (m_translator.load(m_rootFolder + QLatin1String("/translations/webui_") + m_currentLocale)) { |
| 429 | + LogMsg(tr("WebUI translation for selected locale (%1) is successfully loaded.") |
| 430 | + .arg(m_currentLocale)); |
| 431 | + } |
| 432 | + else { |
| 433 | + LogMsg(tr("Couldn't load WebUI translation for selected locale (%1). Falling back to default (en).") |
| 434 | + .arg(m_currentLocale), Log::WARNING); |
| 435 | + } |
432 | 436 | } |
433 | 437 |
|
434 | 438 | m_isLocalAuthEnabled = pref->isWebUiLocalAuthEnabled(); |
@@ -490,7 +494,7 @@ void WebApplication::sendFile(const QString &path) |
490 | 494 | // Translate the file |
491 | 495 | if (isTranslatable) { |
492 | 496 | QString dataStr {data}; |
493 | | - translateDocument(m_currentLocale, dataStr); |
| 497 | + translateDocument(dataStr); |
494 | 498 | data = dataStr.toUtf8(); |
495 | 499 |
|
496 | 500 | m_translatedFiles[path] = {data, lastModified}; // caching translated file |
|
0 commit comments