Skip to content

Commit c1a4ef1

Browse files
committed
Use independent translation for WebUI
1 parent becb002 commit c1a4ef1

60 files changed

Lines changed: 92461 additions & 178 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ qrc_*.cpp
2222
ui_*.h
2323
*.moc
2424
src/lang/qbittorrent_*.qm
25+
src/webui/www/translations/webui_*.qm
2526
.DS_Store
2627
.qmake.stash
2728
src/qbittorrent.app

src/lang/lang.pri

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ for(file, TS_FILES) {
77
}
88

99
isEmpty(QMAKE_LRELEASE) {
10-
win32:QMAKE_LRELEASE = $$[QT_INSTALL_BINS]\\lrelease.exe
11-
else:QMAKE_LRELEASE = $$[QT_INSTALL_BINS]/lrelease
10+
win32: QMAKE_LRELEASE = $$[QT_INSTALL_BINS]/lrelease.exe
11+
else: QMAKE_LRELEASE = $$[QT_INSTALL_BINS]/lrelease
1212
unix {
13-
equals(QT_MAJOR_VERSION, 5) {
14-
!exists($$QMAKE_LRELEASE) { QMAKE_LRELEASE = lrelease-qt5 }
15-
}
16-
} else {
17-
!exists($$QMAKE_LRELEASE) { QMAKE_LRELEASE = lrelease }
13+
equals(QT_MAJOR_VERSION, 5) {
14+
!exists($$QMAKE_LRELEASE): QMAKE_LRELEASE = lrelease-qt5
15+
}
16+
}
17+
else {
18+
!exists($$QMAKE_LRELEASE): QMAKE_LRELEASE = lrelease
1819
}
1920
}
2021

src/webui/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ api/synccontroller.h
1111
api/torrentscontroller.h
1212
api/transfercontroller.h
1313
api/serialize/serialize_torrent.h
14-
extra_translations.h
1514
webapplication.h
1615
webui.h
1716

src/webui/extra_translations.h

Lines changed: 0 additions & 97 deletions
This file was deleted.

src/webui/webapplication.cpp

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include <stdexcept>
3535
#include <vector>
3636

37-
#include <QCoreApplication>
3837
#include <QDateTime>
3938
#include <QDebug>
4039
#include <QFile>
@@ -91,46 +90,6 @@ namespace
9190
return ret;
9291
}
9392

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, &regexMatch);
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('\'', "&#39;");
120-
translation.replace('\"', "&#34;");
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-
13493
inline QUrl urlFromHostHeader(const QString &hostHeader)
13594
{
13695
if (!hostHeader.contains(QLatin1String("://")))
@@ -234,6 +193,43 @@ void WebApplication::sendWebUIFile()
234193
sendFile(localPath);
235194
}
236195

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, &regexMatch);
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('\'', "&#39;");
219+
translation.replace('\"', "&#34;");
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+
237233
WebSession *WebApplication::session()
238234
{
239235
return m_currentSession;
@@ -429,6 +425,14 @@ void WebApplication::configure()
429425
if (m_currentLocale != newLocale) {
430426
m_currentLocale = newLocale;
431427
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+
}
432436
}
433437

434438
m_isLocalAuthEnabled = pref->isWebUiLocalAuthEnabled();
@@ -490,7 +494,7 @@ void WebApplication::sendFile(const QString &path)
490494
// Translate the file
491495
if (isTranslatable) {
492496
QString dataStr {data};
493-
translateDocument(m_currentLocale, dataStr);
497+
translateDocument(dataStr);
494498
data = dataStr.toUtf8();
495499

496500
m_translatedFiles[path] = {data, lastModified}; // caching translated file

src/webui/webapplication.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <QObject>
3535
#include <QRegularExpression>
3636
#include <QSet>
37+
#include <QTranslator>
3738

3839
#include "api/isessionmanager.h"
3940
#include "base/http/irequesthandler.h"
@@ -109,6 +110,8 @@ class WebApplication
109110
void sendFile(const QString &path);
110111
void sendWebUIFile();
111112

113+
void translateDocument(QString &data);
114+
112115
// Session management
113116
QString generateSid() const;
114117
void sessionInitialize();
@@ -142,6 +145,7 @@ class WebApplication
142145
};
143146
QMap<QString, TranslatedFile> m_translatedFiles;
144147
QString m_currentLocale;
148+
QTranslator m_translator;
145149

146150
bool m_isLocalAuthEnabled;
147151
bool m_isAuthSubnetWhitelistEnabled;

src/webui/webui.pri

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ HEADERS += \
1010
$$PWD/api/torrentscontroller.h \
1111
$$PWD/api/transfercontroller.h \
1212
$$PWD/api/serialize/serialize_torrent.h \
13-
$$PWD/extra_translations.h \
1413
$$PWD/webapplication.h \
1514
$$PWD/webui.h
1615

@@ -29,3 +28,25 @@ SOURCES += \
2928
$$PWD/webui.cpp
3029

3130
RESOURCES += $$PWD/www/webui.qrc
31+
32+
# WebUI Translation
33+
isEmpty(QMAKE_LRELEASE) {
34+
win32: QMAKE_LRELEASE = $$[QT_INSTALL_BINS]/lrelease.exe
35+
else: QMAKE_LRELEASE = $$[QT_INSTALL_BINS]/lrelease
36+
unix {
37+
equals(QT_MAJOR_VERSION, 5) {
38+
!exists($$QMAKE_LRELEASE): QMAKE_LRELEASE = lrelease-qt5
39+
}
40+
}
41+
else {
42+
!exists($$QMAKE_LRELEASE): QMAKE_LRELEASE = lrelease
43+
}
44+
}
45+
WEBUI_TRANSLATIONS = $$files(www/translations/webui_*.ts)
46+
WEBUI_TRANSLATIONS_NOEXT = $$replace(WEBUI_TRANSLATIONS, ".ts", "")
47+
message("Building WebUI translations...")
48+
for(L, WEBUI_TRANSLATIONS_NOEXT) {
49+
message("Processing $${L}")
50+
system("$$QMAKE_LRELEASE -silent $${L}.ts -qm $${L}.qm")
51+
!exists("$${L}.qm"): error("Building WebUI translations failed, cannot continue!")
52+
}

0 commit comments

Comments
 (0)