[welcome] Refactor the code that picks a locale to use
- Much like std::find_if, but slightly muddled because there's no iterator that we can sensibly use. - Scan the ComboBox for a locale that matches a predicate. - Log more as the search for a good locale progresses. - Don't mix matching the locale with filling the ComboBox (even though that's slightly more efficient).
This commit is contained in:
parent
522adf766a
commit
4b7465696d
@ -106,80 +106,97 @@ WelcomePage::WelcomePage( RequirementsChecker* requirementsChecker, QWidget* par
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** @brief Match the combobox of languages with a predicate
|
||||||
|
*
|
||||||
|
* Scans the entries in the @p list (actually a ComboBox) and if one
|
||||||
|
* matches the given @p predicate, returns true and sets @p matchFound
|
||||||
|
* to the locale that matched.
|
||||||
|
*
|
||||||
|
* If none match, returns false and leaves @p matchFound unchanged.
|
||||||
|
*/
|
||||||
|
static
|
||||||
|
bool matchLocale( QComboBox& list, QLocale& matchFound, std::function<bool(const QLocale&)> predicate)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < list.count(); i++)
|
||||||
|
{
|
||||||
|
QLocale thisLocale = list.itemData( i, Qt::UserRole ).toLocale();
|
||||||
|
if ( predicate(thisLocale) )
|
||||||
|
{
|
||||||
|
list.setCurrentIndex( i );
|
||||||
|
cDebug() << " .. Matched locale " << thisLocale.name();
|
||||||
|
matchFound = thisLocale;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
WelcomePage::initLanguages()
|
WelcomePage::initLanguages()
|
||||||
{
|
{
|
||||||
ui->languageWidget->setInsertPolicy( QComboBox::InsertAlphabetically );
|
ui->languageWidget->setInsertPolicy( QComboBox::InsertAlphabetically );
|
||||||
|
|
||||||
QLocale defaultLocale = QLocale( QLocale::system().name() );
|
QLocale defaultLocale = QLocale( QLocale::system().name() );
|
||||||
{
|
|
||||||
bool isTranslationAvailable = false;
|
|
||||||
|
|
||||||
|
// Fill the list of translations
|
||||||
|
{
|
||||||
const auto locales = QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';');
|
const auto locales = QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';');
|
||||||
for ( const QString& locale : locales )
|
for ( const QString& locale : locales )
|
||||||
{
|
{
|
||||||
QLocale thisLocale = QLocale( locale );
|
QLocale thisLocale = QLocale( locale );
|
||||||
QString lang = QLocale::languageToString( thisLocale.language() );
|
QString lang = QLocale::languageToString( thisLocale.language() );
|
||||||
|
|
||||||
if ( QLocale::countriesForLanguage( thisLocale.language() ).count() > 2 )
|
if ( QLocale::countriesForLanguage( thisLocale.language() ).count() > 2 )
|
||||||
lang.append( QString( " (%1)" )
|
lang.append( QString( " (%1)" )
|
||||||
.arg( QLocale::countryToString( thisLocale.country() ) ) );
|
.arg( QLocale::countryToString( thisLocale.country() ) ) );
|
||||||
|
|
||||||
ui->languageWidget->addItem( lang, thisLocale );
|
ui->languageWidget->addItem( lang, thisLocale );
|
||||||
if ( thisLocale.language() == defaultLocale.language() &&
|
|
||||||
thisLocale.country() == defaultLocale.country() )
|
|
||||||
{
|
|
||||||
isTranslationAvailable = true;
|
|
||||||
ui->languageWidget->setCurrentIndex( ui->languageWidget->count() - 1 );
|
|
||||||
cDebug() << "Initial locale " << thisLocale.name();
|
|
||||||
CalamaresUtils::installTranslator( thisLocale.name(),
|
|
||||||
Calamares::Branding::instance()->translationsPathPrefix(),
|
|
||||||
qApp );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Find the best initial translation
|
||||||
|
QLocale selectedLocale;
|
||||||
|
|
||||||
|
cDebug() << "Matching exact locale" << defaultLocale;
|
||||||
|
bool isTranslationAvailable =
|
||||||
|
matchLocale( *(ui->languageWidget), selectedLocale,
|
||||||
|
[&](const QLocale& x){ return x.language() == defaultLocale.language() && x.country() == defaultLocale.country(); } );
|
||||||
|
|
||||||
|
if ( !isTranslationAvailable )
|
||||||
|
{
|
||||||
|
cDebug() << "Matching approximate locale" << defaultLocale.language();
|
||||||
|
|
||||||
|
isTranslationAvailable =
|
||||||
|
matchLocale( *(ui->languageWidget), selectedLocale,
|
||||||
|
[&](const QLocale& x){ return x.language() == defaultLocale.language(); } ) ;
|
||||||
|
}
|
||||||
|
|
||||||
if ( !isTranslationAvailable )
|
if ( !isTranslationAvailable )
|
||||||
{
|
{
|
||||||
for (int i = 0; i < ui->languageWidget->count(); i++)
|
QLocale en_us( QLocale::English, QLocale::UnitedStates );
|
||||||
{
|
|
||||||
QLocale thisLocale = ui->languageWidget->itemData( i, Qt::UserRole ).toLocale();
|
cDebug() << "Matching English (US)";
|
||||||
if ( thisLocale.language() == defaultLocale.language() )
|
isTranslationAvailable =
|
||||||
{
|
matchLocale( *(ui->languageWidget), selectedLocale,
|
||||||
isTranslationAvailable = true;
|
[&](const QLocale& x){ return x == en_us; } );
|
||||||
ui->languageWidget->setCurrentIndex( i );
|
|
||||||
cDebug() << "Initial locale " << thisLocale.name();
|
// Now, if it matched, because we didn't match the system locale, switch to the one found
|
||||||
CalamaresUtils::installTranslator( thisLocale.name(),
|
if ( isTranslationAvailable )
|
||||||
Calamares::Branding::instance()->translationsPathPrefix(),
|
QLocale::setDefault( selectedLocale );
|
||||||
qApp );
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !isTranslationAvailable )
|
if ( isTranslationAvailable )
|
||||||
{
|
CalamaresUtils::installTranslator( selectedLocale.name(),
|
||||||
for (int i = 0; i < ui->languageWidget->count(); i++)
|
|
||||||
{
|
|
||||||
QLocale thisLocale = ui->languageWidget->itemData( i, Qt::UserRole ).toLocale();
|
|
||||||
if ( thisLocale == QLocale( QLocale::English, QLocale::UnitedStates ) )
|
|
||||||
{
|
|
||||||
isTranslationAvailable = true;
|
|
||||||
ui->languageWidget->setCurrentIndex( i );
|
|
||||||
cDebug() << "Translation unavailable, so initial locale set to " << thisLocale.name();
|
|
||||||
QLocale::setDefault( thisLocale );
|
|
||||||
CalamaresUtils::installTranslator( thisLocale.name(),
|
|
||||||
Calamares::Branding::instance()->translationsPathPrefix(),
|
Calamares::Branding::instance()->translationsPathPrefix(),
|
||||||
qApp );
|
qApp );
|
||||||
break;
|
else
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( !isTranslationAvailable )
|
|
||||||
cWarning() << "No available translation matched" << defaultLocale;
|
cWarning() << "No available translation matched" << defaultLocale;
|
||||||
|
|
||||||
connect( ui->languageWidget,
|
connect( ui->languageWidget,
|
||||||
static_cast< void ( QComboBox::* )( int ) >( &QComboBox::currentIndexChanged ),
|
static_cast< void ( QComboBox::* )( int ) >( &QComboBox::currentIndexChanged ),
|
||||||
this, [ & ]( int newIndex )
|
this,
|
||||||
|
[&]( int newIndex )
|
||||||
{
|
{
|
||||||
QLocale selectedLocale = ui->languageWidget->itemData( newIndex, Qt::UserRole ).toLocale();
|
QLocale selectedLocale = ui->languageWidget->itemData( newIndex, Qt::UserRole ).toLocale();
|
||||||
cDebug() << "Selected locale" << selectedLocale.name();
|
cDebug() << "Selected locale" << selectedLocale.name();
|
||||||
@ -190,7 +207,6 @@ WelcomePage::initLanguages()
|
|||||||
qApp );
|
qApp );
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
|
Loading…
Reference in New Issue
Block a user