Keyboard: guess at layout based on locale

Split locale into <language>_<country> 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].
This commit is contained in:
Philip 2017-06-17 15:36:09 +02:00
parent b7ce68897d
commit 4c33e9a16a
4 changed files with 61 additions and 4 deletions

View File

@ -1,6 +1,7 @@
/* === This file is part of Calamares - <http://github.com/calamares> === /* === This file is part of Calamares - <http://github.com/calamares> ===
* *
* Copyright 2016, Teo Mrnjavac <teo@kde.org> * Copyright 2016, Teo Mrnjavac <teo@kde.org>
* Copyright 2017, Adriaan de Groot <groot@kde.org>
* *
* Calamares is free software: you can redistribute it and/or modify * Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -59,9 +60,9 @@ KeyboardLayoutModel::data( const QModelIndex& index, int role ) const
void void
KeyboardLayoutModel::init() KeyboardLayoutModel::init()
{ {
QMap< QString, KeyboardGlobal::KeyboardInfo > layouts = KeyboardGlobal::LayoutsMap layouts =
KeyboardGlobal::getKeyboardLayouts(); KeyboardGlobal::getKeyboardLayouts();
for ( QMap< QString, KeyboardGlobal::KeyboardInfo >::const_iterator it = layouts.constBegin(); for ( KeyboardGlobal::LayoutsMap::const_iterator it = layouts.constBegin();
it != layouts.constEnd(); ++it ) it != layouts.constEnd(); ++it )
{ {
m_layouts.append( qMakePair( it.key(), it.value() ) ); m_layouts.append( qMakePair( it.key(), it.value() ) );

View File

@ -1,6 +1,7 @@
/* === This file is part of Calamares - <http://github.com/calamares> === /* === This file is part of Calamares - <http://github.com/calamares> ===
* *
* Copyright 2014-2016, Teo Mrnjavac <teo@kde.org> * Copyright 2014-2016, Teo Mrnjavac <teo@kde.org>
* Copyright 2017, Adriaan de Groot <groot@kde.org>
* *
* Portions from the Manjaro Installation Framework * Portions from the Manjaro Installation Framework
* by Roland Singer <roland@manjaro.org> * by Roland Singer <roland@manjaro.org>
@ -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 void
KeyboardPage::onActivate() KeyboardPage::onActivate()
{ {
ui->listLayout->setFocus(); 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 );
}
} }

View File

@ -1,6 +1,7 @@
/* === This file is part of Calamares - <http://github.com/calamares> === /* === This file is part of Calamares - <http://github.com/calamares> ===
* *
* Copyright 2014-2016, Teo Mrnjavac <teo@kde.org> * Copyright 2014-2016, Teo Mrnjavac <teo@kde.org>
* Copyright 2017, Adriaan de Groot <groot@kde.org>
* *
* Portions from the Manjaro Installation Framework * Portions from the Manjaro Installation Framework
* by Roland Singer <roland@manjaro.org> * by Roland Singer <roland@manjaro.org>
@ -70,6 +71,8 @@ private:
KeyboardGlobal::KeyboardInfo info; KeyboardGlobal::KeyboardInfo info;
}; };
/// Guess a layout based on the split-apart locale
void guessLayout( const QStringList& langParts );
void updateVariants( const QPersistentModelIndex& currentItem, void updateVariants( const QPersistentModelIndex& currentItem,
QString currentVariant = QString() ); QString currentVariant = QString() );

View File

@ -1,6 +1,7 @@
/* === This file is part of Calamares - <http://github.com/calamares> === /* === This file is part of Calamares - <http://github.com/calamares> ===
* *
* Copyright 2014, Teo Mrnjavac <teo@kde.org> * Copyright 2014, Teo Mrnjavac <teo@kde.org>
* Copyright 2017, Adriaan de Groot <groot@kde.org>
* *
* Originally from the Manjaro Installation Framework * Originally from the Manjaro Installation Framework
* by Roland Singer <roland@manjaro.org> * by Roland Singer <roland@manjaro.org>
@ -44,12 +45,15 @@ public:
QMap< QString, QString > variants; QMap< QString, QString > variants;
}; };
static QMap< QString, KeyboardInfo > getKeyboardLayouts(); using LayoutsMap = QMap< QString, KeyboardInfo >;
static LayoutsMap getKeyboardLayouts();
static QMap< QString, QString > getKeyboardModels(); static QMap< QString, QString > getKeyboardModels();
private: private:
static QMap< QString, QString > parseKeyboardModels(QString filepath); static QMap< QString, QString > parseKeyboardModels(QString filepath);
static QMap< QString, KeyboardInfo > parseKeyboardLayouts(QString filepath); static LayoutsMap parseKeyboardLayouts(QString filepath);
}; };
#endif // KEYBOARDGLOBAL_H #endif // KEYBOARDGLOBAL_H