[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:
parent
8cc0e1f5f9
commit
f1cbd5fcbf
@ -38,12 +38,12 @@ LocaleConfiguration::LocaleConfiguration( const QString& localeName, const QStri
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QString
|
void
|
||||||
LocaleConfiguration::setLanguage(const QString& localeName )
|
LocaleConfiguration::setLanguage(const QString& localeName )
|
||||||
{
|
{
|
||||||
QString language = localeName.split( '_' ).first();
|
QString language = localeName.split( '_' ).first();
|
||||||
myLanguageLocaleBcp47 = QLocale( language ).bcp47Name().toLower();
|
m_languageLocaleBcp47 = QLocale( language ).bcp47Name().toLower();
|
||||||
return language;
|
m_lang = localeName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -276,7 +276,7 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
|
|||||||
bool
|
bool
|
||||||
LocaleConfiguration::isEmpty() const
|
LocaleConfiguration::isEmpty() const
|
||||||
{
|
{
|
||||||
return lang.isEmpty() &&
|
return m_lang.isEmpty() &&
|
||||||
lc_numeric.isEmpty() &&
|
lc_numeric.isEmpty() &&
|
||||||
lc_time.isEmpty() &&
|
lc_time.isEmpty() &&
|
||||||
lc_monetary.isEmpty() &&
|
lc_monetary.isEmpty() &&
|
||||||
@ -294,8 +294,8 @@ LocaleConfiguration::toMap() const
|
|||||||
{
|
{
|
||||||
QMap< QString, QString > map;
|
QMap< QString, QString > map;
|
||||||
|
|
||||||
if ( !lang.isEmpty() )
|
if ( !m_lang.isEmpty() )
|
||||||
map.insert( "LANG", lang );
|
map.insert( "LANG", m_lang );
|
||||||
|
|
||||||
if ( !lc_numeric.isEmpty() )
|
if ( !lc_numeric.isEmpty() )
|
||||||
map.insert( "LC_NUMERIC", lc_numeric );
|
map.insert( "LC_NUMERIC", lc_numeric );
|
||||||
@ -327,8 +327,3 @@ LocaleConfiguration::toMap() const
|
|||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString
|
|
||||||
LocaleConfiguration::toBcp47() const
|
|
||||||
{
|
|
||||||
return myLanguageLocaleBcp47;
|
|
||||||
}
|
|
||||||
|
@ -41,13 +41,21 @@ public:
|
|||||||
|
|
||||||
bool isEmpty() const;
|
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
|
// 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
|
// These become all uppercase in locale.conf, but we keep them lowercase here to
|
||||||
// avoid confusion with locale.h.
|
// 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;
|
lc_telephone, lc_measurement, lc_identification;
|
||||||
|
|
||||||
// If the user has explicitly selected language (from the dialog)
|
// If the user has explicitly selected language (from the dialog)
|
||||||
@ -55,20 +63,13 @@ public:
|
|||||||
bool explicit_lang, explicit_lc;
|
bool explicit_lang, explicit_lc;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/** @brief sets lang and the BCP47 representation
|
QString m_lang;
|
||||||
*
|
QString m_languageLocaleBcp47;
|
||||||
* 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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline QDebug& operator <<( QDebug& s, const LocaleConfiguration& l )
|
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
|
#endif // LOCALECONFIGURATION_H
|
||||||
|
@ -164,15 +164,15 @@ LocalePage::LocalePage( QWidget* parent )
|
|||||||
{
|
{
|
||||||
LCLocaleDialog* dlg =
|
LCLocaleDialog* dlg =
|
||||||
new LCLocaleDialog( m_selectedLocaleConfiguration.isEmpty() ?
|
new LCLocaleDialog( m_selectedLocaleConfiguration.isEmpty() ?
|
||||||
guessLocaleConfiguration().lang :
|
guessLocaleConfiguration().language() :
|
||||||
m_selectedLocaleConfiguration.lang,
|
m_selectedLocaleConfiguration.language(),
|
||||||
m_localeGenLines,
|
m_localeGenLines,
|
||||||
this );
|
this );
|
||||||
dlg->exec();
|
dlg->exec();
|
||||||
if ( dlg->result() == QDialog::Accepted &&
|
if ( dlg->result() == QDialog::Accepted &&
|
||||||
!dlg->selectedLCLocale().isEmpty() )
|
!dlg->selectedLCLocale().isEmpty() )
|
||||||
{
|
{
|
||||||
m_selectedLocaleConfiguration.lang = dlg->selectedLCLocale();
|
m_selectedLocaleConfiguration.setLanguage( dlg->selectedLCLocale() );
|
||||||
m_selectedLocaleConfiguration.explicit_lang = true;
|
m_selectedLocaleConfiguration.explicit_lang = true;
|
||||||
this->updateGlobalLocale();
|
this->updateGlobalLocale();
|
||||||
this->updateLocaleLabels();
|
this->updateLocaleLabels();
|
||||||
@ -387,7 +387,7 @@ std::pair< QString, QString > LocalePage::prettyLocaleStatus( const LocaleConfig
|
|||||||
{
|
{
|
||||||
return std::make_pair< QString, QString >(
|
return std::make_pair< QString, QString >(
|
||||||
tr( "The system language will be set to %1." )
|
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." )
|
tr( "The numbers and dates locale will be set to %1." )
|
||||||
.arg( prettyLCLocale( lc.lc_numeric ) )
|
.arg( prettyLCLocale( lc.lc_numeric ) )
|
||||||
);
|
);
|
||||||
@ -442,7 +442,7 @@ LocalePage::onActivate()
|
|||||||
!m_selectedLocaleConfiguration.explicit_lang )
|
!m_selectedLocaleConfiguration.explicit_lang )
|
||||||
{
|
{
|
||||||
auto newLocale = guessLocaleConfiguration();
|
auto newLocale = guessLocaleConfiguration();
|
||||||
m_selectedLocaleConfiguration.lang = newLocale.lang;
|
m_selectedLocaleConfiguration.setLanguage( newLocale.language() );
|
||||||
updateGlobalLocale();
|
updateGlobalLocale();
|
||||||
updateLocaleLabels();
|
updateLocaleLabels();
|
||||||
}
|
}
|
||||||
@ -511,7 +511,7 @@ LocalePage::updateGlobalStorage()
|
|||||||
auto newLocale = guessLocaleConfiguration();
|
auto newLocale = guessLocaleConfiguration();
|
||||||
if ( !m_selectedLocaleConfiguration.isEmpty() &&
|
if ( !m_selectedLocaleConfiguration.isEmpty() &&
|
||||||
m_selectedLocaleConfiguration.explicit_lang )
|
m_selectedLocaleConfiguration.explicit_lang )
|
||||||
newLocale.lang = m_selectedLocaleConfiguration.lang;
|
newLocale.setLanguage( m_selectedLocaleConfiguration.language() );
|
||||||
if ( !m_selectedLocaleConfiguration.isEmpty() &&
|
if ( !m_selectedLocaleConfiguration.isEmpty() &&
|
||||||
m_selectedLocaleConfiguration.explicit_lc )
|
m_selectedLocaleConfiguration.explicit_lc )
|
||||||
{
|
{
|
||||||
|
@ -49,12 +49,12 @@ void LocaleTests::testDefaultLocaleConfiguration()
|
|||||||
{
|
{
|
||||||
LocaleConfiguration lc( "en_US.UTF-8" );
|
LocaleConfiguration lc( "en_US.UTF-8" );
|
||||||
QVERIFY( !lc.isEmpty() );
|
QVERIFY( !lc.isEmpty() );
|
||||||
QCOMPARE( lc.lang, "en_US.UTF-8" );
|
QCOMPARE( lc.language(), "en_US.UTF-8" );
|
||||||
QCOMPARE( lc.toBcp47(), "en" );
|
QCOMPARE( lc.toBcp47(), "en" );
|
||||||
|
|
||||||
LocaleConfiguration lc2( "de_DE.UTF-8" );
|
LocaleConfiguration lc2( "de_DE.UTF-8" );
|
||||||
QVERIFY( !lc2.isEmpty() );
|
QVERIFY( !lc2.isEmpty() );
|
||||||
QCOMPARE( lc2.lang, "de_DE.UTF-8" );
|
QCOMPARE( lc2.language(), "de_DE.UTF-8" );
|
||||||
QCOMPARE( lc2.toBcp47(), "de" );
|
QCOMPARE( lc2.toBcp47(), "de" );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,13 +62,13 @@ void LocaleTests::testSplitLocaleConfiguration()
|
|||||||
{
|
{
|
||||||
LocaleConfiguration lc( "en_US.UTF-8", "de_DE.UTF-8" );
|
LocaleConfiguration lc( "en_US.UTF-8", "de_DE.UTF-8" );
|
||||||
QVERIFY( !lc.isEmpty() );
|
QVERIFY( !lc.isEmpty() );
|
||||||
QCOMPARE( lc.lang, "en_US.UTF-8" );
|
QCOMPARE( lc.language(), "en_US.UTF-8" );
|
||||||
QCOMPARE( lc.toBcp47(), "en" );
|
QCOMPARE( lc.toBcp47(), "en" );
|
||||||
QCOMPARE( lc.lc_numeric, QStringLiteral( "de_DE.UTF-8" ) );
|
QCOMPARE( lc.lc_numeric, QStringLiteral( "de_DE.UTF-8" ) );
|
||||||
|
|
||||||
LocaleConfiguration lc2( "de_DE.UTF-8", "da_DK.UTF-8" );
|
LocaleConfiguration lc2( "de_DE.UTF-8", "da_DK.UTF-8" );
|
||||||
QVERIFY( !lc2.isEmpty() );
|
QVERIFY( !lc2.isEmpty() );
|
||||||
QCOMPARE( lc2.lang, "de_DE.UTF-8" );
|
QCOMPARE( lc2.language(), "de_DE.UTF-8" );
|
||||||
QCOMPARE( lc2.toBcp47(), "de" );
|
QCOMPARE( lc2.toBcp47(), "de" );
|
||||||
QCOMPARE( lc2.lc_numeric, "da_DK.UTF-8" );
|
QCOMPARE( lc2.lc_numeric, "da_DK.UTF-8" );
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user