[locale] Refactor setting the BCP47 name and update test

This commit is contained in:
Adriaan de Groot 2019-01-08 11:39:15 +01:00
parent b1921cced9
commit 0a526febae
3 changed files with 21 additions and 6 deletions

View File

@ -30,9 +30,19 @@ LocaleConfiguration::LocaleConfiguration()
LocaleConfiguration::LocaleConfiguration( const QString& localeName ) LocaleConfiguration::LocaleConfiguration( const QString& localeName )
: LocaleConfiguration() : LocaleConfiguration()
{ {
lang = lc_numeric = lc_time = lc_monetary = lc_paper = lc_name lc_numeric = lc_time = lc_monetary = lc_paper = lc_name
= lc_address = lc_telephone = lc_measurement = lc_address = lc_telephone = lc_measurement
= lc_identification = localeName; = lc_identification = localeName;
(void) setLanguage( localeName );
}
QString
LocaleConfiguration::setLanguage(const QString& localeName )
{
QString language = localeName.split( '_' ).first();
myLanguageLocaleBcp47 = QLocale( language ).bcp47Name().toLower();
return language;
} }
@ -42,10 +52,7 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
const QString& countryCode ) const QString& countryCode )
{ {
LocaleConfiguration lc; LocaleConfiguration lc;
QString language = lc.setLanguage( languageLocale );
// Note that the documentation how this works is in packages.conf
QString language = languageLocale.split( '_' ).first();
lc.myLanguageLocaleBcp47 = QLocale(language).bcp47Name().toLower();
QStringList linesForLanguage; QStringList linesForLanguage;
for ( const QString &line : availableLocales ) for ( const QString &line : availableLocales )

View File

@ -51,6 +51,14 @@ public:
bool explicit_lang, explicit_lc; bool explicit_lang, explicit_lc;
private: 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 myLanguageLocaleBcp47;
}; };

View File

@ -49,5 +49,5 @@ void LocaleTests::testDefaultLocaleConfiguration()
{ {
LocaleConfiguration lc( "en_US.UTF-8" ); LocaleConfiguration lc( "en_US.UTF-8" );
QVERIFY( !lc.isEmpty() ); QVERIFY( !lc.isEmpty() );
QCOMPARE( lc.toBcp47(), "en_US" ); QCOMPARE( lc.toBcp47(), "en" );
} }