Skip to content

Commit

Permalink
Merge pull request #9979 from Chocobo1/i18n_bak
Browse files Browse the repository at this point in the history
Unify translation files loading action
  • Loading branch information
Chocobo1 committed Dec 19, 2018
2 parents 6f63805 + e98d4e8 commit 8ab94d7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
27 changes: 12 additions & 15 deletions src/webui/webapplication.cpp
Expand Up @@ -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 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) {
Expand All @@ -212,12 +208,12 @@ void WebApplication::translateDocument(QString &data)
const QString sourceText = regexMatch.captured(1);
const QString context = regexMatch.captured(3);

QString translation = sourceText;
if (isTranslationNeeded) {
const QString loadedText = m_translator.translate(context.toUtf8().constData(), sourceText.toUtf8().constData(), nullptr, 1);
if (!loadedText.isEmpty())
translation = loadedText;
}
const QString loadedText = m_translationFileLoaded
? m_translator.translate(context.toUtf8().constData(), sourceText.toUtf8().constData())
: QString();
// `loadedText` is empty when translation is not provided
// it should fallback to `sourceText`
QString translation = loadedText.isEmpty() ? sourceText : loadedText;

// Use HTML code for quotes to prevent issues with JS
translation.replace('\'', "&#39;");
Expand Down Expand Up @@ -436,13 +432,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("Web UI translation for selected locale (%1) is successfully loaded.")
.arg(m_currentLocale));

m_translationFileLoaded = m_translator.load(m_rootFolder + QLatin1String("/translations/webui_") + newLocale);
if (m_translationFileLoaded) {
LogMsg(tr("Web UI translation for selected locale (%1) has been successfully loaded.")
.arg(newLocale));
}
else {
LogMsg(tr("Couldn't load Web UI translation for selected locale (%1). Falling back to default (en).")
.arg(m_currentLocale), Log::WARNING);
LogMsg(tr("Couldn't load Web UI translation for selected locale (%1).").arg(newLocale), Log::WARNING);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/webui/webapplication.h
Expand Up @@ -146,6 +146,7 @@ class WebApplication
QMap<QString, TranslatedFile> m_translatedFiles;
QString m_currentLocale;
QTranslator m_translator;
bool m_translationFileLoaded = false;

bool m_isLocalAuthEnabled;
bool m_isAuthSubnetWhitelistEnabled;
Expand Down
2 changes: 1 addition & 1 deletion src/webui/www/translations/webui_en.ts
Expand Up @@ -5,7 +5,7 @@
<name>AboutDlg</name>
<message>
<source>About</source>
<translation type="unfinished" />
<translation>About</translation>
</message>
</context>
<context>
Expand Down

0 comments on commit 8ab94d7

Please sign in to comment.