From 084f4d24450b018999b97f53f3580cc69507cc5c Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 14 Dec 2018 10:52:55 +0100 Subject: [PATCH] [libcalamaresui] Refactor: move LocaleLabel to UI library - This is prep-work for making locale labels consistent everywhere. - While here, improve code documentation. --- .../utils/CalamaresUtilsGui.cpp | 37 ++++++++++ src/libcalamaresui/utils/CalamaresUtilsGui.h | 65 ++++++++++++++++- src/modules/welcome/WelcomePage.cpp | 72 +------------------ 3 files changed, 103 insertions(+), 71 deletions(-) diff --git a/src/libcalamaresui/utils/CalamaresUtilsGui.cpp b/src/libcalamaresui/utils/CalamaresUtilsGui.cpp index 425ee1811..41465ca02 100644 --- a/src/libcalamaresui/utils/CalamaresUtilsGui.cpp +++ b/src/libcalamaresui/utils/CalamaresUtilsGui.cpp @@ -266,5 +266,42 @@ clearLayout( QLayout* layout ) } } +LocaleLabel::LocaleLabel( const QString& locale ) + : m_locale( LocaleLabel::getLocale( locale ) ) + , m_localeId( locale ) +{ + QString sortKey = QLocale::languageToString( m_locale.language() ); + QString label = m_locale.nativeLanguageName(); + + if ( label.isEmpty() ) + label = QString( QLatin1Literal( "* %1 (%2)" ) ).arg( locale, sortKey ); + + if ( locale.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 2 ) + { + QLatin1Literal countrySuffix( " (%1)" ); + + sortKey.append( QString( countrySuffix ).arg( QLocale::countryToString( m_locale.country() ) ) ); + + // If the language name is RTL, make this parenthetical addition RTL as well. + QString countryFormat = label.isRightToLeft() ? QString( QChar( 0x202B ) ) : QString(); + countryFormat.append( countrySuffix ); + label.append( countryFormat.arg( m_locale.nativeCountryName() ) ); + } + + m_sortKey = sortKey; + m_label = label; +} + +QLocale LocaleLabel::getLocale( const QString& localeName ) +{ + if ( localeName.contains( "@latin" ) ) + { + QLocale loc( localeName ); // Ignores @latin + return QLocale( loc.language(), QLocale::Script::LatinScript, loc.country() ); + } + else + return QLocale( localeName ); +} + } diff --git a/src/libcalamaresui/utils/CalamaresUtilsGui.h b/src/libcalamaresui/utils/CalamaresUtilsGui.h index 4b041466d..1e5f283b1 100644 --- a/src/libcalamaresui/utils/CalamaresUtilsGui.h +++ b/src/libcalamaresui/utils/CalamaresUtilsGui.h @@ -23,6 +23,7 @@ #include "utils/CalamaresUtils.h" #include "UiDllMacro.h" +#include #include #include @@ -125,6 +126,68 @@ constexpr int windowMinimumWidth = 800; constexpr int windowMinimumHeight = 520; constexpr int windowPreferredWidth = 1024; constexpr int windowPreferredHeight = 520; -} + +/** + * @brief Consistent locale (language + country) naming. + * + * Support class to turn locale names (as used by Calamares's + * translation system) into QLocales, and also into consistent + * human-readable text labels. + */ +class LocaleLabel : public QObject +{ + Q_OBJECT + +public: + /** @brief Construct from a locale name. + * + * The locale name should be one that Qt recognizes, e.g. en_US or ar_EY. + */ + LocaleLabel( const QString& localeName ); + + /** @brief Define a sorting order. + * + * English (@see isEnglish() -- it means en_US) is sorted at the top. + */ + bool operator <(const LocaleLabel& other) const + { + if ( isEnglish() ) + return !other.isEnglish(); + if ( other.isEnglish() ) + return false; + return m_sortKey < other.m_sortKey; + } + + /** @brief Is this locale English? + * + * en_US and en (American English) is defined as English. The Queen's + * English -- proper English -- is relegated to non-English status. + */ + bool isEnglish() const + { + return m_localeId == QLatin1Literal( "en_US" ) || m_localeId == QLatin1Literal( "en" ); + } + + /** @brief Get the human-readable name for this locale. */ + QString label() const { return m_label; } + /** @brief Get the Qt locale. */ + QLocale locale() const { return m_locale; } + + /** @brief Get a Qt locale for the given @p localeName + * + * This special-cases `sr@latin`, which is used as a translation + * name in Calamares, while Qt recognizes `sr@latn`. + */ + static QLocale getLocale( const QString& localeName ); + +protected: + QLocale m_locale; + QString m_localeId; // the locale identifier, e.g. "en_GB" + QString m_sortKey; // the English name of the locale + QString m_label; // the native name of the locale +} ; + + +} // namespace CalamaresUtils #endif // CALAMARESUTILSGUI_H diff --git a/src/modules/welcome/WelcomePage.cpp b/src/modules/welcome/WelcomePage.cpp index 4fc44e0d5..8ffb153bf 100644 --- a/src/modules/welcome/WelcomePage.cpp +++ b/src/modules/welcome/WelcomePage.cpp @@ -132,74 +132,6 @@ bool matchLocale( QComboBox& list, QLocale& matchFound, std::function 2 ) - { - QLatin1Literal countrySuffix( " (%1)" ); - - sortKey.append( QString( countrySuffix ).arg( QLocale::countryToString( m_locale.country() ) ) ); - - // If the language name is RTL, make this parenthetical addition RTL as well. - QString countryFormat = label.isRightToLeft() ? QString( QChar( 0x202B ) ) : QString(); - countryFormat.append( countrySuffix ); - label.append( countryFormat.arg( m_locale.nativeCountryName() ) ); - } - - m_sortKey = sortKey; - m_label = label; - } - - QLocale m_locale; - QString m_localeId; // the locale identifier, e.g. "en_GB" - QString m_sortKey; // the English name of the locale - QString m_label; // the native name of the locale - - /** @brief Define a sorting order. - * - * English (@see isEnglish() -- it means en_US) is sorted at the top. - */ - bool operator <(const LocaleLabel& other) const - { - if ( isEnglish() ) - return !other.isEnglish(); - if ( other.isEnglish() ) - return false; - return m_sortKey < other.m_sortKey; - } - - /** @brief Is this locale English? - * - * en_US and en (American English) is defined as English. The Queen's - * English -- proper English -- is relegated to non-English status. - */ - bool isEnglish() const - { - return m_localeId == QLatin1Literal( "en_US" ) || m_localeId == QLatin1Literal( "en" ); - } - - static QLocale getLocale( const QString& localeName ) - { - if ( localeName.contains( "@latin" ) ) - { - QLocale loc( localeName ); - return QLocale( loc.language(), QLocale::Script::LatinScript, loc.country() ); - } - else - return QLocale( localeName ); - } -} ; - void WelcomePage::initLanguages() { @@ -208,7 +140,7 @@ WelcomePage::initLanguages() ui->languageWidget->setInsertPolicy( QComboBox::InsertAtBottom ); { - std::list< LocaleLabel > localeList; + std::list< CalamaresUtils::LocaleLabel > localeList; const auto locales = QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';'); for ( const QString& locale : locales ) { @@ -219,7 +151,7 @@ WelcomePage::initLanguages() for ( const auto& locale : localeList ) { - ui->languageWidget->addItem( locale.m_label, locale.m_locale ); + ui->languageWidget->addItem( locale.label(), locale.locale() ); } }