From d956c79291c3d39cf55c90afd40532759d51337d Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 30 Jan 2020 19:06:33 +0100 Subject: [PATCH] [libcalamares] Load translations from more places - To support translation testing, without needing to recompile Calamares, load files from the local directory when debugging, or from /usr/share/calamares/lang/ in general. - This allows updating translations and testing them with just lrelease (a translation build tool) installed, without rebuilding Calamares. - This allows distro's to ship updated or modified translations without rebuilding Calamares. --- src/libcalamares/utils/Retranslator.cpp | 49 ++++++++++++++++--------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/src/libcalamares/utils/Retranslator.cpp b/src/libcalamares/utils/Retranslator.cpp index 4c7c3b3f5..85dfb62b6 100644 --- a/src/libcalamares/utils/Retranslator.cpp +++ b/src/libcalamares/utils/Retranslator.cpp @@ -18,7 +18,9 @@ #include "Retranslator.h" -#include "Logger.h" +#include "Settings.h" +#include "utils/Dirs.h" +#include "utils/Logger.h" #include #include @@ -125,34 +127,47 @@ BrandingLoader::tryLoad( QTranslator* translator ) return false; } -bool -CalamaresLoader::tryLoad( QTranslator* translator ) +static bool +tryLoad( QTranslator* translator, const QString& prefix, const QString& localeName ) { - if ( translator->load( QString( ":/lang/calamares_" ) + m_localeName ) ) + // In debug-mode, try loading from the current directory + if ( Calamares::Settings::instance() && Calamares::Settings::instance()->debugMode() && translator->load( prefix + localeName ) ) { - cDebug() << Logger::SubEntry << "Calamares using locale:" << m_localeName; + cDebug() << Logger::SubEntry << "Loaded local translation" << prefix << localeName; + return true; + } + + // Or load from appDataDir -- often /usr/share/calamares -- subdirectory land/ + QDir localeData( CalamaresUtils::appDataDir() ); + if ( localeData.exists() && translator->load( localeData.absolutePath() + QStringLiteral("/lang/") + prefix + localeName) ) + { + cDebug() << Logger::SubEntry << "Loaded appdata translation" << prefix << localeName; + return true; + } + + // Or from QRC (most common) + if ( translator->load( QStringLiteral( ":/lang/") + prefix + localeName ) ) + { + cDebug() << Logger::SubEntry << "Loaded QRC translation" << prefix << localeName; return true; } else { - cDebug() << Logger::SubEntry << "Calamares using default, system locale not found:" << m_localeName; - return translator->load( QString( ":/lang/calamares_en" ) ); + cDebug() << Logger::SubEntry << "No translation for" << prefix << localeName << "using default (en)"; + return translator->load( QStringLiteral( ":/lang/" ) + prefix + QStringLiteral( "en" ) ); } } +bool +CalamaresLoader::tryLoad( QTranslator* translator ) +{ + return ::tryLoad( translator, QStringLiteral( "calamares_" ), m_localeName ); +} + bool TZLoader::tryLoad( QTranslator* translator ) { - if ( translator->load( QString( ":/lang/tz_" ) + m_localeName ) ) - { - cDebug() << Logger::SubEntry << "Calamares Timezones using locale:" << m_localeName; - return true; - } - else - { - cDebug() << Logger::SubEntry << "Calamares Timezones using default, system locale not found:" << m_localeName; - return translator->load( QString( ":/lang/tz_en" ) ); - } + return ::tryLoad( translator, QStringLiteral( "tz_" ), m_localeName ); } static void