[calamares] Local translations can be a separate setting

- Don't stick this in Settings, though, it becomes overly complicated.
main
Adriaan de Groot
parent 24c2c435a0
commit 4b3f7eb209

@ -23,6 +23,7 @@
#include "Settings.h" #include "Settings.h"
#include "utils/Dirs.h" #include "utils/Dirs.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include "utils/Retranslator.h"
#include "3rdparty/kdsingleapplicationguard/kdsingleapplicationguard.h" #include "3rdparty/kdsingleapplicationguard/kdsingleapplicationguard.h"
@ -57,6 +58,9 @@ handle_args( CalamaresApplication& a )
"Also look in current directory for configuration. Implies -D8." ); "Also look in current directory for configuration. Implies -D8." );
QCommandLineOption debugLevelOption( QCommandLineOption debugLevelOption(
QStringLiteral( "D" ), "Verbose output for debugging purposes (0-8).", "level" ); QStringLiteral( "D" ), "Verbose output for debugging purposes (0-8).", "level" );
QCommandLineOption debugTxOption( QStringList { "T", "debug-translation" },
"Also look in the current directory for translation." );
QCommandLineOption configOption( QCommandLineOption configOption(
QStringList { "c", "config" }, "Configuration directory to use, for testing purposes.", "config" ); QStringList { "c", "config" }, "Configuration directory to use, for testing purposes.", "config" );
QCommandLineOption xdgOption( QStringList { "X", "xdg-config" }, "Use XDG_{CONFIG,DATA}_DIRS as well." ); QCommandLineOption xdgOption( QStringList { "X", "xdg-config" }, "Use XDG_{CONFIG,DATA}_DIRS as well." );
@ -70,6 +74,7 @@ handle_args( CalamaresApplication& a )
parser.addOption( debugLevelOption ); parser.addOption( debugLevelOption );
parser.addOption( configOption ); parser.addOption( configOption );
parser.addOption( xdgOption ); parser.addOption( xdgOption );
parser.addOption( debugTxOption );
parser.process( a ); parser.process( a );
@ -82,7 +87,7 @@ handle_args( CalamaresApplication& a )
{ {
CalamaresUtils::setXdgDirs(); CalamaresUtils::setXdgDirs();
} }
CalamaresUtils::setAllowLocalTranslation( parser.isSet( debugOption ) || parser.isSet( debugTxOption ) );
Calamares::Settings::init( parser.isSet( debugOption ) ); Calamares::Settings::init( parser.isSet( debugOption ) );
a.init(); a.init();
} }

@ -27,6 +27,8 @@
#include <QEvent> #include <QEvent>
#include <QTranslator> #include <QTranslator>
static bool s_allowLocalTranslations = false;
/** @brief Helper class for loading translations /** @brief Helper class for loading translations
* *
* This is used by the loadSingletonTranslator() function to hand off * This is used by the loadSingletonTranslator() function to hand off
@ -131,7 +133,7 @@ static bool
tryLoad( QTranslator* translator, const QString& prefix, const QString& localeName ) tryLoad( QTranslator* translator, const QString& prefix, const QString& localeName )
{ {
// In debug-mode, try loading from the current directory // In debug-mode, try loading from the current directory
if ( Calamares::Settings::instance() && Calamares::Settings::instance()->debugMode() && translator->load( prefix + localeName ) ) if ( s_allowLocalTranslations && translator->load( prefix + localeName ) )
{ {
cDebug() << Logger::SubEntry << "Loaded local translation" << prefix << localeName; cDebug() << Logger::SubEntry << "Loaded local translation" << prefix << localeName;
return true; return true;
@ -139,14 +141,15 @@ tryLoad( QTranslator* translator, const QString& prefix, const QString& localeNa
// Or load from appDataDir -- often /usr/share/calamares -- subdirectory land/ // Or load from appDataDir -- often /usr/share/calamares -- subdirectory land/
QDir localeData( CalamaresUtils::appDataDir() ); QDir localeData( CalamaresUtils::appDataDir() );
if ( localeData.exists() && translator->load( localeData.absolutePath() + QStringLiteral("/lang/") + prefix + localeName) ) if ( localeData.exists()
&& translator->load( localeData.absolutePath() + QStringLiteral( "/lang/" ) + prefix + localeName ) )
{ {
cDebug() << Logger::SubEntry << "Loaded appdata translation" << prefix << localeName; cDebug() << Logger::SubEntry << "Loaded appdata translation" << prefix << localeName;
return true; return true;
} }
// Or from QRC (most common) // Or from QRC (most common)
if ( translator->load( QStringLiteral( ":/lang/") + prefix + localeName ) ) if ( translator->load( QStringLiteral( ":/lang/" ) + prefix + localeName ) )
{ {
cDebug() << Logger::SubEntry << "Loaded QRC translation" << prefix << localeName; cDebug() << Logger::SubEntry << "Loaded QRC translation" << prefix << localeName;
return true; return true;
@ -260,5 +263,11 @@ Retranslator::eventFilter( QObject* obj, QEvent* e )
return QObject::eventFilter( obj, e ); return QObject::eventFilter( obj, e );
} }
void
setAllowLocalTranslation( bool allow )
{
s_allowLocalTranslations = allow;
}
} // namespace CalamaresUtils } // namespace CalamaresUtils

@ -42,6 +42,15 @@ DLLEXPORT void installTranslator( const QLocale& locale, const QString& branding
DLLEXPORT QString translatorLocaleName(); DLLEXPORT QString translatorLocaleName();
/** @brief Set @p allow to true to load translations from current dir.
*
* If false, (or never called) the translations are loaded only from
* system locations (the AppData dir) and from QRC (compiled in).
* Enable local translations to test translations stored in the
* current directory.
*/
DLLEXPORT void setAllowLocalTranslation( bool allow );
class Retranslator : public QObject class Retranslator : public QObject
{ {
Q_OBJECT Q_OBJECT

Loading…
Cancel
Save