Permalink
Browse files

Use independent translation for WebUI

  • Loading branch information...
glassez committed Jun 7, 2018
1 parent becb002 commit c1a4ef13776d5dbdaddff3938a68d64df24b121e
Showing with 92,461 additions and 178 deletions.
  1. +1 −0 .gitignore
  2. +8 −7 src/lang/lang.pri
  3. +0 −1 src/webui/CMakeLists.txt
  4. +0 −97 src/webui/extra_translations.h
  5. +46 −42 src/webui/webapplication.cpp
  6. +4 −0 src/webui/webapplication.h
  7. +22 −1 src/webui/webui.pri
  8. +22 −22 src/webui/www/private/index.html
  9. +8 −8 src/webui/www/private/preferences_content.html
  10. +1,846 −0 src/webui/www/translations/webui_ar.ts
  11. +1,846 −0 src/webui/www/translations/webui_be.ts
  12. +1,846 −0 src/webui/www/translations/webui_bg.ts
  13. +1,846 −0 src/webui/www/translations/webui_ca.ts
  14. +1,846 −0 src/webui/www/translations/webui_cs.ts
  15. +1,846 −0 src/webui/www/translations/webui_da.ts
  16. +1,846 −0 src/webui/www/translations/webui_de.ts
  17. +1,846 −0 src/webui/www/translations/webui_el.ts
  18. +1,846 −0 src/webui/www/translations/webui_en.ts
  19. +1,846 −0 src/webui/www/translations/webui_en_AU.ts
  20. +1,846 −0 src/webui/www/translations/webui_en_GB.ts
  21. +1,846 −0 src/webui/www/translations/webui_eo.ts
  22. +1,846 −0 src/webui/www/translations/webui_es.ts
  23. +1,846 −0 src/webui/www/translations/webui_eu.ts
  24. +1,846 −0 src/webui/www/translations/webui_fi.ts
  25. +1,846 −0 src/webui/www/translations/webui_fr.ts
  26. +1,846 −0 src/webui/www/translations/webui_gl.ts
  27. +1,846 −0 src/webui/www/translations/webui_he.ts
  28. +1,846 −0 src/webui/www/translations/webui_hi_IN.ts
  29. +1,846 −0 src/webui/www/translations/webui_hr.ts
  30. +1,846 −0 src/webui/www/translations/webui_hu.ts
  31. +1,846 −0 src/webui/www/translations/webui_hy.ts
  32. +1,846 −0 src/webui/www/translations/webui_id.ts
  33. +1,846 −0 src/webui/www/translations/webui_is.ts
  34. +1,846 −0 src/webui/www/translations/webui_it.ts
  35. +1,846 −0 src/webui/www/translations/webui_ja.ts
  36. +1,846 −0 src/webui/www/translations/webui_ka.ts
  37. +1,846 −0 src/webui/www/translations/webui_ko.ts
  38. +1,846 −0 src/webui/www/translations/webui_lt.ts
  39. +1,846 −0 src/webui/www/translations/webui_lv_LV.ts
  40. +1,846 −0 src/webui/www/translations/webui_ms_MY.ts
  41. +1,846 −0 src/webui/www/translations/webui_nb.ts
  42. +1,846 −0 src/webui/www/translations/webui_nl.ts
  43. +1,846 −0 src/webui/www/translations/webui_oc.ts
  44. +1,846 −0 src/webui/www/translations/webui_pl.ts
  45. +1,846 −0 src/webui/www/translations/webui_pt_BR.ts
  46. +1,846 −0 src/webui/www/translations/webui_pt_PT.ts
  47. +1,846 −0 src/webui/www/translations/webui_ro.ts
  48. +1,846 −0 src/webui/www/translations/webui_ru.ts
  49. +1,846 −0 src/webui/www/translations/webui_sk.ts
  50. +1,846 −0 src/webui/www/translations/webui_sl.ts
  51. +1,846 −0 src/webui/www/translations/webui_sr.ts
  52. +1,846 −0 src/webui/www/translations/webui_sv.ts
  53. +1,846 −0 src/webui/www/translations/webui_tr.ts
  54. +1,846 −0 src/webui/www/translations/webui_uk.ts
  55. +1,846 −0 src/webui/www/translations/webui_uz@Latn.ts
  56. +1,846 −0 src/webui/www/translations/webui_vi.ts
  57. +1,846 −0 src/webui/www/translations/webui_zh.ts
  58. +1,846 −0 src/webui/www/translations/webui_zh_HK.ts
  59. +1,846 −0 src/webui/www/translations/webui_zh_TW.ts
  60. +50 −0 src/webui/www/webui.qrc
@@ -22,6 +22,7 @@ qrc_*.cpp
ui_*.h
*.moc
src/lang/qbittorrent_*.qm
src/webui/www/translations/webui_*.qm
.DS_Store
.qmake.stash
src/qbittorrent.app
@@ -7,14 +7,15 @@ for(file, TS_FILES) {
}
isEmpty(QMAKE_LRELEASE) {
win32:QMAKE_LRELEASE = $$[QT_INSTALL_BINS]\\lrelease.exe
else:QMAKE_LRELEASE = $$[QT_INSTALL_BINS]/lrelease
win32: QMAKE_LRELEASE = $$[QT_INSTALL_BINS]/lrelease.exe
else: QMAKE_LRELEASE = $$[QT_INSTALL_BINS]/lrelease
unix {
equals(QT_MAJOR_VERSION, 5) {
!exists($$QMAKE_LRELEASE) { QMAKE_LRELEASE = lrelease-qt5 }
}
} else {
!exists($$QMAKE_LRELEASE) { QMAKE_LRELEASE = lrelease }
equals(QT_MAJOR_VERSION, 5) {
!exists($$QMAKE_LRELEASE): QMAKE_LRELEASE = lrelease-qt5
}
}
else {
!exists($$QMAKE_LRELEASE): QMAKE_LRELEASE = lrelease
}
}
@@ -11,7 +11,6 @@ api/synccontroller.h
api/torrentscontroller.h
api/transfercontroller.h
api/serialize/serialize_torrent.h
extra_translations.h
webapplication.h
webui.h

This file was deleted.

Oops, something went wrong.
@@ -34,7 +34,6 @@
#include <stdexcept>
#include <vector>
#include <QCoreApplication>
#include <QDateTime>
#include <QDebug>
#include <QFile>
@@ -91,46 +90,6 @@ namespace
return ret;
}
void translateDocument(const QString &locale, QString &data)
{
const QRegularExpression regex("QBT_TR\\((([^\\)]|\\)(?!QBT_TR))+)\\)QBT_TR(\\[CONTEXT=([a-zA-Z_][a-zA-Z0-9_]*)\\])");
const QRegularExpression mnemonic("\\(?&([a-zA-Z]?\\))?");
const bool isTranslationNeeded = !locale.startsWith("en")
|| locale.startsWith("en_AU") || locale.startsWith("en_GB");
int i = 0;
bool found = true;
while (i < data.size() && found) {
QRegularExpressionMatch regexMatch;
i = data.indexOf(regex, i, &regexMatch);
if (i >= 0) {
const QString word = regexMatch.captured(1);
const QString context = regexMatch.captured(4);
QString translation = isTranslationNeeded
? qApp->translate(context.toUtf8().constData(), word.toUtf8().constData(), nullptr, 1)
: word;
// Remove keyboard shortcuts
translation.remove(mnemonic);
// Use HTML code for quotes to prevent issues with JS
translation.replace('\'', "&#39;");
translation.replace('\"', "&#34;");
data.replace(i, regexMatch.capturedLength(), translation);
i += translation.length();
}
else {
found = false; // no more translatable strings
}
data.replace(QLatin1String("${LANG}"), locale.left(2));
data.replace(QLatin1String("${VERSION}"), QBT_VERSION);
}
}
inline QUrl urlFromHostHeader(const QString &hostHeader)
{
if (!hostHeader.contains(QLatin1String("://")))
@@ -234,6 +193,43 @@ void WebApplication::sendWebUIFile()
sendFile(localPath);
}
void WebApplication::translateDocument(QString &data)
{
const QRegularExpression regex("QBT_TR\\((([^\\)]|\\)(?!QBT_TR))+)\\)QBT_TR\\[CONTEXT=([a-zA-Z_][a-zA-Z0-9_]*)\\]");
const bool isTranslationNeeded = !m_currentLocale.startsWith("en")
|| m_currentLocale.startsWith("en_AU") || m_currentLocale.startsWith("en_GB")
|| !m_translator.isEmpty();
int i = 0;
bool found = true;
while (i < data.size() && found) {
QRegularExpressionMatch regexMatch;
i = data.indexOf(regex, i, &regexMatch);
if (i >= 0) {
const QString word = regexMatch.captured(1);
const QString context = regexMatch.captured(3);
QString translation = isTranslationNeeded
? m_translator.translate(context.toUtf8().constData(), word.toUtf8().constData(), nullptr, 1)
: word;
// Use HTML code for quotes to prevent issues with JS
translation.replace('\'', "&#39;");
translation.replace('\"', "&#34;");
data.replace(i, regexMatch.capturedLength(), translation);
i += translation.length();
}
else {
found = false; // no more translatable strings
}
data.replace(QLatin1String("${LANG}"), m_currentLocale.left(2));
data.replace(QLatin1String("${VERSION}"), QBT_VERSION);
}
}
WebSession *WebApplication::session()
{
return m_currentSession;
@@ -429,6 +425,14 @@ void WebApplication::configure()
if (m_currentLocale != newLocale) {
m_currentLocale = newLocale;
m_translatedFiles.clear();
if (m_translator.load(m_rootFolder + QLatin1String("/translations/webui_") + m_currentLocale)) {
LogMsg(tr("WebUI translation for selected locale (%1) is successfully loaded.")
.arg(m_currentLocale));
}
else {
LogMsg(tr("Couldn't load WebUI translation for selected locale (%1). Falling back to default (en).")
.arg(m_currentLocale), Log::WARNING);
}
}
m_isLocalAuthEnabled = pref->isWebUiLocalAuthEnabled();
@@ -490,7 +494,7 @@ void WebApplication::sendFile(const QString &path)
// Translate the file
if (isTranslatable) {
QString dataStr {data};
translateDocument(m_currentLocale, dataStr);
translateDocument(dataStr);
data = dataStr.toUtf8();
m_translatedFiles[path] = {data, lastModified}; // caching translated file
@@ -34,6 +34,7 @@
#include <QObject>
#include <QRegularExpression>
#include <QSet>
#include <QTranslator>
#include "api/isessionmanager.h"
#include "base/http/irequesthandler.h"
@@ -109,6 +110,8 @@ class WebApplication
void sendFile(const QString &path);
void sendWebUIFile();
void translateDocument(QString &data);
// Session management
QString generateSid() const;
void sessionInitialize();
@@ -142,6 +145,7 @@ class WebApplication
};
QMap<QString, TranslatedFile> m_translatedFiles;
QString m_currentLocale;
QTranslator m_translator;
bool m_isLocalAuthEnabled;
bool m_isAuthSubnetWhitelistEnabled;
@@ -10,7 +10,6 @@ HEADERS += \
$$PWD/api/torrentscontroller.h \
$$PWD/api/transfercontroller.h \
$$PWD/api/serialize/serialize_torrent.h \
$$PWD/extra_translations.h \
$$PWD/webapplication.h \
$$PWD/webui.h
@@ -29,3 +28,25 @@ SOURCES += \
$$PWD/webui.cpp
RESOURCES += $$PWD/www/webui.qrc
# WebUI Translation
isEmpty(QMAKE_LRELEASE) {
win32: QMAKE_LRELEASE = $$[QT_INSTALL_BINS]/lrelease.exe
else: QMAKE_LRELEASE = $$[QT_INSTALL_BINS]/lrelease
unix {
equals(QT_MAJOR_VERSION, 5) {
!exists($$QMAKE_LRELEASE): QMAKE_LRELEASE = lrelease-qt5
}
}
else {
!exists($$QMAKE_LRELEASE): QMAKE_LRELEASE = lrelease
}
}
WEBUI_TRANSLATIONS = $$files(www/translations/webui_*.ts)
WEBUI_TRANSLATIONS_NOEXT = $$replace(WEBUI_TRANSLATIONS, ".ts", "")
message("Building WebUI translations...")
for(L, WEBUI_TRANSLATIONS_NOEXT) {
message("Processing $${L}")
system("$$QMAKE_LRELEASE -silent $${L}.ts -qm $${L}.qm")
!exists("$${L}.qm"): error("Building WebUI translations failed, cannot continue!")
}
Oops, something went wrong.

0 comments on commit c1a4ef1

Please sign in to comment.