[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,16 +121,25 @@ 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 )
{
QLocale defaultLocale = defaultTranslation.locale();
cDebug() << "Trying to match locale" << defaultLocale;
matchedLocaleIndex = m_languages->find(
[ & ]( const QLocale& x )
{ return x.language() == defaultLocale.language() && x.country() == defaultLocale.country(); } );
if ( matchedLocaleIndex < 0 ) if ( matchedLocaleIndex < 0 )
{ {
cDebug() << Logger::SubEntry << "Matching approximate locale" << defaultLocale.language(); cDebug() << Logger::SubEntry << "Trying to match approximate locale" << defaultLocale.language();
matchedLocaleIndex matchedLocaleIndex
= m_languages->find( [ & ]( const QLocale& x ) { return x.language() == defaultLocale.language(); } ); = m_languages->find( [ & ]( const QLocale& x ) { return x.language() == defaultLocale.language(); } );
@ -140,7 +149,7 @@ Config::initLanguages()
{ {
QLocale en_us( QLocale::English, QLocale::UnitedStates ); QLocale en_us( QLocale::English, QLocale::UnitedStates );
cDebug() << Logger::SubEntry << "Matching English (US)"; cDebug() << Logger::SubEntry << "Trying to match English (US)";
matchedLocaleIndex = m_languages->find( en_us ); matchedLocaleIndex = m_languages->find( en_us );
// Now, if it matched, because we didn't match the system locale, switch to the one found // Now, if it matched, because we didn't match the system locale, switch to the one found
@ -149,6 +158,7 @@ Config::initLanguages()
QLocale::setDefault( m_languages->locale( matchedLocaleIndex ).locale() ); QLocale::setDefault( m_languages->locale( matchedLocaleIndex ).locale() );
} }
} }
}
if ( matchedLocaleIndex >= 0 ) if ( matchedLocaleIndex >= 0 )
{ {
@ -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,7 +378,10 @@ 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,
&FWString::finished,
[ config, future, handler ]()
{
QString countryResult = future->future().result(); QString countryResult = future->future().result();
cDebug() << "GeoIP result for welcome=" << countryResult; cDebug() << "GeoIP result for welcome=" << countryResult;
::setCountry( config, countryResult, handler ); ::setCountry( config, countryResult, handler );