[locale] Improve LocaleConfiguration constructors

- Allow split-setting of the language and formats
 - Test new constructors
 - Since fromLanguageAndLocation can handle empty localeGen
   lists just fine, skip all the weird checks that return
   invalid guessed locale configurations.
This commit is contained in:
Adriaan de Groot 2019-01-08 13:23:16 +01:00
parent 0a526febae
commit dbe50fe3db
5 changed files with 34 additions and 27 deletions

View File

@ -27,16 +27,17 @@ LocaleConfiguration::LocaleConfiguration()
}
LocaleConfiguration::LocaleConfiguration( const QString& localeName )
LocaleConfiguration::LocaleConfiguration( const QString& localeName, const QString& formatsName )
: LocaleConfiguration()
{
lc_numeric = lc_time = lc_monetary = lc_paper = lc_name
= lc_address = lc_telephone = lc_measurement
= lc_identification = localeName;
= lc_identification = formatsName;
(void) setLanguage( localeName );
}
QString
LocaleConfiguration::setLanguage(const QString& localeName )
{
@ -51,8 +52,7 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
const QStringList& availableLocales,
const QString& countryCode )
{
LocaleConfiguration lc;
QString language = lc.setLanguage( languageLocale );
QString language = languageLocale.split( '_' ).first();
QStringList linesForLanguage;
for ( const QString &line : availableLocales )
@ -269,12 +269,7 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
if ( lc_formats.isEmpty() )
lc_formats = lang;
lc.lang = lang;
lc.lc_address = lc.lc_identification = lc.lc_measurement = lc.lc_monetary
= lc.lc_name = lc.lc_numeric = lc.lc_paper = lc.lc_telephone
= lc.lc_time = lc_formats;
return lc;
return LocaleConfiguration( lang, lc_formats );
}

View File

@ -29,7 +29,10 @@ public:
/// @brief Create an empty locale, with nothing set
explicit LocaleConfiguration();
/// @brief Create a locale with everything set to the given @p localeName
explicit LocaleConfiguration( const QString& localeName /* "en_US.UTF-8" */ );
explicit LocaleConfiguration( const QString& localeName /* "en_US.UTF-8" */ )
: LocaleConfiguration( localeName, localeName ) { };
/// @brief Create a locale with language and formats separate
explicit LocaleConfiguration( const QString& localeName, const QString& formatsName );
static LocaleConfiguration fromLanguageAndLocation( const QString& language,
const QStringList& availableLocales,

View File

@ -452,22 +452,7 @@ LocalePage::onActivate()
LocaleConfiguration
LocalePage::guessLocaleConfiguration() const
{
static QString defaultLocale = QStringLiteral( "en_US.UTF-8" );
QLocale myLocale; // User-selected language
// If we cannot say anything about available locales
if ( m_localeGenLines.isEmpty() )
{
cWarning() << "guessLocaleConfiguration can't guess from an empty list.";
return LocaleConfiguration( defaultLocale );
}
QString myLanguageLocale = myLocale.name();
if ( myLanguageLocale.isEmpty() )
return LocaleConfiguration( defaultLocale );
return LocaleConfiguration::fromLanguageAndLocation( myLanguageLocale,
return LocaleConfiguration::fromLanguageAndLocation( QLocale().name(),
m_localeGenLines,
m_tzWidget->getCurrentLocation().country );
}

View File

@ -50,4 +50,27 @@ void LocaleTests::testDefaultLocaleConfiguration()
LocaleConfiguration lc( "en_US.UTF-8" );
QVERIFY( !lc.isEmpty() );
QCOMPARE( lc.toBcp47(), "en" );
LocaleConfiguration lc2( "de_DE.UTF-8" );
QVERIFY( !lc2.isEmpty() );
QCOMPARE( lc2.toBcp47(), "de" );
}
void LocaleTests::testSplitLocaleConfiguration()
{
LocaleConfiguration lc( "en_US.UTF-8", "de_DE.UTF-8" );
QVERIFY( !lc.isEmpty() );
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.toBcp47(), "de" );
QCOMPARE( lc2.lc_numeric, "da_DK.UTF-8" );
LocaleConfiguration lc3( "da_DK.UTF-8", "de_DE.UTF-8" );
QVERIFY( !lc3.isEmpty() );
QCOMPARE( lc3.toBcp47(), "da" );
QCOMPARE( lc3.lc_numeric, "de_DE.UTF-8" );
}

View File

@ -34,6 +34,7 @@ private Q_SLOTS:
// Check the sample config file is processed correctly
void testEmptyLocaleConfiguration();
void testDefaultLocaleConfiguration();
void testSplitLocaleConfiguration();
};
#endif