diff --git a/src/libcalamaresui/utils/CalamaresUtilsGui.cpp b/src/libcalamaresui/utils/CalamaresUtilsGui.cpp index 425ee1811..a7783af3e 100644 --- a/src/libcalamaresui/utils/CalamaresUtilsGui.cpp +++ b/src/libcalamaresui/utils/CalamaresUtilsGui.cpp @@ -170,11 +170,7 @@ createRoundedImage( const QPixmap& pixmap, const QSize& size, float frameWidthPc painter.setBrush( brush ); painter.setPen( pen ); - painter.drawRoundedRect( outerRect, qreal(frameWidthPct) * 100.0, qreal(frameWidthPct) * 100.0, Qt::RelativeSize ); - -/* painter.setBrush( Qt::transparent ); - painter.setPen( Qt::white ); - painter.drawRoundedRect( outerRect, frameWidthPct, frameWidthPct, Qt::RelativeSize ); */ + painter.drawRoundedRect( outerRect, qreal( frameWidthPct ) * 100.0, qreal( frameWidthPct ) * 100.0, Qt::RelativeSize ); return frame; } @@ -246,7 +242,7 @@ setDefaultFontSize( int points ) QSize defaultIconSize() { - const int w = int(defaultFontHeight() * 1.6); + const int w = int( defaultFontHeight() * 1.6 ); return QSize( w, w ); } @@ -266,5 +262,44 @@ clearLayout( QLayout* layout ) } } +LocaleLabel::LocaleLabel( const QString& locale, LabelFormat format ) + : m_locale( LocaleLabel::getLocale( locale ) ) + , m_localeId( locale ) +{ + QString longFormat = QObject::tr( "%1 (%2)", "Language (Country)" ); + + QString sortKey = QLocale::languageToString( m_locale.language() ); + QString languageName = m_locale.nativeLanguageName(); + QString countryName; + + if ( languageName.isEmpty() ) + languageName = QString( QLatin1Literal( "* %1 (%2)" ) ).arg( locale, sortKey ); + + bool needsCountryName = ( format == LabelFormat::AlwaysWithCountry ) || + (locale.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 1 ); + + if ( needsCountryName ) + { + sortKey.append( '+' ); + sortKey.append( QLocale::countryToString( m_locale.country() ) ); + + countryName = m_locale.nativeCountryName(); + } + + m_sortKey = sortKey; + m_label = needsCountryName ? longFormat.arg( languageName ).arg( countryName ) : languageName; +} + +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..6a036b218 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,77 @@ 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: + /** @brief Formatting option for label -- add (country) to label. */ + enum class LabelFormat { AlwaysWithCountry, IfNeededWithCountry } ; + + /** @brief Construct from a locale name. + * + * The @p localeName should be one that Qt recognizes, e.g. en_US or ar_EY. + * The @p format determines whether the country name is always present + * in the label (human-readable form) or only if needed for disambiguation. + */ + LocaleLabel( const QString& localeName, LabelFormat format = LabelFormat::IfNeededWithCountry ); + + /** @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/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index 6f4473014..9056ba273 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -369,6 +369,8 @@ KeyboardPage::onActivate() { const auto langParts = lang.split( '_', QString::SkipEmptyParts ); + // Note that this his string is not fit for display purposes! + // It doesn't come from QLocale::nativeCountryName. QString country = QLocale::countryToString( QLocale( lang ).country() ); cDebug() << " .. extracted country" << country << "::" << langParts; diff --git a/src/modules/locale/LocalePage.cpp b/src/modules/locale/LocalePage.cpp index 9aad283c6..ea77d6b9f 100644 --- a/src/modules/locale/LocalePage.cpp +++ b/src/modules/locale/LocalePage.cpp @@ -21,6 +21,7 @@ #include "timezonewidget/timezonewidget.h" #include "SetTimezoneJob.h" +#include "utils/CalamaresUtilsGui.h" #include "utils/Logger.h" #include "utils/Retranslator.h" #include "GlobalStorage.h" @@ -383,12 +384,14 @@ LocalePage::init( const QString& initialRegion, std::pair< QString, QString > LocalePage::prettyLocaleStatus( const LocaleConfiguration& lc ) const { + using CalamaresUtils::LocaleLabel; + + LocaleLabel lang( lc.lang, LocaleLabel::LabelFormat::AlwaysWithCountry ); + LocaleLabel num( lc.lc_numeric, LocaleLabel::LabelFormat::AlwaysWithCountry ); + return std::make_pair< QString, QString >( - tr( "The system language will be set to %1." ) - .arg( prettyLCLocale( lc.lang ) ), - tr( "The numbers and dates locale will be set to %1." ) - .arg( prettyLCLocale( lc.lc_numeric ) ) - ); + tr( "The system language will be set to %1." ).arg( lang.label() ), + tr( "The numbers and dates locale will be set to %1." ).arg( num.label() ) ); } QString @@ -468,19 +471,6 @@ LocalePage::guessLocaleConfiguration() const } -QString -LocalePage::prettyLCLocale( const QString& lcLocale ) const -{ - QString localeString = lcLocale; - if ( localeString.endsWith( " UTF-8" ) ) - localeString.remove( " UTF-8" ); - - QLocale locale( localeString ); - //: Language (Country) - return tr( "%1 (%2)" ).arg( QLocale::languageToString( locale.language() ) ) - .arg( QLocale::countryToString( locale.country() ) ); -} - void LocalePage::updateGlobalStorage() { diff --git a/src/modules/locale/LocalePage.h b/src/modules/locale/LocalePage.h index c4ca20503..fae16cfb9 100644 --- a/src/modules/locale/LocalePage.h +++ b/src/modules/locale/LocalePage.h @@ -51,7 +51,6 @@ public: private: LocaleConfiguration guessLocaleConfiguration() const; - QString prettyLCLocale( const QString& localesMap ) const; // For the given locale config, return two strings describing // the settings for language and numbers. diff --git a/src/modules/welcome/WelcomePage.cpp b/src/modules/welcome/WelcomePage.cpp index 0a1d2fb7d..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() ); } } @@ -345,4 +277,3 @@ WelcomePage::focusInEvent( QFocusEvent* e ) ui->languageWidget->setFocus(); e->accept(); } -