[partition] Fix translation issues

- Strings were being used as logical values, and then logged
  (which should be in English) and also used in the UI (which
  should be localized). Replace with a MessageAndPath class,
  used only locally, that defers the translation until called-
  upon explicitly.
- Replace some VG stuff with similar calls to apply().
This commit is contained in:
Adriaan de Groot 2021-11-01 23:48:18 +01:00
parent e56158f5b4
commit c322eaa430

View File

@ -23,6 +23,7 @@
#include <kpmcore/core/partition.h> #include <kpmcore/core/partition.h>
#include <kpmcore/util/report.h> #include <kpmcore/util/report.h>
#include <QCoreApplication>
#include <QDir> #include <QDir>
#include <QProcess> #include <QProcess>
#include <QStringList> #include <QStringList>
@ -112,8 +113,42 @@ getSwapsForDevice( const QString& deviceName )
* *
*/ */
class MessageAndPath
{
public:
///@brief An unsuccessful attempt at something
MessageAndPath() {}
///@brief A success at doing @p thing to @p path
MessageAndPath( const char* thing, const QString& path )
: m_message( thing )
, m_path( path )
{
}
bool isEmpty() const { return m_message; }
explicit operator QString() const
{
return isEmpty() ? QString() : QCoreApplication::translate( "ClearMountsJob", m_message );
}
const char* const m_message = nullptr;
QString const m_path;
};
STATICTEST inline QDebug&
operator<<( QDebug& s, const MessageAndPath& m )
{
if ( m.isEmpty() )
{
return s;
}
return s << QString( m.m_message ).arg( m.m_path );
}
///@brief Returns a debug-string if @p partPath could be unmounted ///@brief Returns a debug-string if @p partPath could be unmounted
STATICTEST QString STATICTEST MessageAndPath
tryUmount( const QString& partPath ) tryUmount( const QString& partPath )
{ {
QProcess process; QProcess process;
@ -121,21 +156,21 @@ tryUmount( const QString& partPath )
process.waitForFinished(); process.waitForFinished();
if ( process.exitCode() == 0 ) if ( process.exitCode() == 0 )
{ {
return QString( "Successfully unmounted %1." ).arg( partPath ); return { QT_TRANSLATE_NOOP( "ClearMountsJob", "Successfully unmounted %1." ), partPath };
} }
process.start( "swapoff", { partPath } ); process.start( "swapoff", { partPath } );
process.waitForFinished(); process.waitForFinished();
if ( process.exitCode() == 0 ) if ( process.exitCode() == 0 )
{ {
return QString( "Successfully disabled swap %1." ).arg( partPath ); return { QT_TRANSLATE_NOOP( "ClearMountsJob", "Successfully disabled swap %1." ), partPath };
} }
return QString(); return {};
} }
///@brief Returns a debug-string if @p partPath was swap and could be cleared ///@brief Returns a debug-string if @p partPath was swap and could be cleared
STATICTEST QString STATICTEST MessageAndPath
tryClearSwap( const QString& partPath ) tryClearSwap( const QString& partPath )
{ {
QProcess process; QProcess process;
@ -144,21 +179,21 @@ tryClearSwap( const QString& partPath )
QString swapPartUuid = QString::fromLocal8Bit( process.readAllStandardOutput() ).simplified(); QString swapPartUuid = QString::fromLocal8Bit( process.readAllStandardOutput() ).simplified();
if ( process.exitCode() != 0 || swapPartUuid.isEmpty() ) if ( process.exitCode() != 0 || swapPartUuid.isEmpty() )
{ {
return QString(); return {};
} }
process.start( "mkswap", { "-U", swapPartUuid, partPath } ); process.start( "mkswap", { "-U", swapPartUuid, partPath } );
process.waitForFinished(); process.waitForFinished();
if ( process.exitCode() != 0 ) if ( process.exitCode() != 0 )
{ {
return QString(); return {};
} }
return QString( "Successfully cleared swap %1." ).arg( partPath ); return { QT_TRANSLATE_NOOP( "ClearMountsJob", "Successfully cleared swap %1." ), partPath };
} }
///@brief Returns a debug-string if @p mapperPath could be closed ///@brief Returns a debug-string if @p mapperPath could be closed
STATICTEST QString STATICTEST MessageAndPath
tryCryptoClose( const QString& mapperPath ) tryCryptoClose( const QString& mapperPath )
{ {
/* ignored */ tryUmount( mapperPath ); /* ignored */ tryUmount( mapperPath );
@ -168,20 +203,20 @@ tryCryptoClose( const QString& mapperPath )
process.waitForFinished(); process.waitForFinished();
if ( process.exitCode() == 0 ) if ( process.exitCode() == 0 )
{ {
return QString( "Successfully closed mapper device %1." ).arg( mapperPath ); return { QT_TRANSLATE_NOOP( "ClearMountsJob", "Successfully closed mapper device %1." ), mapperPath };
} }
return QString(); return {};
} }
///@brief Apply @p f to all the @p paths, appending successes to @p news ///@brief Apply @p f to all the @p paths, appending successes to @p news
template < typename F > template < typename T, typename F >
void void
apply( const QStringList& paths, F f, QStringList& news ) apply( const T& paths, F f, QList< MessageAndPath >& news )
{ {
for ( const QString& p : qAsConst( paths ) ) for ( const QString& p : qAsConst( paths ) )
{ {
QString n = f( p ); auto n = f( p );
if ( !n.isEmpty() ) if ( !n.isEmpty() )
{ {
news.append( n ); news.append( n );
@ -189,6 +224,17 @@ apply( const QStringList& paths, F f, QStringList& news )
} }
} }
STATICTEST QStringList
stringify( const QList< MessageAndPath >& news )
{
QStringList l;
for ( const auto& m : qAsConst( news ) )
{
l << QString( m );
}
return l;
}
ClearMountsJob::ClearMountsJob( Device* device ) ClearMountsJob::ClearMountsJob( Device* device )
: Calamares::Job() : Calamares::Job()
, m_device( device ) , m_device( device )
@ -214,7 +260,7 @@ ClearMountsJob::exec()
QString deviceName = m_device->deviceNode().split( '/' ).last(); QString deviceName = m_device->deviceNode().split( '/' ).last();
QStringList goodNews; QList< MessageAndPath > goodNews;
QProcess process; QProcess process;
const QStringList partitionsList = getPartitionsForDevice( deviceName ); const QStringList partitionsList = getPartitionsForDevice( deviceName );
@ -228,17 +274,15 @@ ClearMountsJob::exec()
if ( process.exitCode() == 0 ) //means LVM2 tools are installed if ( process.exitCode() == 0 ) //means LVM2 tools are installed
{ {
const QStringList lvscanLines = QString::fromLocal8Bit( process.readAllStandardOutput() ).split( '\n' ); const QStringList lvscanLines = QString::fromLocal8Bit( process.readAllStandardOutput() ).split( '\n' );
for ( const QString& lvscanLine : lvscanLines ) apply(
{ lvscanLines,
[]( const QString& lvscanLine ) {
QString lvPath = lvscanLine.simplified().split( ' ' ).value( 1 ); //second column QString lvPath = lvscanLine.simplified().split( ' ' ).value( 1 ); //second column
lvPath = lvPath.replace( '\'', "" ); lvPath = lvPath.replace( '\'', "" );
QString news = tryUmount( lvPath ); return tryUmount( lvPath );
if ( !news.isEmpty() ) },
{ goodNews );
goodNews.append( news );
}
}
} }
else else
{ {
@ -268,15 +312,19 @@ ClearMountsJob::exec()
vgSet.insert( vgName ); vgSet.insert( vgName );
} }
foreach ( const QString& vgName, vgSet ) apply(
{ vgSet,
process.start( "vgchange", { "-an", vgName } ); []( const QString& vgName ) {
process.waitForFinished(); QProcess vgProcess;
if ( process.exitCode() == 0 ) vgProcess.start( "vgchange", { "-an", vgName } );
{ vgProcess.waitForFinished();
goodNews.append( QString( "Successfully disabled volume group %1." ).arg( vgName ) ); return ( vgProcess.exitCode() == 0 )
} ? MessageAndPath { QT_TRANSLATE_NOOP( "ClearMountsJob",
} "Successfully disabled volume group %1." ),
vgName }
: MessageAndPath {};
},
goodNews );
} }
} }
else else
@ -290,9 +338,8 @@ ClearMountsJob::exec()
Calamares::JobResult ok = Calamares::JobResult::ok(); Calamares::JobResult ok = Calamares::JobResult::ok();
ok.setMessage( tr( "Cleared all mounts for %1" ).arg( m_device->deviceNode() ) ); ok.setMessage( tr( "Cleared all mounts for %1" ).arg( m_device->deviceNode() ) );
ok.setDetails( goodNews.join( "\n" ) ); // FIXME: this exposes untranslated strings ok.setDetails( stringify( goodNews ).join( "\n" ) );
cDebug() << "ClearMountsJob finished. Here's what was done:" << Logger::DebugListT< MessageAndPath >( goodNews );
cDebug() << "ClearMountsJob finished. Here's what was done:\n" << goodNews.join( "\n" );
return ok; return ok;
} }