[welcome] Start up in more-specific system language

Because of the way Qt interprets the environment variable
LANG, using `sr@latin` or `sr@latn` or `ca@valencia` would
get you `sr` or `ca`, respectively, which isn't an exact
match. Now that Translation has special-handling for
those values of LANG, match with the ID first.

This allows starting Calamares in Serbian (Latin script)
or Catalan (Valencia) for locales that need it. (Qt doesn't
recognize ca@valencia as a variant, since that's a region-
based locale, not country- or script-based)
This commit is contained in:
Adriaan de Groot 2022-05-04 10:47:49 +02:00
parent 792c4914b0
commit 8aa8597ab0

View File

@ -121,32 +121,42 @@ void
Config::initLanguages() Config::initLanguages()
{ {
// Find the best initial translation // Find the best initial translation
QLocale defaultLocale = QLocale( QLocale::system().name() ); CalamaresUtils::Locale::Translation defaultTranslation;
cDebug() << "Matching locale" << defaultLocale; cDebug() << "Trying to match locale" << defaultTranslation.id();
int matchedLocaleIndex = m_languages->find( [&]( const QLocale& x ) { int matchedLocaleIndex = m_languages->find( defaultTranslation.id() );
return x.language() == defaultLocale.language() && x.country() == defaultLocale.country();
} );
// Need to match by some other means than the exact translation Id
if ( matchedLocaleIndex < 0 ) if ( matchedLocaleIndex < 0 )
{ {
cDebug() << Logger::SubEntry << "Matching approximate locale" << defaultLocale.language();
matchedLocaleIndex QLocale defaultLocale = defaultTranslation.locale();
= m_languages->find( [&]( const QLocale& x ) { return x.language() == defaultLocale.language(); } );
}
if ( matchedLocaleIndex < 0 ) cDebug() << "Trying to match locale" << defaultLocale;
{ matchedLocaleIndex = m_languages->find(
QLocale en_us( QLocale::English, QLocale::UnitedStates ); [ & ]( const QLocale& x )
{ return x.language() == defaultLocale.language() && x.country() == defaultLocale.country(); } );
cDebug() << Logger::SubEntry << "Matching English (US)"; if ( matchedLocaleIndex < 0 )
matchedLocaleIndex = m_languages->find( en_us );
// Now, if it matched, because we didn't match the system locale, switch to the one found
if ( matchedLocaleIndex >= 0 )
{ {
QLocale::setDefault( m_languages->locale( matchedLocaleIndex ).locale() ); cDebug() << Logger::SubEntry << "Trying to match approximate locale" << defaultLocale.language();
matchedLocaleIndex
= m_languages->find( [ & ]( const QLocale& x ) { return x.language() == defaultLocale.language(); } );
}
if ( matchedLocaleIndex < 0 )
{
QLocale en_us( QLocale::English, QLocale::UnitedStates );
cDebug() << Logger::SubEntry << "Trying to match English (US)";
matchedLocaleIndex = m_languages->find( en_us );
// Now, if it matched, because we didn't match the system locale, switch to the one found
if ( matchedLocaleIndex >= 0 )
{
QLocale::setDefault( m_languages->locale( matchedLocaleIndex ).locale() );
}
} }
} }
@ -156,7 +166,7 @@ Config::initLanguages()
} }
else else
{ {
cWarning() << "No available translation matched" << defaultLocale; cWarning() << "No available translation matched" << defaultTranslation.id() << defaultTranslation.locale();
} }
} }
@ -191,7 +201,8 @@ Config::setLocaleIndex( int index )
QLocale::setDefault( selectedTranslation.locale() ); QLocale::setDefault( selectedTranslation.locale() );
const auto* branding = Calamares::Branding::instance(); const auto* branding = Calamares::Branding::instance();
CalamaresUtils::installTranslator( selectedTranslation.id(), branding ? branding->translationsDirectory() : QString() ); CalamaresUtils::installTranslator( selectedTranslation.id(),
branding ? branding->translationsDirectory() : QString() );
if ( Calamares::JobQueue::instance() && Calamares::JobQueue::instance()->globalStorage() ) if ( Calamares::JobQueue::instance() && Calamares::JobQueue::instance()->globalStorage() )
{ {
CalamaresUtils::Locale::insertGS( *Calamares::JobQueue::instance()->globalStorage(), CalamaresUtils::Locale::insertGS( *Calamares::JobQueue::instance()->globalStorage(),
@ -367,13 +378,16 @@ setGeoIP( Config* config, const QVariantMap& configurationMap )
if ( handler->type() != CalamaresUtils::GeoIP::Handler::Type::None ) if ( handler->type() != CalamaresUtils::GeoIP::Handler::Type::None )
{ {
auto* future = new FWString(); auto* future = new FWString();
QObject::connect( future, &FWString::finished, [config, future, handler]() { QObject::connect( future,
QString countryResult = future->future().result(); &FWString::finished,
cDebug() << "GeoIP result for welcome=" << countryResult; [ config, future, handler ]()
::setCountry( config, countryResult, handler ); {
future->deleteLater(); QString countryResult = future->future().result();
delete handler; cDebug() << "GeoIP result for welcome=" << countryResult;
} ); ::setCountry( config, countryResult, handler );
future->deleteLater();
delete handler;
} );
future->setFuture( handler->queryRaw() ); future->setFuture( handler->queryRaw() );
} }
else else