i18n: avoid translation tricks, use QCoreApplication::translate

Instead of using tr and some macro hacks to get lupdate to
recognize the translation, instead use QCoreApplication::translate()
which takes its own context for translation.
This commit is contained in:
Adriaan de Groot 2018-02-07 16:29:36 +01:00
parent d3b5189d06
commit f954496acf
2 changed files with 66 additions and 81 deletions

View file

@ -23,6 +23,7 @@
#include "JobQueue.h" #include "JobQueue.h"
#include "GlobalStorage.h" #include "GlobalStorage.h"
#include <QCoreApplication>
#include <QDir> #include <QDir>
#include <QProcess> #include <QProcess>
#include <QRegularExpression> #include <QRegularExpression>
@ -253,44 +254,44 @@ System::doChroot() const
Calamares::JobResult Calamares::JobResult
ProcessResult::explainProcess( const QObject* parent, int ec, const QString& command, const QString& output, int timeout ) ProcessResult::explainProcess( const QObject* parent, int ec, const QString& command, const QString& output, int timeout )
{ {
#define tr parent->tr
using Calamares::JobResult; using Calamares::JobResult;
if ( ec == 0 ) if ( ec == 0 )
return JobResult::ok(); return JobResult::ok();
QString outputMessage = output.isEmpty() ? QStringLiteral("\nThere was no output from the command.") QString outputMessage = output.isEmpty()
: (tr("\nOutput:\n") + output); ? QCoreApplication::translate( "ProcessResult", "\nThere was no output from the command.")
: (QCoreApplication::translate( "ProcessResult", "\nOutput:\n") + output);
if ( ec == -1 ) //Crash! if ( ec == -1 ) //Crash!
return JobResult::error( tr( "External command crashed." ), return JobResult::error( QCoreApplication::translate( "ProcessResult", "External command crashed." ),
tr( "Command <i>%1</i> crashed." ) QCoreApplication::translate( "ProcessResult", "Command <i>%1</i> crashed." )
.arg( command ) .arg( command )
+ outputMessage ); + outputMessage );
if ( ec == -2 ) if ( ec == -2 )
return JobResult::error( tr( "External command failed to start." ), return JobResult::error( QCoreApplication::translate( "ProcessResult", "External command failed to start." ),
tr( "Command <i>%1</i> failed to start." ) QCoreApplication::translate( "ProcessResult", "Command <i>%1</i> failed to start." )
.arg( command ) ); .arg( command ) );
if ( ec == -3 ) if ( ec == -3 )
return JobResult::error( tr( "Internal error when starting command." ), return JobResult::error( QCoreApplication::translate( "ProcessResult", "Internal error when starting command." ),
tr( "Bad parameters for process job call." ) ); QCoreApplication::translate( "ProcessResult", "Bad parameters for process job call." ) );
if ( ec == -4 ) if ( ec == -4 )
return JobResult::error( tr( "External command failed to finish." ), return JobResult::error( QCoreApplication::translate( "ProcessResult", "External command failed to finish." ),
tr( "Command <i>%1</i> failed to finish in %2 seconds." ) QCoreApplication::translate( "ProcessResult", "Command <i>%1</i> failed to finish in %2 seconds." )
.arg( command ) .arg( command )
.arg( timeout ) .arg( timeout )
+ outputMessage ); + outputMessage );
//Any other exit code //Any other exit code
return JobResult::error( tr( "External command finished with errors." ), return JobResult::error( QCoreApplication::translate( "ProcessResult", "External command finished with errors." ),
tr( "Command <i>%1</i> finished with exit code %2." ) QCoreApplication::translate( "ProcessResult", "Command <i>%1</i> finished with exit code %2." )
.arg( command ) .arg( command )
.arg( ec ) .arg( ec )
+ outputMessage ); + outputMessage );
#undef tr #undef trIndirect
} }
} // namespace } // namespace

View file

@ -20,6 +20,7 @@
#include "utils/Logger.h" #include "utils/Logger.h"
#include <QCoreApplication>
#include <QString> #include <QString>
#include <QWidget> #include <QWidget>
@ -51,22 +52,6 @@ PasswordCheck::PasswordCheck( MessageFunc m, AcceptFunc a )
{ {
} }
// Try to trick Transifex into accepting these strings
#define tr parent->tr
struct LengthExplainer
{
static QString too_short( QWidget* parent )
{
return tr( "Password is too short" );
}
static QString too_long( QWidget* parent )
{
return tr( "Password is too long" );
}
} ;
#undef tr
DEFINE_CHECK_FUNC( minLength ) DEFINE_CHECK_FUNC( minLength )
{ {
int minLength = -1; int minLength = -1;
@ -77,9 +62,9 @@ DEFINE_CHECK_FUNC( minLength )
cDebug() << " .. minLength set to" << minLength; cDebug() << " .. minLength set to" << minLength;
checks.push_back( checks.push_back(
PasswordCheck( PasswordCheck(
[parent]() []()
{ {
return LengthExplainer::too_short( parent ); return QCoreApplication::translate( "PWQ", "Password is too short" );
}, },
[minLength]( const QString& s ) [minLength]( const QString& s )
{ {
@ -99,9 +84,9 @@ DEFINE_CHECK_FUNC( maxLength )
cDebug() << " .. maxLength set to" << maxLength; cDebug() << " .. maxLength set to" << maxLength;
checks.push_back( checks.push_back(
PasswordCheck( PasswordCheck(
[parent]() []()
{ {
return LengthExplainer::too_long( parent ); return QCoreApplication::translate("PWQ", "Password is too long" );
}, },
[maxLength]( const QString& s ) [maxLength]( const QString& s )
{ {
@ -154,7 +139,6 @@ public:
return m_rv < 0; return m_rv < 0;
} }
#define tr parent->tr
/* This is roughly the same as the function pwquality_strerror, /* This is roughly the same as the function pwquality_strerror,
* only with QStrings instead, and using the Qt translation scheme. * only with QStrings instead, and using the Qt translation scheme.
* It is used under the terms of the GNU GPL v3 or later, as * It is used under the terms of the GNU GPL v3 or later, as
@ -168,123 +152,123 @@ public:
if ( m_rv >= arbitrary_minimum_strength ) if ( m_rv >= arbitrary_minimum_strength )
return QString(); return QString();
if ( m_rv >= 0 ) if ( m_rv >= 0 )
return tr( "Password is too weak" ); return QCoreApplication::translate( "PWQ", "Password is too weak" );
switch ( m_rv ) switch ( m_rv )
{ {
case PWQ_ERROR_MEM_ALLOC: case PWQ_ERROR_MEM_ALLOC:
if ( auxerror ) if ( auxerror )
{ {
QString s = tr( "Memory allocation error when setting '%1'" ).arg( ( const char* )auxerror ); QString s = QCoreApplication::translate( "PWQ", "Memory allocation error when setting '%1'" ).arg( ( const char* )auxerror );
free( auxerror ); free( auxerror );
return s; return s;
} }
return tr( "Memory allocation error" ); return QCoreApplication::translate( "PWQ", "Memory allocation error" );
case PWQ_ERROR_SAME_PASSWORD: case PWQ_ERROR_SAME_PASSWORD:
return tr( "The password is the same as the old one" ); return QCoreApplication::translate( "PWQ", "The password is the same as the old one" );
case PWQ_ERROR_PALINDROME: case PWQ_ERROR_PALINDROME:
return tr( "The password is a palindrome" ); return QCoreApplication::translate( "PWQ", "The password is a palindrome" );
case PWQ_ERROR_CASE_CHANGES_ONLY: case PWQ_ERROR_CASE_CHANGES_ONLY:
return tr( "The password differs with case changes only" ); return QCoreApplication::translate( "PWQ", "The password differs with case changes only" );
case PWQ_ERROR_TOO_SIMILAR: case PWQ_ERROR_TOO_SIMILAR:
return tr( "The password is too similar to the old one" ); return QCoreApplication::translate( "PWQ", "The password is too similar to the old one" );
case PWQ_ERROR_USER_CHECK: case PWQ_ERROR_USER_CHECK:
return tr( "The password contains the user name in some form" ); return QCoreApplication::translate( "PWQ", "The password contains the user name in some form" );
case PWQ_ERROR_GECOS_CHECK: case PWQ_ERROR_GECOS_CHECK:
return tr( "The password contains words from the real name of the user in some form" ); return QCoreApplication::translate( "PWQ", "The password contains words from the real name of the user in some form" );
case PWQ_ERROR_BAD_WORDS: case PWQ_ERROR_BAD_WORDS:
return tr( "The password contains forbidden words in some form" ); return QCoreApplication::translate( "PWQ", "The password contains forbidden words in some form" );
case PWQ_ERROR_MIN_DIGITS: case PWQ_ERROR_MIN_DIGITS:
if ( auxerror ) if ( auxerror )
return tr( "The password contains less than %1 digits" ).arg( ( long )auxerror ); return QCoreApplication::translate( "PWQ", "The password contains less than %1 digits" ).arg( ( long )auxerror );
return tr( "The password contains too few digits" ); return QCoreApplication::translate( "PWQ", "The password contains too few digits" );
case PWQ_ERROR_MIN_UPPERS: case PWQ_ERROR_MIN_UPPERS:
if ( auxerror ) if ( auxerror )
return tr( "The password contains less than %1 uppercase letters" ).arg( ( long )auxerror ); return QCoreApplication::translate( "PWQ", "The password contains less than %1 uppercase letters" ).arg( ( long )auxerror );
return tr( "The password contains too few uppercase letters" ); return QCoreApplication::translate( "PWQ", "The password contains too few uppercase letters" );
case PWQ_ERROR_MIN_LOWERS: case PWQ_ERROR_MIN_LOWERS:
if ( auxerror ) if ( auxerror )
return tr( "The password contains less than %1 lowercase letters" ).arg( ( long )auxerror ); return QCoreApplication::translate( "PWQ", "The password contains less than %1 lowercase letters" ).arg( ( long )auxerror );
return tr( "The password contains too few lowercase letters" ); return QCoreApplication::translate( "PWQ", "The password contains too few lowercase letters" );
case PWQ_ERROR_MIN_OTHERS: case PWQ_ERROR_MIN_OTHERS:
if ( auxerror ) if ( auxerror )
return tr( "The password contains less than %1 non-alphanumeric characters" ).arg( ( long )auxerror ); return QCoreApplication::translate( "PWQ", "The password contains less than %1 non-alphanumeric characters" ).arg( ( long )auxerror );
return tr( "The password contains too few non-alphanumeric characters" ); return QCoreApplication::translate( "PWQ", "The password contains too few non-alphanumeric characters" );
case PWQ_ERROR_MIN_LENGTH: case PWQ_ERROR_MIN_LENGTH:
if ( auxerror ) if ( auxerror )
return tr( "The password is shorter than %1 characters" ).arg( ( long )auxerror ); return QCoreApplication::translate( "PWQ", "The password is shorter than %1 characters" ).arg( ( long )auxerror );
return tr( "The password is too short" ); return QCoreApplication::translate( "PWQ", "The password is too short" );
case PWQ_ERROR_ROTATED: case PWQ_ERROR_ROTATED:
return tr( "The password is just rotated old one" ); return QCoreApplication::translate( "PWQ", "The password is just rotated old one" );
case PWQ_ERROR_MIN_CLASSES: case PWQ_ERROR_MIN_CLASSES:
if ( auxerror ) if ( auxerror )
return tr( "The password contains less than %1 character classes" ).arg( ( long )auxerror ); return QCoreApplication::translate( "PWQ", "The password contains less than %1 character classes" ).arg( ( long )auxerror );
return tr( "The password does not contain enough character classes" ); return QCoreApplication::translate( "PWQ", "The password does not contain enough character classes" );
case PWQ_ERROR_MAX_CONSECUTIVE: case PWQ_ERROR_MAX_CONSECUTIVE:
if ( auxerror ) if ( auxerror )
return tr( "The password contains more than %1 same characters consecutively" ).arg( ( long )auxerror ); return QCoreApplication::translate( "PWQ", "The password contains more than %1 same characters consecutively" ).arg( ( long )auxerror );
return tr( "The password contains too many same characters consecutively" ); return QCoreApplication::translate( "PWQ", "The password contains too many same characters consecutively" );
case PWQ_ERROR_MAX_CLASS_REPEAT: case PWQ_ERROR_MAX_CLASS_REPEAT:
if ( auxerror ) if ( auxerror )
return tr( "The password contains more than %1 characters of the same class consecutively" ).arg( ( long )auxerror ); return QCoreApplication::translate( "PWQ", "The password contains more than %1 characters of the same class consecutively" ).arg( ( long )auxerror );
return tr( "The password contains too many characters of the same class consecutively" ); return QCoreApplication::translate( "PWQ", "The password contains too many characters of the same class consecutively" );
case PWQ_ERROR_MAX_SEQUENCE: case PWQ_ERROR_MAX_SEQUENCE:
if ( auxerror ) if ( auxerror )
return tr( "The password contains monotonic sequence longer than %1 characters" ).arg( ( long )auxerror ); return QCoreApplication::translate( "PWQ", "The password contains monotonic sequence longer than %1 characters" ).arg( ( long )auxerror );
return tr( "The password contains too long of a monotonic character sequence" ); return QCoreApplication::translate( "PWQ", "The password contains too long of a monotonic character sequence" );
case PWQ_ERROR_EMPTY_PASSWORD: case PWQ_ERROR_EMPTY_PASSWORD:
return tr( "No password supplied" ); return QCoreApplication::translate( "PWQ", "No password supplied" );
case PWQ_ERROR_RNG: case PWQ_ERROR_RNG:
return tr( "Cannot obtain random numbers from the RNG device" ); return QCoreApplication::translate( "PWQ", "Cannot obtain random numbers from the RNG device" );
case PWQ_ERROR_GENERATION_FAILED: case PWQ_ERROR_GENERATION_FAILED:
return tr( "Password generation failed - required entropy too low for settings" ); return QCoreApplication::translate( "PWQ", "Password generation failed - required entropy too low for settings" );
case PWQ_ERROR_CRACKLIB_CHECK: case PWQ_ERROR_CRACKLIB_CHECK:
if ( auxerror ) if ( auxerror )
{ {
/* Here the string comes from cracklib, don't free? */ /* Here the string comes from cracklib, don't free? */
return tr( "The password fails the dictionary check - %1" ).arg( ( const char* )auxerror ); return QCoreApplication::translate( "PWQ", "The password fails the dictionary check - %1" ).arg( ( const char* )auxerror );
} }
return tr( "The password fails the dictionary check" ); return QCoreApplication::translate( "PWQ", "The password fails the dictionary check" );
case PWQ_ERROR_UNKNOWN_SETTING: case PWQ_ERROR_UNKNOWN_SETTING:
if ( auxerror ) if ( auxerror )
{ {
QString s = tr( "Unknown setting - %1" ).arg( ( const char* )auxerror ); QString s = QCoreApplication::translate( "PWQ", "Unknown setting - %1" ).arg( ( const char* )auxerror );
free( auxerror ); free( auxerror );
return s; return s;
} }
return tr( "Unknown setting" ); return QCoreApplication::translate( "PWQ", "Unknown setting" );
case PWQ_ERROR_INTEGER: case PWQ_ERROR_INTEGER:
if ( auxerror ) if ( auxerror )
{ {
QString s = tr( "Bad integer value of setting - %1" ).arg( ( const char* )auxerror ); QString s = QCoreApplication::translate( "PWQ", "Bad integer value of setting - %1" ).arg( ( const char* )auxerror );
free( auxerror ); free( auxerror );
return s; return s;
} }
return tr( "Bad integer value" ); return QCoreApplication::translate( "PWQ", "Bad integer value" );
case PWQ_ERROR_NON_INT_SETTING: case PWQ_ERROR_NON_INT_SETTING:
if ( auxerror ) if ( auxerror )
{ {
QString s = tr( "Setting %1 is not of integer type" ).arg( ( const char* )auxerror ); QString s = QCoreApplication::translate( "PWQ", "Setting %1 is not of integer type" ).arg( ( const char* )auxerror );
free( auxerror ); free( auxerror );
return s; return s;
} }
return tr( "Setting is not of integer type" ); return QCoreApplication::translate( "PWQ", "Setting is not of integer type" );
case PWQ_ERROR_NON_STR_SETTING: case PWQ_ERROR_NON_STR_SETTING:
if ( auxerror ) if ( auxerror )
{ {
QString s = tr( "Setting %1 is not of string type" ).arg( ( const char* )auxerror ); QString s = QCoreApplication::translate( "PWQ", "Setting %1 is not of string type" ).arg( ( const char* )auxerror );
free( auxerror ); free( auxerror );
return s; return s;
} }
return tr( "Setting is not of string type" ); return QCoreApplication::translate( "PWQ", "Setting is not of string type" );
case PWQ_ERROR_CFGFILE_OPEN: case PWQ_ERROR_CFGFILE_OPEN:
return tr( "Opening the configuration file failed" ); return QCoreApplication::translate( "PWQ", "Opening the configuration file failed" );
case PWQ_ERROR_CFGFILE_MALFORMED: case PWQ_ERROR_CFGFILE_MALFORMED:
return tr( "The configuration file is malformed" ); return QCoreApplication::translate( "PWQ", "The configuration file is malformed" );
case PWQ_ERROR_FATAL_FAILURE: case PWQ_ERROR_FATAL_FAILURE:
return tr( "Fatal failure" ); return QCoreApplication::translate( "PWQ", "Fatal failure" );
default: default:
return tr( "Unknown error" ); return QCoreApplication::translate( "PWQ", "Unknown error" );
} }
} }
#undef tr #undef tr