1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-02-04 10:54:35 +00:00

Merge pull request #8895 from Chocobo1/locale

Apply locale changes immediately in WebUI
This commit is contained in:
Mike Tzou 2018-05-15 00:29:45 +08:00 committed by GitHub
commit 3b1fa19ea8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 16 deletions

View File

@ -95,33 +95,32 @@ namespace
return ret; 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 regex("QBT_TR\\((([^\\)]|\\)(?!QBT_TR))+)\\)QBT_TR(\\[CONTEXT=([a-zA-Z_][a-zA-Z0-9_]*)\\])");
const QRegExp mnemonic("\\(?&([a-zA-Z]?\\))?"); const QRegExp mnemonic("\\(?&([a-zA-Z]?\\))?");
const bool isTranslationNeeded = !locale.startsWith("en")
|| locale.startsWith("en_AU") || locale.startsWith("en_GB");
int i = 0; int i = 0;
bool found = true; bool found = true;
const QString locale = Preferences::instance()->getLocale();
bool isTranslationNeeded = !locale.startsWith("en") || locale.startsWith("en_AU") || locale.startsWith("en_GB");
while (i < data.size() && found) { while (i < data.size() && found) {
i = regex.indexIn(data, i); i = regex.indexIn(data, i);
if (i >= 0) { if (i >= 0) {
//qDebug("Found translatable string: %s", regex.cap(1).toUtf8().data()); const QString word = regex.cap(1);
QByteArray word = regex.cap(1).toUtf8(); 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 // Remove keyboard shortcuts
translation.replace(mnemonic, ""); translation.replace(mnemonic, "");
// Use HTML code for quotes to prevent issues with JS // Use HTML code for quotes to prevent issues with JS
translation.replace("'", "&#39;"); translation.replace('\'', "&#39;");
translation.replace("\"", "&#34;"); translation.replace('\"', "&#34;");
data.replace(i, regex.matchedLength(), translation); data.replace(i, regex.matchedLength(), translation);
i += translation.length(); i += translation.length();
@ -414,7 +413,7 @@ void WebApplication::configure()
{ {
const auto pref = Preferences::instance(); 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(); }); std::for_each(m_domainList.begin(), m_domainList.end(), [](QString &entry) { entry = entry.trimmed(); });
const QString rootFolder = Utils::Fs::expandPathAbs( const QString rootFolder = Utils::Fs::expandPathAbs(
@ -423,6 +422,12 @@ void WebApplication::configure()
m_translatedFiles.clear(); m_translatedFiles.clear();
m_rootFolder = rootFolder; 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) void WebApplication::registerAPIController(const QString &scope, APIController *controller)
@ -470,7 +475,7 @@ void WebApplication::sendFile(const QString &path)
// Translate the file // Translate the file
if (isTranslatable) { if (isTranslatable) {
QString dataStr {data}; QString dataStr {data};
translateDocument(dataStr); translateDocument(m_currentLocale, dataStr);
data = dataStr.toUtf8(); data = dataStr.toUtf8();
m_translatedFiles[path] = {data, lastModified}; // caching translated file m_translatedFiles[path] = {data, lastModified}; // caching translated file

View File

@ -141,4 +141,5 @@ private:
QDateTime lastModified; QDateTime lastModified;
}; };
QMap<QString, TranslatedFile> m_translatedFiles; QMap<QString, TranslatedFile> m_translatedFiles;
QString m_currentLocale;
}; };