From 92a4e73a22371e5260ab090b767494269956ace8 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 11 May 2018 20:45:00 +0800 Subject: [PATCH 1/2] Apply locale changes immediately in WebUI --- src/webui/webapplication.cpp | 16 +++++++++++----- src/webui/webapplication.h | 1 + 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/webui/webapplication.cpp b/src/webui/webapplication.cpp index 0ad1ab1ac..ca932eb3f 100644 --- a/src/webui/webapplication.cpp +++ b/src/webui/webapplication.cpp @@ -95,15 +95,15 @@ namespace return ret; } - void translateDocument(QString &data) + void translateDocument(const QString &locale, QString &data) { const QRegExp regex("QBT_TR\\((([^\\)]|\\)(?!QBT_TR))+)\\)QBT_TR(\\[CONTEXT=([a-zA-Z_][a-zA-Z0-9_]*)\\])"); const QRegExp mnemonic("\\(?&([a-zA-Z]?\\))?"); int i = 0; bool found = true; - const QString locale = Preferences::instance()->getLocale(); - bool isTranslationNeeded = !locale.startsWith("en") || locale.startsWith("en_AU") || locale.startsWith("en_GB"); + bool isTranslationNeeded = !locale.startsWith("en") + || locale.startsWith("en_AU") || locale.startsWith("en_GB"); while (i < data.size() && found) { i = regex.indexIn(data, i); @@ -420,7 +420,7 @@ void WebApplication::configure() { const auto pref = Preferences::instance(); - m_domainList = Preferences::instance()->getServerDomains().split(';', QString::SkipEmptyParts); + m_domainList = pref->getServerDomains().split(';', QString::SkipEmptyParts); std::for_each(m_domainList.begin(), m_domainList.end(), [](QString &entry) { entry = entry.trimmed(); }); const QString rootFolder = Utils::Fs::expandPathAbs( @@ -429,6 +429,12 @@ void WebApplication::configure() m_translatedFiles.clear(); m_rootFolder = rootFolder; } + + const QString newLocale = pref->getLocale(); + if (m_currentLocale != newLocale) { + m_currentLocale = newLocale; + m_translatedFiles.clear(); + } } void WebApplication::registerAPIController(const QString &scope, APIController *controller) @@ -476,7 +482,7 @@ void WebApplication::sendFile(const QString &path) // Translate the file if (isTranslatable) { QString dataStr {data}; - translateDocument(dataStr); + translateDocument(m_currentLocale, dataStr); data = dataStr.toUtf8(); m_translatedFiles[path] = {data, lastModified}; // caching translated file diff --git a/src/webui/webapplication.h b/src/webui/webapplication.h index 1d613eccb..509927993 100644 --- a/src/webui/webapplication.h +++ b/src/webui/webapplication.h @@ -141,4 +141,5 @@ private: QDateTime lastModified; }; QMap m_translatedFiles; + QString m_currentLocale; }; From 5ae926a3769db4cfeecaf47940feb6f03274aba0 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 11 May 2018 21:34:08 +0800 Subject: [PATCH 2/2] Refactor code Add const to variables. No functionality change. --- src/webui/webapplication.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/webui/webapplication.cpp b/src/webui/webapplication.cpp index ca932eb3f..f2c25aa30 100644 --- a/src/webui/webapplication.cpp +++ b/src/webui/webapplication.cpp @@ -99,29 +99,28 @@ namespace { const QRegExp regex("QBT_TR\\((([^\\)]|\\)(?!QBT_TR))+)\\)QBT_TR(\\[CONTEXT=([a-zA-Z_][a-zA-Z0-9_]*)\\])"); const QRegExp mnemonic("\\(?&([a-zA-Z]?\\))?"); - int i = 0; - bool found = true; - bool isTranslationNeeded = !locale.startsWith("en") + 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) { i = regex.indexIn(data, i); if (i >= 0) { - //qDebug("Found translatable string: %s", regex.cap(1).toUtf8().data()); - QByteArray word = regex.cap(1).toUtf8(); + const QString word = regex.cap(1); + const QString context = regex.cap(4); + + QString translation = isTranslationNeeded + ? qApp->translate(context.toUtf8().constData(), word.toUtf8().constData(), nullptr, 1) + : word; - QString translation = word; - if (isTranslationNeeded) { - QString context = regex.cap(4); - translation = qApp->translate(context.toUtf8().constData(), word.constData(), nullptr, 1); - } // Remove keyboard shortcuts translation.replace(mnemonic, ""); // Use HTML code for quotes to prevent issues with JS - translation.replace("'", "'"); - translation.replace("\"", """); + translation.replace('\'', "'"); + translation.replace('\"', """); data.replace(i, regex.matchedLength(), translation); i += translation.length();