From bf9f1c95bc603cf4e9d74a6346bbea271f01e7ba Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 26 Jul 2021 11:28:11 +0200 Subject: [PATCH 1/8] [libcalamares] Rename classes describing Translations - the name 'Label' was a relic of the class being UI-centered --- src/libcalamares/locale/Label.cpp | 10 +++---- src/libcalamares/locale/Label.h | 10 +++---- src/libcalamares/locale/LabelModel.cpp | 36 +++++++++++++------------- src/libcalamares/locale/LabelModel.h | 14 +++++----- src/modules/locale/Config.cpp | 4 +-- src/modules/welcome/Config.cpp | 2 +- src/modules/welcome/Config.h | 6 ++--- src/modules/welcome/WelcomePage.cpp | 2 +- src/modules/welcome/WelcomePage.h | 2 +- 9 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/libcalamares/locale/Label.cpp b/src/libcalamares/locale/Label.cpp index e2cfbc70a..f79d0d3e5 100644 --- a/src/libcalamares/locale/Label.cpp +++ b/src/libcalamares/locale/Label.cpp @@ -46,14 +46,14 @@ namespace CalamaresUtils namespace Locale { -Label::Label( QObject* parent ) - : Label( QString(), LabelFormat::IfNeededWithCountry, parent ) +Translation::Translation( QObject* parent ) + : Translation( QString(), LabelFormat::IfNeededWithCountry, parent ) { } -Label::Label( const QString& locale, LabelFormat format, QObject* parent ) +Translation::Translation( const QString& locale, LabelFormat format, QObject* parent ) : QObject( parent ) - , m_locale( Label::getLocale( locale ) ) + , m_locale( getLocale( locale ) ) , m_localeId( locale.isEmpty() ? m_locale.name() : locale ) { auto special = specialCase( locale ); @@ -80,7 +80,7 @@ Label::Label( const QString& locale, LabelFormat format, QObject* parent ) } QLocale -Label::getLocale( const QString& localeName ) +Translation::getLocale( const QString& localeName ) { if ( localeName.isEmpty() ) { diff --git a/src/libcalamares/locale/Label.h b/src/libcalamares/locale/Label.h index b0f17930a..04ad4f5e8 100644 --- a/src/libcalamares/locale/Label.h +++ b/src/libcalamares/locale/Label.h @@ -34,7 +34,7 @@ namespace Locale * - `ca@valencia` is the Catalan dialect spoken in Valencia. * There is no Qt code for it. */ -class Label : public QObject +class Translation : public QObject { Q_OBJECT @@ -47,7 +47,7 @@ public: }; /** @brief Empty locale. This uses the system-default locale. */ - Label( QObject* parent = nullptr ); + Translation( QObject* parent = nullptr ); /** @brief Construct from a locale name. * @@ -55,7 +55,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. */ - Label( const QString& localeName, + Translation( const QString& localeName, LabelFormat format = LabelFormat::IfNeededWithCountry, QObject* parent = nullptr ); @@ -64,7 +64,7 @@ public: * * Locales are sorted by their id, which means the ISO 2-letter code + country. */ - bool operator<( const Label& other ) const { return m_localeId < other.m_localeId; } + bool operator<( const Translation& other ) const { return m_localeId < other.m_localeId; } /** @brief Is this locale English? * @@ -96,7 +96,7 @@ public: */ static QLocale getLocale( const QString& localeName ); -protected: +private: QLocale m_locale; QString m_localeId; // the locale identifier, e.g. "en_GB" QString m_label; // the native name of the locale diff --git a/src/libcalamares/locale/LabelModel.cpp b/src/libcalamares/locale/LabelModel.cpp index 9a9be9905..93d0aab3e 100644 --- a/src/libcalamares/locale/LabelModel.cpp +++ b/src/libcalamares/locale/LabelModel.cpp @@ -20,7 +20,7 @@ namespace CalamaresUtils namespace Locale { -LabelModel::LabelModel( const QStringList& locales, QObject* parent ) +TranslationsModel::TranslationsModel( const QStringList& locales, QObject* parent ) : QAbstractListModel( parent ) , m_localeIds( locales ) { @@ -29,20 +29,20 @@ LabelModel::LabelModel( const QStringList& locales, QObject* parent ) for ( const auto& l : locales ) { - m_locales.push_back( new Label( l, Label::LabelFormat::IfNeededWithCountry, this ) ); + m_locales.push_back( new Translation( l, Translation::LabelFormat::IfNeededWithCountry, this ) ); } } -LabelModel::~LabelModel() {} +TranslationsModel::~TranslationsModel() {} int -LabelModel::rowCount( const QModelIndex& ) const +TranslationsModel::rowCount( const QModelIndex& ) const { return m_locales.count(); } QVariant -LabelModel::data( const QModelIndex& index, int role ) const +TranslationsModel::data( const QModelIndex& index, int role ) const { if ( ( role != LabelRole ) && ( role != EnglishLabelRole ) ) { @@ -67,13 +67,13 @@ LabelModel::data( const QModelIndex& index, int role ) const } QHash< int, QByteArray > -LabelModel::roleNames() const +TranslationsModel::roleNames() const { return { { LabelRole, "label" }, { EnglishLabelRole, "englishLabel" } }; } -const Label& -LabelModel::locale( int row ) const +const Translation& +TranslationsModel::locale( int row ) const { if ( ( row < 0 ) || ( row >= m_locales.count() ) ) { @@ -88,7 +88,7 @@ LabelModel::locale( int row ) const } int -LabelModel::find( std::function< bool( const Label& ) > predicate ) const +TranslationsModel::find( std::function< bool( const Translation& ) > predicate ) const { for ( int row = 0; row < m_locales.count(); ++row ) { @@ -101,19 +101,19 @@ LabelModel::find( std::function< bool( const Label& ) > predicate ) const } int -LabelModel::find( std::function< bool( const QLocale& ) > predicate ) const +TranslationsModel::find( std::function< bool( const QLocale& ) > predicate ) const { - return find( [&]( const Label& l ) { return predicate( l.locale() ); } ); + return find( [&]( const Translation& l ) { return predicate( l.locale() ); } ); } int -LabelModel::find( const QLocale& locale ) const +TranslationsModel::find( const QLocale& locale ) const { - return find( [&]( const Label& l ) { return locale == l.locale(); } ); + return find( [&]( const Translation& l ) { return locale == l.locale(); } ); } int -LabelModel::find( const QString& countryCode ) const +TranslationsModel::find( const QString& countryCode ) const { if ( countryCode.length() != 2 ) { @@ -121,18 +121,18 @@ LabelModel::find( const QString& countryCode ) const } auto c_l = countryData( countryCode ); - int r = find( [&]( const Label& l ) { return ( l.language() == c_l.second ) && ( l.country() == c_l.first ); } ); + int r = find( [&]( const Translation& l ) { return ( l.language() == c_l.second ) && ( l.country() == c_l.first ); } ); if ( r >= 0 ) { return r; } - return find( [&]( const Label& l ) { return l.language() == c_l.second; } ); + return find( [&]( const Translation& l ) { return l.language() == c_l.second; } ); } -LabelModel* +TranslationsModel* availableTranslations() { - static LabelModel* model = new LabelModel( QStringLiteral( CALAMARES_TRANSLATION_LANGUAGES ).split( ';' ) ); + static TranslationsModel* model = new TranslationsModel( QStringLiteral( CALAMARES_TRANSLATION_LANGUAGES ).split( ';' ) ); return model; } diff --git a/src/libcalamares/locale/LabelModel.h b/src/libcalamares/locale/LabelModel.h index 7e6f2dacc..1d12295b3 100644 --- a/src/libcalamares/locale/LabelModel.h +++ b/src/libcalamares/locale/LabelModel.h @@ -24,7 +24,7 @@ namespace CalamaresUtils namespace Locale { -class DLLEXPORT LabelModel : public QAbstractListModel +class DLLEXPORT TranslationsModel : public QAbstractListModel { Q_OBJECT @@ -35,8 +35,8 @@ public: EnglishLabelRole = Qt::UserRole + 1 }; - LabelModel( const QStringList& locales, QObject* parent = nullptr ); - ~LabelModel() override; + TranslationsModel( const QStringList& locales, QObject* parent = nullptr ); + ~TranslationsModel() override; int rowCount( const QModelIndex& parent ) const override; @@ -48,7 +48,7 @@ public: * This is the backing data for the model; if @p row is out-of-range, * returns a reference to en_US. */ - const Label& locale( int row ) const; + const Translation& locale( int row ) const; /// @brief Returns all of the locale Ids (e.g. en_US) put into this model. const QStringList& localeIds() const { return m_localeIds; } @@ -58,14 +58,14 @@ public: * Returns the row number of the first match, or -1 if there isn't one. */ int find( std::function< bool( const QLocale& ) > predicate ) const; - int find( std::function< bool( const Label& ) > predicate ) const; + int find( std::function< bool( const Translation& ) > predicate ) const; /// @brief Looks for an item using the same locale, -1 if there isn't one int find( const QLocale& ) const; /// @brief Looks for an item that best matches the 2-letter country code int find( const QString& countryCode ) const; private: - QVector< Label* > m_locales; + QVector< Translation* > m_locales; QStringList m_localeIds; }; @@ -79,7 +79,7 @@ private: * * NOTE: While the model is not typed const, it should be. Do not modify. */ -DLLEXPORT LabelModel* availableTranslations(); +DLLEXPORT TranslationsModel* availableTranslations(); } // namespace Locale } // namespace CalamaresUtils #endif diff --git a/src/modules/locale/Config.cpp b/src/modules/locale/Config.cpp index 854d65eef..700011258 100644 --- a/src/modules/locale/Config.cpp +++ b/src/modules/locale/Config.cpp @@ -368,9 +368,9 @@ Config::currentTimezoneName() const static inline QString localeLabel( const QString& s ) { - using CalamaresUtils::Locale::Label; + using CalamaresUtils::Locale::Translation; - Label lang( s, Label::LabelFormat::AlwaysWithCountry ); + Translation lang( s, Translation::LabelFormat::AlwaysWithCountry ); return lang.label(); } diff --git a/src/modules/welcome/Config.cpp b/src/modules/welcome/Config.cpp index bc489d186..756cb0e5a 100644 --- a/src/modules/welcome/Config.cpp +++ b/src/modules/welcome/Config.cpp @@ -84,7 +84,7 @@ Config::retranslate() emit warningMessageChanged( m_warningMessage ); } -CalamaresUtils::Locale::LabelModel* +CalamaresUtils::Locale::TranslationsModel* Config::languagesModel() const { return m_languages; diff --git a/src/modules/welcome/Config.h b/src/modules/welcome/Config.h index a3f1276a6..2597ccb5a 100644 --- a/src/modules/welcome/Config.h +++ b/src/modules/welcome/Config.h @@ -27,7 +27,7 @@ class Config : public QObject * This is a list-model, with names and descriptions for the translations * available to Calamares. */ - Q_PROPERTY( CalamaresUtils::Locale::LabelModel* languagesModel READ languagesModel CONSTANT FINAL ) + Q_PROPERTY( CalamaresUtils::Locale::TranslationsModel* languagesModel READ languagesModel CONSTANT FINAL ) /** @brief The requirements (from modules) and their checked-status * * The model grows rows over time as each module is checked and its @@ -92,7 +92,7 @@ public: QString warningMessage() const; public slots: - CalamaresUtils::Locale::LabelModel* languagesModel() const; + CalamaresUtils::Locale::TranslationsModel* languagesModel() const; void retranslate(); ///@brief The **global** requirements model, from ModuleManager @@ -116,7 +116,7 @@ signals: private: void initLanguages(); - CalamaresUtils::Locale::LabelModel* m_languages = nullptr; + CalamaresUtils::Locale::TranslationsModel* m_languages = nullptr; std::unique_ptr< QSortFilterProxyModel > m_filtermodel; QString m_languageIcon; diff --git a/src/modules/welcome/WelcomePage.cpp b/src/modules/welcome/WelcomePage.cpp index 376fe1ea6..b56b6754a 100644 --- a/src/modules/welcome/WelcomePage.cpp +++ b/src/modules/welcome/WelcomePage.cpp @@ -275,5 +275,5 @@ LocaleTwoColumnDelegate::paint( QPainter* painter, const QStyleOptionViewItem& o Qt::AlignRight | Qt::AlignVCenter, option.palette, false, - index.data( CalamaresUtils::Locale::LabelModel::EnglishLabelRole ).toString() ); + index.data( CalamaresUtils::Locale::TranslationsModel::EnglishLabelRole ).toString() ); } diff --git a/src/modules/welcome/WelcomePage.h b/src/modules/welcome/WelcomePage.h index cbbc7f510..ceeb5160b 100644 --- a/src/modules/welcome/WelcomePage.h +++ b/src/modules/welcome/WelcomePage.h @@ -64,7 +64,7 @@ private: Ui::WelcomePage* ui; CheckerContainer* m_checkingWidget; - CalamaresUtils::Locale::LabelModel* m_languages; + CalamaresUtils::Locale::TranslationsModel* m_languages; Config* m_conf; }; From bc9d8fb13a2a9f703ac4517eb0c10f9ea4612fd9 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 26 Jul 2021 11:38:15 +0200 Subject: [PATCH 2/8] [libcalamares] Rename files Label -> Translation --- src/libcalamares/CMakeLists.txt | 4 ++-- src/libcalamares/locale/Tests.cpp | 2 +- src/libcalamares/locale/TranslatableConfiguration.cpp | 3 ++- src/libcalamares/locale/{Label.cpp => Translation.cpp} | 2 +- src/libcalamares/locale/{Label.h => Translation.h} | 8 ++++---- .../locale/{LabelModel.cpp => TranslationsModel.cpp} | 8 +++++--- .../locale/{LabelModel.h => TranslationsModel.h} | 6 +++--- src/modules/locale/Config.cpp | 2 +- src/modules/welcome/Config.h | 2 +- src/modules/welcome/WelcomePage.cpp | 1 - src/modules/welcome/WelcomePage.h | 2 +- src/modules/welcomeq/WelcomeQmlViewStep.cpp | 2 +- 12 files changed, 22 insertions(+), 20 deletions(-) rename src/libcalamares/locale/{Label.cpp => Translation.cpp} (99%) rename src/libcalamares/locale/{Label.h => Translation.h} (95%) rename src/libcalamares/locale/{LabelModel.cpp => TranslationsModel.cpp} (90%) rename src/libcalamares/locale/{LabelModel.h => TranslationsModel.h} (96%) diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index d4755bde4..645a26d25 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -40,12 +40,12 @@ set( libSources # Locale-data service locale/Global.cpp - locale/Label.cpp - locale/LabelModel.cpp locale/Lookup.cpp locale/TimeZone.cpp locale/TranslatableConfiguration.cpp locale/TranslatableString.cpp + locale/Translation.cpp + locale/TranslationsModel.cpp # Modules modulesystem/Config.cpp diff --git a/src/libcalamares/locale/Tests.cpp b/src/libcalamares/locale/Tests.cpp index 05e8f610c..f234c1596 100644 --- a/src/libcalamares/locale/Tests.cpp +++ b/src/libcalamares/locale/Tests.cpp @@ -9,9 +9,9 @@ */ #include "locale/Global.h" -#include "locale/LabelModel.h" #include "locale/TimeZone.h" #include "locale/TranslatableConfiguration.h" +#include "locale/TranslationsModel.h" #include "CalamaresVersion.h" #include "GlobalStorage.h" diff --git a/src/libcalamares/locale/TranslatableConfiguration.cpp b/src/libcalamares/locale/TranslatableConfiguration.cpp index c10307aee..3a95722d1 100644 --- a/src/libcalamares/locale/TranslatableConfiguration.cpp +++ b/src/libcalamares/locale/TranslatableConfiguration.cpp @@ -10,7 +10,7 @@ #include "TranslatableConfiguration.h" -#include "LabelModel.h" +#include "TranslationsModel.h" #include "utils/Logger.h" #include "utils/Variant.h" @@ -69,6 +69,7 @@ TranslatedString::get() const QString TranslatedString::get( const QLocale& locale ) const { + // TODO: keep track of special cases like sr@latin and ca@valencia QString localeName = locale.name(); // Special case, sr@latin doesn't have the @latin reflected in the name if ( locale.language() == QLocale::Language::Serbian && locale.script() == QLocale::Script::LatinScript ) diff --git a/src/libcalamares/locale/Label.cpp b/src/libcalamares/locale/Translation.cpp similarity index 99% rename from src/libcalamares/locale/Label.cpp rename to src/libcalamares/locale/Translation.cpp index f79d0d3e5..0a2e594be 100644 --- a/src/libcalamares/locale/Label.cpp +++ b/src/libcalamares/locale/Translation.cpp @@ -9,7 +9,7 @@ * */ -#include "Label.h" +#include "Translation.h" #include diff --git a/src/libcalamares/locale/Label.h b/src/libcalamares/locale/Translation.h similarity index 95% rename from src/libcalamares/locale/Label.h rename to src/libcalamares/locale/Translation.h index 04ad4f5e8..a4402ebaa 100644 --- a/src/libcalamares/locale/Label.h +++ b/src/libcalamares/locale/Translation.h @@ -9,8 +9,8 @@ * */ -#ifndef LOCALE_LABEL_H -#define LOCALE_LABEL_H +#ifndef LOCALE_TRANSLATION_H +#define LOCALE_TRANSLATION_H #include #include @@ -56,8 +56,8 @@ public: * in the label (human-readable form) or only if needed for disambiguation. */ Translation( const QString& localeName, - LabelFormat format = LabelFormat::IfNeededWithCountry, - QObject* parent = nullptr ); + LabelFormat format = LabelFormat::IfNeededWithCountry, + QObject* parent = nullptr ); /** @brief Define a sorting order. diff --git a/src/libcalamares/locale/LabelModel.cpp b/src/libcalamares/locale/TranslationsModel.cpp similarity index 90% rename from src/libcalamares/locale/LabelModel.cpp rename to src/libcalamares/locale/TranslationsModel.cpp index 93d0aab3e..af1bd1801 100644 --- a/src/libcalamares/locale/LabelModel.cpp +++ b/src/libcalamares/locale/TranslationsModel.cpp @@ -9,7 +9,7 @@ * */ -#include "LabelModel.h" +#include "TranslationsModel.h" #include "Lookup.h" @@ -121,7 +121,8 @@ TranslationsModel::find( const QString& countryCode ) const } auto c_l = countryData( countryCode ); - int r = find( [&]( const Translation& l ) { return ( l.language() == c_l.second ) && ( l.country() == c_l.first ); } ); + int r = find( + [&]( const Translation& l ) { return ( l.language() == c_l.second ) && ( l.country() == c_l.first ); } ); if ( r >= 0 ) { return r; @@ -132,7 +133,8 @@ TranslationsModel::find( const QString& countryCode ) const TranslationsModel* availableTranslations() { - static TranslationsModel* model = new TranslationsModel( QStringLiteral( CALAMARES_TRANSLATION_LANGUAGES ).split( ';' ) ); + static TranslationsModel* model + = new TranslationsModel( QStringLiteral( CALAMARES_TRANSLATION_LANGUAGES ).split( ';' ) ); return model; } diff --git a/src/libcalamares/locale/LabelModel.h b/src/libcalamares/locale/TranslationsModel.h similarity index 96% rename from src/libcalamares/locale/LabelModel.h rename to src/libcalamares/locale/TranslationsModel.h index 1d12295b3..b87c00027 100644 --- a/src/libcalamares/locale/LabelModel.h +++ b/src/libcalamares/locale/TranslationsModel.h @@ -9,11 +9,11 @@ * */ -#ifndef LOCALE_LABELMODEL_H -#define LOCALE_LABELMODEL_H +#ifndef LOCALE_TRANSLATIONSMODEL_H +#define LOCALE_TRANSLATIONSMODEL_H #include "DllMacro.h" -#include "Label.h" +#include "Translation.h" #include #include diff --git a/src/modules/locale/Config.cpp b/src/modules/locale/Config.cpp index 700011258..9627fcfc3 100644 --- a/src/modules/locale/Config.cpp +++ b/src/modules/locale/Config.cpp @@ -15,7 +15,7 @@ #include "JobQueue.h" #include "Settings.h" #include "locale/Global.h" -#include "locale/Label.h" +#include "locale/Translation.h" #include "modulesystem/ModuleManager.h" #include "network/Manager.h" #include "utils/Logger.h" diff --git a/src/modules/welcome/Config.h b/src/modules/welcome/Config.h index 2597ccb5a..ad509a983 100644 --- a/src/modules/welcome/Config.h +++ b/src/modules/welcome/Config.h @@ -10,7 +10,7 @@ #ifndef WELCOME_CONFIG_H #define WELCOME_CONFIG_H -#include "locale/LabelModel.h" +#include "locale/TranslationsModel.h" #include "modulesystem/RequirementsModel.h" #include diff --git a/src/modules/welcome/WelcomePage.cpp b/src/modules/welcome/WelcomePage.cpp index b56b6754a..a82d873e9 100644 --- a/src/modules/welcome/WelcomePage.cpp +++ b/src/modules/welcome/WelcomePage.cpp @@ -20,7 +20,6 @@ #include "Settings.h" #include "ViewManager.h" -#include "locale/LabelModel.h" #include "modulesystem/ModuleManager.h" #include "modulesystem/RequirementsModel.h" #include "utils/CalamaresUtilsGui.h" diff --git a/src/modules/welcome/WelcomePage.h b/src/modules/welcome/WelcomePage.h index ceeb5160b..dba1f6a28 100644 --- a/src/modules/welcome/WelcomePage.h +++ b/src/modules/welcome/WelcomePage.h @@ -11,7 +11,7 @@ #ifndef WELCOMEPAGE_H #define WELCOMEPAGE_H -#include "locale/LabelModel.h" +#include "locale/TranslationsModel.h" #include #include diff --git a/src/modules/welcomeq/WelcomeQmlViewStep.cpp b/src/modules/welcomeq/WelcomeQmlViewStep.cpp index af32f2992..0d1d8cb3c 100644 --- a/src/modules/welcomeq/WelcomeQmlViewStep.cpp +++ b/src/modules/welcomeq/WelcomeQmlViewStep.cpp @@ -12,7 +12,7 @@ #include "checker/GeneralRequirements.h" -#include "locale/LabelModel.h" +#include "locale/TranslationsModel.h" #include "utils/Dirs.h" #include "utils/Logger.h" #include "utils/Variant.h" From 559c53b09c353d816fd9265e4c59a5def2d9872c Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 26 Jul 2021 13:14:30 +0200 Subject: [PATCH 3/8] [libcalamares]: stronger type for translation name QString -> Id for translations in the external API, to avoid accidentally converting a QLocale name (e.g. ca_ES) into a Calamares translation name. This preserves special-cases like ca@valencia and sr@latin. --- src/calamares/CalamaresApplication.cpp | 2 +- src/libcalamares/locale/Translation.h | 7 ++- src/libcalamares/utils/Retranslator.cpp | 54 +++++++------------- src/libcalamares/utils/Retranslator.h | 17 +++--- src/modules/keyboard/KeyboardLayoutModel.cpp | 2 +- src/modules/welcome/Config.cpp | 16 +++--- 6 files changed, 45 insertions(+), 53 deletions(-) diff --git a/src/calamares/CalamaresApplication.cpp b/src/calamares/CalamaresApplication.cpp index 08a5606e1..6c3eed6d1 100644 --- a/src/calamares/CalamaresApplication.cpp +++ b/src/calamares/CalamaresApplication.cpp @@ -78,7 +78,7 @@ CalamaresApplication::init() initQmlPath(); initBranding(); - CalamaresUtils::installTranslator( QLocale::system(), QString() ); + CalamaresUtils::installTranslator(); setQuitOnLastWindowClosed( false ); setWindowIcon( QIcon( Calamares::Branding::instance()->imagePath( Calamares::Branding::ProductIcon ) ) ); diff --git a/src/libcalamares/locale/Translation.h b/src/libcalamares/locale/Translation.h index a4402ebaa..db5be3c7c 100644 --- a/src/libcalamares/locale/Translation.h +++ b/src/libcalamares/locale/Translation.h @@ -46,6 +46,11 @@ public: IfNeededWithCountry }; + struct Id + { + QString name; + }; + /** @brief Empty locale. This uses the system-default locale. */ Translation( QObject* parent = nullptr ); @@ -82,7 +87,7 @@ public: QLocale locale() const { return m_locale; } QString name() const { return m_locale.name(); } - QString id() const { return m_localeId; } + Id id() const { return { m_localeId }; } /// @brief Convenience accessor to the language part of the locale QLocale::Language language() const { return m_locale.language(); } diff --git a/src/libcalamares/utils/Retranslator.cpp b/src/libcalamares/utils/Retranslator.cpp index 2e71fc011..2c1d2edb3 100644 --- a/src/libcalamares/utils/Retranslator.cpp +++ b/src/libcalamares/utils/Retranslator.cpp @@ -28,29 +28,8 @@ static bool s_allowLocalTranslations = false; */ struct TranslationLoader { - static QString mungeLocaleName( const QLocale& locale ) - { - QString localeName = locale.name(); - localeName.replace( "-", "_" ); - - if ( localeName == "C" ) - { - localeName = "en"; - } - - // Special case of sr@latin - // - // See top-level CMakeLists.txt about special cases for translation loading. - if ( locale.language() == QLocale::Language::Serbian && locale.script() == QLocale::Script::LatinScript ) - { - localeName = QStringLiteral( "sr@latin" ); - } - return localeName; - } - - TranslationLoader( const QLocale& locale ) - : m_locale( locale ) - , m_localeName( mungeLocaleName( locale ) ) + TranslationLoader( const QString& locale ) + : m_localeName( locale ) { } @@ -58,14 +37,13 @@ struct TranslationLoader /// @brief Loads @p translator with the specific translations of this type virtual bool tryLoad( QTranslator* translator ) = 0; - const QLocale& m_locale; QString m_localeName; }; /// @brief Loads translations for branding struct BrandingLoader : public TranslationLoader { - BrandingLoader( const QLocale& locale, const QString& prefix ) + BrandingLoader( const QString& locale, const QString& prefix ) : TranslationLoader( locale ) , m_prefix( prefix ) { @@ -106,7 +84,7 @@ BrandingLoader::tryLoad( QTranslator* translator ) { QString filenameBase( m_prefix ); filenameBase.remove( 0, m_prefix.lastIndexOf( QDir::separator() ) + 1 ); - if ( translator->load( m_locale, filenameBase, "_", brandingTranslationsDir.absolutePath() ) ) + if ( translator->load( m_localeName, filenameBase, "_", brandingTranslationsDir.absolutePath() ) ) { cDebug() << Logger::SubEntry << "Branding using locale:" << m_localeName; return true; @@ -189,26 +167,32 @@ static QTranslator* s_tztranslator = nullptr; static QString s_translatorLocaleName; void -installTranslator( const QLocale& locale, const QString& brandingTranslationsPrefix ) +installTranslator( const CalamaresUtils::Locale::Translation::Id& locale, const QString& brandingTranslationsPrefix ) { - loadSingletonTranslator( BrandingLoader( locale, brandingTranslationsPrefix ), s_brandingTranslator ); - loadSingletonTranslator( TZLoader( locale ), s_tztranslator ); - loadSingletonTranslator( CalamaresLoader( locale ), s_translator ); + s_translatorLocaleName = locale.name; - s_translatorLocaleName = CalamaresLoader::mungeLocaleName( locale ); + loadSingletonTranslator( BrandingLoader( locale.name, brandingTranslationsPrefix ), s_brandingTranslator ); + loadSingletonTranslator( TZLoader( locale.name ), s_tztranslator ); + loadSingletonTranslator( CalamaresLoader( locale.name ), s_translator ); } +void +installTranslator() +{ + // Just wrap it up like an Id + installTranslator( { QLocale::system().name() }, QString() ); +} -QString +CalamaresUtils::Locale::Translation::Id translatorLocaleName() { - return s_translatorLocaleName; + return { s_translatorLocaleName }; } bool -loadTranslator( const QLocale& locale, const QString& prefix, QTranslator* translator ) +loadTranslator( const CalamaresUtils::Locale::Translation::Id& locale, const QString& prefix, QTranslator* translator ) { - return ::tryLoad( translator, prefix, locale.name() ); + return ::tryLoad( translator, prefix, locale.name ); } Retranslator::Retranslator( QObject* parent ) diff --git a/src/libcalamares/utils/Retranslator.h b/src/libcalamares/utils/Retranslator.h index 9d8617cbd..efe12ef8a 100644 --- a/src/libcalamares/utils/Retranslator.h +++ b/src/libcalamares/utils/Retranslator.h @@ -12,8 +12,8 @@ #define UTILS_RETRANSLATOR_H #include "DllMacro.h" +#include "locale/Translation.h" -#include #include #include @@ -25,12 +25,15 @@ class QTranslator; namespace CalamaresUtils { -/** - * @brief installTranslator changes the application language. - * @param locale the new locale. +/** @brief changes the application language. + * @param locale the new locale (names as defined by Calamares). * @param brandingTranslationsPrefix the branding path prefix, from Calamares::Branding. */ -DLLEXPORT void installTranslator( const QLocale& locale, const QString& brandingTranslationsPrefix ); +DLLEXPORT void installTranslator( const CalamaresUtils::Locale::Translation::Id& locale, const QString& brandingTranslationsPrefix ); + +/** @brief Initializes the translations with the current system settings + */ +DLLEXPORT void installTranslator(); /** @brief The name of the (locale of the) most recently installed translator * @@ -38,7 +41,7 @@ DLLEXPORT void installTranslator( const QLocale& locale, const QString& branding * QLocale passed in, because Calamares will munge some names and * may remap translations. */ -DLLEXPORT QString translatorLocaleName(); +DLLEXPORT CalamaresUtils::Locale::Translation::Id translatorLocaleName(); /** @brief Loads translations into the given @p translator * @@ -53,7 +56,7 @@ DLLEXPORT QString translatorLocaleName(); * * @returns @c true on success */ -DLLEXPORT bool loadTranslator( const QLocale& locale, const QString& prefix, QTranslator* translator ); +DLLEXPORT bool loadTranslator( const CalamaresUtils::Locale::Translation::Id& locale, const QString& prefix, QTranslator* translator ); /** @brief Set @p allow to true to load translations from current dir. * diff --git a/src/modules/keyboard/KeyboardLayoutModel.cpp b/src/modules/keyboard/KeyboardLayoutModel.cpp index 34a1dec88..3b9ba19fe 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.cpp +++ b/src/modules/keyboard/KeyboardLayoutModel.cpp @@ -27,7 +27,7 @@ retranslateKeyboardModels() { s_kbtranslator = new QTranslator; } - (void)CalamaresUtils::loadTranslator( QLocale(), QStringLiteral( "kb_" ), s_kbtranslator ); + (void)CalamaresUtils::loadTranslator( CalamaresUtils::translatorLocaleName(), QStringLiteral( "kb_" ), s_kbtranslator ); } diff --git a/src/modules/welcome/Config.cpp b/src/modules/welcome/Config.cpp index 756cb0e5a..4d9fcad2b 100644 --- a/src/modules/welcome/Config.cpp +++ b/src/modules/welcome/Config.cpp @@ -150,10 +150,10 @@ Config::initLanguages() if ( matchedLocaleIndex >= 0 ) { - QString name = m_languages->locale( matchedLocaleIndex ).name(); - cDebug() << Logger::SubEntry << "Matched with index" << matchedLocaleIndex << name; + auto languageId = m_languages->locale( matchedLocaleIndex ).id(); + cDebug() << Logger::SubEntry << "Matched with index" << matchedLocaleIndex << languageId.name; - CalamaresUtils::installTranslator( name, Calamares::Branding::instance()->translationsDirectory() ); + CalamaresUtils::installTranslator( languageId, Calamares::Branding::instance()->translationsDirectory() ); setLocaleIndex( matchedLocaleIndex ); } else @@ -188,16 +188,16 @@ Config::setLocaleIndex( int index ) m_localeIndex = index; - const auto& selectedLocale = m_languages->locale( m_localeIndex ).locale(); - cDebug() << "Index" << index << "Selected locale" << selectedLocale; + const auto& selectedTranslation = m_languages->locale( m_localeIndex ); + cDebug() << "Index" << index << "Selected locale" << selectedTranslation.id().name; - QLocale::setDefault( selectedLocale ); - CalamaresUtils::installTranslator( selectedLocale, Calamares::Branding::instance()->translationsDirectory() ); + QLocale::setDefault( selectedTranslation.locale() ); + CalamaresUtils::installTranslator( selectedTranslation.id(), Calamares::Branding::instance()->translationsDirectory() ); if ( Calamares::JobQueue::instance() && Calamares::JobQueue::instance()->globalStorage() ) { CalamaresUtils::Locale::insertGS( *Calamares::JobQueue::instance()->globalStorage(), QStringLiteral( "LANG" ), - CalamaresUtils::translatorLocaleName() ); + CalamaresUtils::translatorLocaleName().name ); } emit localeIndexChanged( m_localeIndex ); } From 73bfc6ca32fb88816aa2cda163eaa367adaa4b17 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 7 Sep 2021 11:38:54 +0200 Subject: [PATCH 4/8] [libcalamares] Use structured bindings to unpack a std::pair --- src/libcalamares/locale/Translation.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libcalamares/locale/Translation.cpp b/src/libcalamares/locale/Translation.cpp index 0a2e594be..4afb1bbc8 100644 --- a/src/libcalamares/locale/Translation.cpp +++ b/src/libcalamares/locale/Translation.cpp @@ -56,11 +56,11 @@ Translation::Translation( const QString& locale, LabelFormat format, QObject* pa , m_locale( getLocale( locale ) ) , m_localeId( locale.isEmpty() ? m_locale.name() : locale ) { - auto special = specialCase( locale ); + auto [ _, name ] = specialCase( locale ); QString longFormat = QObject::tr( "%1 (%2)" ); - QString languageName = special.second ? *special.second : m_locale.nativeLanguageName(); + QString languageName = name ? *name : m_locale.nativeLanguageName(); QString englishName = m_locale.languageToString( m_locale.language() ); if ( languageName.isEmpty() ) @@ -87,8 +87,8 @@ Translation::getLocale( const QString& localeName ) return QLocale(); } - auto special = specialCase( localeName ); - return special.first ? *special.first : QLocale( localeName ); + auto [ locale, _ ] = specialCase( localeName ); + return locale ? *locale : QLocale( localeName ); } } // namespace Locale From ad1a4b647970dda2359110416342e87f0c71e641 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 7 Sep 2021 11:42:32 +0200 Subject: [PATCH 5/8] [libcalamares] APIdox on Translation --- src/libcalamares/locale/Translation.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libcalamares/locale/Translation.h b/src/libcalamares/locale/Translation.h index db5be3c7c..67de90821 100644 --- a/src/libcalamares/locale/Translation.h +++ b/src/libcalamares/locale/Translation.h @@ -86,7 +86,16 @@ public: /** @brief Get the Qt locale. */ QLocale locale() const { return m_locale; } + /** @brief Get the Qt-internal name (code) of the locale + * + * This is not necessarily the same as what is in id(). + */ QString name() const { return m_locale.name(); } + /** @brief Gets the Calamares internal name (code) of the locale. + * + * This is a strongly-typed return to avoid it ending up all over + * the place as a QString. + */ Id id() const { return { m_localeId }; } /// @brief Convenience accessor to the language part of the locale From 5f4e65bc773485f0f26d2c96106e170103792d35 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 7 Sep 2021 12:35:37 +0200 Subject: [PATCH 6/8] [libcalamares] Code-format Retranslator, hide internal symbols --- src/libcalamares/utils/Retranslator.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/libcalamares/utils/Retranslator.cpp b/src/libcalamares/utils/Retranslator.cpp index 2c1d2edb3..48b61c12a 100644 --- a/src/libcalamares/utils/Retranslator.cpp +++ b/src/libcalamares/utils/Retranslator.cpp @@ -19,6 +19,9 @@ #include #include +namespace +{ + static bool s_allowLocalTranslations = false; /** @brief Helper class for loading translations @@ -159,6 +162,8 @@ loadSingletonTranslator( TranslationLoader&& loader, QTranslator*& translator_p } } +} // namespace + namespace CalamaresUtils { static QTranslator* s_brandingTranslator = nullptr; @@ -211,13 +216,15 @@ Retranslator::eventFilter( QObject* obj, QEvent* e ) return QObject::eventFilter( obj, e ); } -Retranslator* Retranslator::instance() +Retranslator* +Retranslator::instance() { - static Retranslator s_instance(nullptr); + static Retranslator s_instance( nullptr ); return &s_instance; } -void Retranslator::attach(QObject* o, std::function f) +void +Retranslator::attach( QObject* o, std::function< void() > f ) { connect( instance(), &Retranslator::languageChanged, o, f ); f(); From 3ff5896dc65320c30b1676dfb844e5918deff42b Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 7 Sep 2021 12:35:47 +0200 Subject: [PATCH 7/8] [libcalamares] Remove unused method --- src/libcalamares/locale/Translation.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/libcalamares/locale/Translation.h b/src/libcalamares/locale/Translation.h index 67de90821..39b177351 100644 --- a/src/libcalamares/locale/Translation.h +++ b/src/libcalamares/locale/Translation.h @@ -86,11 +86,6 @@ public: /** @brief Get the Qt locale. */ QLocale locale() const { return m_locale; } - /** @brief Get the Qt-internal name (code) of the locale - * - * This is not necessarily the same as what is in id(). - */ - QString name() const { return m_locale.name(); } /** @brief Gets the Calamares internal name (code) of the locale. * * This is a strongly-typed return to avoid it ending up all over From 4e60f8af135f721570b4bf86e685db6ec5425c42 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 7 Sep 2021 12:51:57 +0200 Subject: [PATCH 8/8] [libcalamares] Use strong types for locale Ids Change the API to force strong type for more methods. This cascades to a couple of consumers. --- src/libcalamares/locale/Translation.cpp | 22 ++++++++++--------- src/libcalamares/locale/Translation.h | 6 ++--- src/libcalamares/locale/TranslationsModel.cpp | 2 +- src/modules/locale/Config.cpp | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/libcalamares/locale/Translation.cpp b/src/libcalamares/locale/Translation.cpp index 4afb1bbc8..fb8890e87 100644 --- a/src/libcalamares/locale/Translation.cpp +++ b/src/libcalamares/locale/Translation.cpp @@ -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 ); } diff --git a/src/libcalamares/locale/Translation.h b/src/libcalamares/locale/Translation.h index 39b177351..5e5ce33ba 100644 --- a/src/libcalamares/locale/Translation.h +++ b/src/libcalamares/locale/Translation.h @@ -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; diff --git a/src/libcalamares/locale/TranslationsModel.cpp b/src/libcalamares/locale/TranslationsModel.cpp index af1bd1801..25d075df4 100644 --- a/src/libcalamares/locale/TranslationsModel.cpp +++ b/src/libcalamares/locale/TranslationsModel.cpp @@ -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 ) ); } } diff --git a/src/modules/locale/Config.cpp b/src/modules/locale/Config.cpp index 9627fcfc3..ce48edd82 100644 --- a/src/modules/locale/Config.cpp +++ b/src/modules/locale/Config.cpp @@ -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(); }