OSDN Git Service

[libcalamares] Use strong types for locale Ids
authorAdriaan de Groot <groot@kde.org>
Tue, 7 Sep 2021 10:51:57 +0000 (12:51 +0200)
committerAdriaan de Groot <groot@kde.org>
Tue, 7 Sep 2021 10:51:57 +0000 (12:51 +0200)
Change the API to force strong type for more methods.
This cascades to a couple of consumers.

src/libcalamares/locale/Translation.cpp
src/libcalamares/locale/Translation.h
src/libcalamares/locale/TranslationsModel.cpp
src/modules/locale/Config.cpp

index 4afb1bb..fb8890e 100644 (file)
@@ -25,8 +25,9 @@
  * Returns a pair of nullptrs for non-special cases.
  */
 static std::pair< QLocale*, QString* >
-specialCase( const QString& localeName )
+specialCase( const CalamaresUtils::Locale::Translation::Id& locale )
 {
+    const QString localeName = locale.name;
     if ( localeName == "sr@latin" )
     {
         static QLocale loc( QLocale::Language::Serbian, QLocale::Script::LatinScript, QLocale::Country::Serbia );
@@ -47,16 +48,16 @@ namespace Locale
 {
 
 Translation::Translation( QObject* parent )
-    : Translation( QString(), LabelFormat::IfNeededWithCountry, parent )
+    : Translation( { QString() }, LabelFormat::IfNeededWithCountry, parent )
 {
 }
 
-Translation::Translation( const QString& locale, LabelFormat format, QObject* parent )
+Translation::Translation( const Id& localeId, LabelFormat format, QObject* parent )
     : QObject( parent )
-    , m_locale( getLocale( locale ) )
-    , m_localeId( locale.isEmpty() ? m_locale.name() : locale )
+    , m_locale( getLocale( localeId ) )
+    , m_localeId( localeId.name.isEmpty() ? m_locale.name() : localeId.name )
 {
-    auto [ _, name ] = specialCase( locale );
+    auto [ _, name ] = specialCase( localeId );
 
     QString longFormat = QObject::tr( "%1 (%2)" );
 
@@ -65,11 +66,11 @@ Translation::Translation( const QString& locale, LabelFormat format, QObject* pa
 
     if ( languageName.isEmpty() )
     {
-        languageName = QString( "* %1 (%2)" ).arg( locale, englishName );
+        languageName = QString( "* %1 (%2)" ).arg( localeId.name, englishName );
     }
 
     bool needsCountryName = ( format == LabelFormat::AlwaysWithCountry )
-        || ( locale.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 1 );
+        || ( localeId.name.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 1 );
     QString countryName = ( needsCountryName ?
 
                                              m_locale.nativeCountryName()
@@ -80,14 +81,15 @@ Translation::Translation( const QString& locale, LabelFormat format, QObject* pa
 }
 
 QLocale
-Translation::getLocale( const QString& localeName )
+Translation::getLocale( const Id& localeId )
 {
+    const QString& localeName = localeId.name;
     if ( localeName.isEmpty() )
     {
         return QLocale();
     }
 
-    auto [ locale, _ ] = specialCase( localeName );
+    auto [ locale, _ ] = specialCase( localeId );
     return locale ? *locale : QLocale( localeName );
 }
 
index 39b1773..5e5ce33 100644 (file)
@@ -60,9 +60,7 @@ public:
      * The @p format determines whether the country name is always present
      * in the label (human-readable form) or only if needed for disambiguation.
      */
-    Translation( const QString& localeName,
-                 LabelFormat format = LabelFormat::IfNeededWithCountry,
-                 QObject* parent = nullptr );
+    Translation( const Id& localeId, LabelFormat format = LabelFormat::IfNeededWithCountry, QObject* parent = nullptr );
 
 
     /** @brief Define a sorting order.
@@ -103,7 +101,7 @@ public:
      *
      * This obeys special cases as described in the class documentation.
      */
-    static QLocale getLocale( const QString& localeName );
+    static QLocale getLocale( const Id& localeId );
 
 private:
     QLocale m_locale;
index af1bd18..25d075d 100644 (file)
@@ -29,7 +29,7 @@ TranslationsModel::TranslationsModel( const QStringList& locales, QObject* paren
 
     for ( const auto& l : locales )
     {
-        m_locales.push_back( new Translation( l, Translation::LabelFormat::IfNeededWithCountry, this ) );
+        m_locales.push_back( new Translation( { l }, Translation::LabelFormat::IfNeededWithCountry, this ) );
     }
 }
 
index 9627fcf..ce48edd 100644 (file)
@@ -370,7 +370,7 @@ localeLabel( const QString& s )
 {
     using CalamaresUtils::Locale::Translation;
 
-    Translation lang( s, Translation::LabelFormat::AlwaysWithCountry );
+    Translation lang( { s }, Translation::LabelFormat::AlwaysWithCountry );
     return lang.label();
 }