From 88715b9a0fbf8a77104408b1e37d68762dc63d87 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 7 Jun 2017 14:05:13 -0400 Subject: [PATCH] Keyboard: guess at layout based on locale Split locale into _ and go looking for keyboard layouts that match. Do that in reverse, so look for country first. - known weakness is el_CY (should get layout gr) because CY and el don't name any keyboard layout. - known weakness are Hausa, Igbo .. which are ha_NG and ig_NG. They select keyboard layout ng, which is labeled "English (Nigeria)"; they ought to select ng(hausa) and ng(igbo), which are the right variant keyboard layouts to use. - similar selecting a locale in Canada (en_CA, fr_CA, iu_CA ...) will select keyboard layout ca, which is for French-speaking Canada. Locale en_CA should select keyboard en -- e.g. en(us). But iu_CA (Inuktituk) needs layout ca(ike). --- src/modules/keyboard/KeyboardPage.cpp | 49 +++++++++++++++++++++++++++ src/modules/keyboard/KeyboardPage.h | 3 ++ 2 files changed, 52 insertions(+) diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index 117530a88..770c7c6b1 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -1,6 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014-2016, Teo Mrnjavac + * Copyright 2017, Adriaan de Groot * * Portions from the Manjaro Installation Framework * by Roland Singer @@ -221,10 +222,58 @@ KeyboardPage::createJobs( const QString& xOrgConfFileName, } +void +KeyboardPage::guessLayout( const QStringList& langParts ) +{ + const KeyboardLayoutModel* klm = dynamic_cast< KeyboardLayoutModel* >( ui->listLayout->model() ); + bool foundCountryPart = false; + for ( auto countryPart = langParts.rbegin(); !foundCountryPart && countryPart != langParts.rend(); ++countryPart) + { + cDebug() << " .. looking for locale part" << *countryPart; + for ( int i = 0; i < klm->rowCount(); ++i ) + { + QModelIndex idx = klm->index( i ); + if ( idx.isValid() && + ( idx.data( KeyboardLayoutModel::KeyboardLayoutKeyRole ).toString().compare( *countryPart, Qt::CaseInsensitive ) == 0 ) ) + { + cDebug() << " .. matched" << idx.data( KeyboardLayoutModel::KeyboardLayoutKeyRole ).toString(); + ui->listLayout->setCurrentIndex( idx ); + foundCountryPart = true; + break; + } + } + } +} + + void KeyboardPage::onActivate() { ui->listLayout->setFocus(); + + // Try to preselect a layout, depending on language and locale + Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage(); + QString lang = gs->value( "localeConf" ).toMap().value( "LANG" ).toString(); + + cDebug() << "Got locale language" << lang; + if ( !lang.isEmpty() ) + { + // Chop off .codeset and @modifier + int index = lang.indexOf('.'); + if ( index >= 0 ) + lang.truncate( index ); + index = lang.indexOf('@'); + if ( index >= 0 ) + lang.truncate( index ); + + lang.replace( '-', '_' ); // Normalize separators + const auto langParts = lang.split( '_' , QString::SkipEmptyParts ); + + QString country = QLocale::countryToString( QLocale( lang ).country() ); + cDebug() << " .. extracted country" << country << "::" << langParts; + + guessLayout( langParts ); + } } diff --git a/src/modules/keyboard/KeyboardPage.h b/src/modules/keyboard/KeyboardPage.h index c60d62b2b..6f828f446 100644 --- a/src/modules/keyboard/KeyboardPage.h +++ b/src/modules/keyboard/KeyboardPage.h @@ -1,6 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014-2016, Teo Mrnjavac + * Copyright 2017, Adriaan de Groot * * Portions from the Manjaro Installation Framework * by Roland Singer @@ -70,6 +71,8 @@ private: KeyboardGlobal::KeyboardInfo info; }; + /// Guess a layout based on the split-apart locale + void guessLayout( const QStringList& langParts ); void updateVariants( const QPersistentModelIndex& currentItem, QString currentVariant = QString() );