Browse Source

Merge pull request #9979 from Chocobo1/i18n_bak

Unify translation files loading action
adaptive-webui-19844
Mike Tzou 6 years ago committed by GitHub
parent
commit
8ab94d7704
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      src/webui/webapplication.cpp
  2. 1
      src/webui/webapplication.h
  3. 2
      src/webui/www/translations/webui_en.ts

27
src/webui/webapplication.cpp

@ -199,10 +199,6 @@ void WebApplication::translateDocument(QString &data)
{ {
const QRegularExpression regex("QBT_TR\\((([^\\)]|\\)(?!QBT_TR))+)\\)QBT_TR\\[CONTEXT=([a-zA-Z_][a-zA-Z0-9_]*)\\]"); 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; int i = 0;
bool found = true; bool found = true;
while (i < data.size() && found) { while (i < data.size() && found) {
@ -212,12 +208,12 @@ void WebApplication::translateDocument(QString &data)
const QString sourceText = regexMatch.captured(1); const QString sourceText = regexMatch.captured(1);
const QString context = regexMatch.captured(3); const QString context = regexMatch.captured(3);
QString translation = sourceText; const QString loadedText = m_translationFileLoaded
if (isTranslationNeeded) { ? m_translator.translate(context.toUtf8().constData(), sourceText.toUtf8().constData())
const QString loadedText = m_translator.translate(context.toUtf8().constData(), sourceText.toUtf8().constData(), nullptr, 1); : QString();
if (!loadedText.isEmpty()) // `loadedText` is empty when translation is not provided
translation = loadedText; // it should fallback to `sourceText`
} QString translation = loadedText.isEmpty() ? sourceText : loadedText;
// 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;");
@ -436,13 +432,14 @@ void WebApplication::configure()
if (m_currentLocale != newLocale) { if (m_currentLocale != newLocale) {
m_currentLocale = newLocale; m_currentLocale = newLocale;
m_translatedFiles.clear(); m_translatedFiles.clear();
if (m_translator.load(m_rootFolder + QLatin1String("/translations/webui_") + m_currentLocale)) {
LogMsg(tr("Web UI translation for selected locale (%1) is successfully loaded.") m_translationFileLoaded = m_translator.load(m_rootFolder + QLatin1String("/translations/webui_") + newLocale);
.arg(m_currentLocale)); if (m_translationFileLoaded) {
LogMsg(tr("Web UI translation for selected locale (%1) has been successfully loaded.")
.arg(newLocale));
} }
else { else {
LogMsg(tr("Couldn't load Web UI translation for selected locale (%1). Falling back to default (en).") LogMsg(tr("Couldn't load Web UI translation for selected locale (%1).").arg(newLocale), Log::WARNING);
.arg(m_currentLocale), Log::WARNING);
} }
} }

1
src/webui/webapplication.h

@ -146,6 +146,7 @@ private:
QMap<QString, TranslatedFile> m_translatedFiles; QMap<QString, TranslatedFile> m_translatedFiles;
QString m_currentLocale; QString m_currentLocale;
QTranslator m_translator; QTranslator m_translator;
bool m_translationFileLoaded = false;
bool m_isLocalAuthEnabled; bool m_isLocalAuthEnabled;
bool m_isAuthSubnetWhitelistEnabled; bool m_isAuthSubnetWhitelistEnabled;

2
src/webui/www/translations/webui_en.ts

@ -5,7 +5,7 @@
<name>AboutDlg</name> <name>AboutDlg</name>
<message> <message>
<source>About</source> <source>About</source>
<translation type="unfinished" /> <translation>About</translation>
</message> </message>
</context> </context>
<context> <context>

Loading…
Cancel
Save