[locale] Provide API for setting language

- The language and BCP need to be in-sync
 - Existing code was inconsistent in setting things, which is why
   you could get through the locale page without setting a locale
   (at all) or it would keep English in spite of picking Germand on
   the welcome page.
 - Patch tests to use that API.
This commit is contained in:
Adriaan de Groot 2019-01-08 18:09:34 +01:00
parent 8cc0e1f5f9
commit f1cbd5fcbf
4 changed files with 30 additions and 34 deletions

View File

@ -38,12 +38,12 @@ LocaleConfiguration::LocaleConfiguration( const QString& localeName, const QStri
}
QString
void
LocaleConfiguration::setLanguage(const QString& localeName )
{
QString language = localeName.split( '_' ).first();
myLanguageLocaleBcp47 = QLocale( language ).bcp47Name().toLower();
return language;
m_languageLocaleBcp47 = QLocale( language ).bcp47Name().toLower();
m_lang = localeName;
}
@ -276,7 +276,7 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
bool
LocaleConfiguration::isEmpty() const
{
return lang.isEmpty() &&
return m_lang.isEmpty() &&
lc_numeric.isEmpty() &&
lc_time.isEmpty() &&
lc_monetary.isEmpty() &&
@ -294,8 +294,8 @@ LocaleConfiguration::toMap() const
{
QMap< QString, QString > map;
if ( !lang.isEmpty() )
map.insert( "LANG", lang );
if ( !m_lang.isEmpty() )
map.insert( "LANG", m_lang );
if ( !lc_numeric.isEmpty() )
map.insert( "LC_NUMERIC", lc_numeric );
@ -327,8 +327,3 @@ LocaleConfiguration::toMap() const
return map;
}
QString
LocaleConfiguration::toBcp47() const
{
return myLanguageLocaleBcp47;
}

View File

@ -41,13 +41,21 @@ public:
bool isEmpty() const;
QMap< QString, QString > toMap() const;
/** @brief sets lang and the BCP47 representation
*
* Note that the documentation how this works is in packages.conf
*/
void setLanguage( const QString& localeName );
QString language() const { return m_lang; }
// Note that the documentation how this works is in packages.conf
QString toBcp47() const;
QString toBcp47() const { return m_languageLocaleBcp47; }
QMap< QString, QString > toMap() const;
// These become all uppercase in locale.conf, but we keep them lowercase here to
// avoid confusion with locale.h.
QString lang, lc_numeric, lc_time, lc_monetary, lc_paper, lc_name, lc_address,
QString lc_numeric, lc_time, lc_monetary, lc_paper, lc_name, lc_address,
lc_telephone, lc_measurement, lc_identification;
// If the user has explicitly selected language (from the dialog)
@ -55,20 +63,13 @@ public:
bool explicit_lang, explicit_lc;
private:
/** @brief sets lang and the BCP47 representation
*
* Note that the documentation how this works is in packages.conf
*
* @return The language part of the locale (e.g. before _)
*/
QString setLanguage( const QString& localeName );
QString myLanguageLocaleBcp47;
QString m_lang;
QString m_languageLocaleBcp47;
};
inline QDebug& operator <<( QDebug& s, const LocaleConfiguration& l )
{
return s << l.lang << '(' << l.toBcp47() << ") +" << l.lc_numeric;
return s << l.language() << '(' << l.toBcp47() << ") +" << l.lc_numeric;
}
#endif // LOCALECONFIGURATION_H

View File

@ -164,15 +164,15 @@ LocalePage::LocalePage( QWidget* parent )
{
LCLocaleDialog* dlg =
new LCLocaleDialog( m_selectedLocaleConfiguration.isEmpty() ?
guessLocaleConfiguration().lang :
m_selectedLocaleConfiguration.lang,
guessLocaleConfiguration().language() :
m_selectedLocaleConfiguration.language(),
m_localeGenLines,
this );
dlg->exec();
if ( dlg->result() == QDialog::Accepted &&
!dlg->selectedLCLocale().isEmpty() )
{
m_selectedLocaleConfiguration.lang = dlg->selectedLCLocale();
m_selectedLocaleConfiguration.setLanguage( dlg->selectedLCLocale() );
m_selectedLocaleConfiguration.explicit_lang = true;
this->updateGlobalLocale();
this->updateLocaleLabels();
@ -387,7 +387,7 @@ std::pair< QString, QString > LocalePage::prettyLocaleStatus( const LocaleConfig
{
return std::make_pair< QString, QString >(
tr( "The system language will be set to %1." )
.arg( prettyLCLocale( lc.lang ) ),
.arg( prettyLCLocale( lc.language() ) ),
tr( "The numbers and dates locale will be set to %1." )
.arg( prettyLCLocale( lc.lc_numeric ) )
);
@ -442,7 +442,7 @@ LocalePage::onActivate()
!m_selectedLocaleConfiguration.explicit_lang )
{
auto newLocale = guessLocaleConfiguration();
m_selectedLocaleConfiguration.lang = newLocale.lang;
m_selectedLocaleConfiguration.setLanguage( newLocale.language() );
updateGlobalLocale();
updateLocaleLabels();
}
@ -511,7 +511,7 @@ LocalePage::updateGlobalStorage()
auto newLocale = guessLocaleConfiguration();
if ( !m_selectedLocaleConfiguration.isEmpty() &&
m_selectedLocaleConfiguration.explicit_lang )
newLocale.lang = m_selectedLocaleConfiguration.lang;
newLocale.setLanguage( m_selectedLocaleConfiguration.language() );
if ( !m_selectedLocaleConfiguration.isEmpty() &&
m_selectedLocaleConfiguration.explicit_lc )
{

View File

@ -49,12 +49,12 @@ void LocaleTests::testDefaultLocaleConfiguration()
{
LocaleConfiguration lc( "en_US.UTF-8" );
QVERIFY( !lc.isEmpty() );
QCOMPARE( lc.lang, "en_US.UTF-8" );
QCOMPARE( lc.language(), "en_US.UTF-8" );
QCOMPARE( lc.toBcp47(), "en" );
LocaleConfiguration lc2( "de_DE.UTF-8" );
QVERIFY( !lc2.isEmpty() );
QCOMPARE( lc2.lang, "de_DE.UTF-8" );
QCOMPARE( lc2.language(), "de_DE.UTF-8" );
QCOMPARE( lc2.toBcp47(), "de" );
}
@ -62,13 +62,13 @@ void LocaleTests::testSplitLocaleConfiguration()
{
LocaleConfiguration lc( "en_US.UTF-8", "de_DE.UTF-8" );
QVERIFY( !lc.isEmpty() );
QCOMPARE( lc.lang, "en_US.UTF-8" );
QCOMPARE( lc.language(), "en_US.UTF-8" );
QCOMPARE( lc.toBcp47(), "en" );
QCOMPARE( lc.lc_numeric, QStringLiteral( "de_DE.UTF-8" ) );
LocaleConfiguration lc2( "de_DE.UTF-8", "da_DK.UTF-8" );
QVERIFY( !lc2.isEmpty() );
QCOMPARE( lc2.lang, "de_DE.UTF-8" );
QCOMPARE( lc2.language(), "de_DE.UTF-8" );
QCOMPARE( lc2.toBcp47(), "de" );
QCOMPARE( lc2.lc_numeric, "da_DK.UTF-8" );