From 8aa8597ab083f9473a1db14f93acbdc145df3d9d Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 4 May 2022 10:47:49 +0200 Subject: [PATCH] [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) --- src/modules/welcome/Config.cpp | 68 ++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/src/modules/welcome/Config.cpp b/src/modules/welcome/Config.cpp index 94f2192c2..0baadd82f 100644 --- a/src/modules/welcome/Config.cpp +++ b/src/modules/welcome/Config.cpp @@ -121,32 +121,42 @@ void Config::initLanguages() { // Find the best initial translation - QLocale defaultLocale = QLocale( QLocale::system().name() ); + CalamaresUtils::Locale::Translation defaultTranslation; - cDebug() << "Matching locale" << defaultLocale; - int matchedLocaleIndex = m_languages->find( [&]( const QLocale& x ) { - return x.language() == defaultLocale.language() && x.country() == defaultLocale.country(); - } ); + cDebug() << "Trying to match locale" << defaultTranslation.id(); + int matchedLocaleIndex = m_languages->find( defaultTranslation.id() ); + // Need to match by some other means than the exact translation Id if ( matchedLocaleIndex < 0 ) { - cDebug() << Logger::SubEntry << "Matching approximate locale" << defaultLocale.language(); - matchedLocaleIndex - = m_languages->find( [&]( const QLocale& x ) { return x.language() == defaultLocale.language(); } ); - } + QLocale defaultLocale = defaultTranslation.locale(); - if ( matchedLocaleIndex < 0 ) - { - QLocale en_us( QLocale::English, QLocale::UnitedStates ); + cDebug() << "Trying to match locale" << defaultLocale; + matchedLocaleIndex = m_languages->find( + [ & ]( const QLocale& x ) + { return x.language() == defaultLocale.language() && x.country() == defaultLocale.country(); } ); - cDebug() << Logger::SubEntry << "Matching 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 ) + 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 { - 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() ); 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() ) { 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 ) { auto* future = new FWString(); - QObject::connect( future, &FWString::finished, [config, future, handler]() { - QString countryResult = future->future().result(); - cDebug() << "GeoIP result for welcome=" << countryResult; - ::setCountry( config, countryResult, handler ); - future->deleteLater(); - delete handler; - } ); + QObject::connect( future, + &FWString::finished, + [ config, future, handler ]() + { + QString countryResult = future->future().result(); + cDebug() << "GeoIP result for welcome=" << countryResult; + ::setCountry( config, countryResult, handler ); + future->deleteLater(); + delete handler; + } ); future->setFuture( handler->queryRaw() ); } else