From 2fcc008d7be7525fd1dce58230dda7641eddf412 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 2 Dec 2024 12:45:26 +0100 Subject: [PATCH] [libcalamares] Follow deprecations in QLocale Prefer territory() over country() in Qt 6.6 and later. --- src/libcalamares/locale/Translation.cpp | 47 ++++++++++++++++++++++--- src/libcalamares/locale/Translation.h | 10 +++++- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/libcalamares/locale/Translation.cpp b/src/libcalamares/locale/Translation.cpp index 0a7312704..d250661bb 100644 --- a/src/libcalamares/locale/Translation.cpp +++ b/src/libcalamares/locale/Translation.cpp @@ -12,6 +12,7 @@ #include "Translation.h" #include +#include namespace { @@ -145,6 +146,44 @@ specialCaseSystemLanguage() { return ( s.language == language ) && lookup_region( region, s.regions ); } ); return ( it != std::cend( special_cases ) ) ? QString::fromLatin1( it->id ) : QString(); } + +///@brief Country (territory) name for this locale +QString +territoryName( const QLocale& locale ) +{ +#if QT_VERSION < QT_VERSION_CHECK( 6, 6, 0 ) + return QLocale::countryToString( locale.country() ); +#else + return QLocale::territoryToString( locale.territory() ); +#endif +} + +QString +nativeTerritoryName( const QLocale& locale ) +{ +#if QT_VERSION < QT_VERSION_CHECK( 6, 6, 0 ) + return locale.nativeCountryName(); +#else + return locale.nativeTerritoryName(); +#endif +} + +bool +needsTerritorialDisambiguation( const QLocale& locale ) +{ +#if QT_VERSION < QT_VERSION_CHECK( 6, 6, 0 ) + return QLocale::countriesForLanguage( locale.language() ).count() > 1; +#else + std::set s; + for(const auto & l : QLocale::matchingLocales( locale.language(), QLocale::Script::AnyScript, QLocale::Territory::AnyTerritory )) + { + s.insert(l.territory()); + } + return s.size() > 1; +#endif +} + + } // namespace namespace Calamares @@ -178,12 +217,10 @@ Translation::Translation( const Id& localeId, LabelFormat format, QObject* paren } bool needsCountryName = ( format == LabelFormat::AlwaysWithCountry ) - || ( !name && localeId.name.contains( '_' ) - && QLocale::countriesForLanguage( m_locale.language() ).count() > 1 ); - QString countryName = needsCountryName ? m_locale.nativeCountryName() : QString(); + || ( !name && localeId.name.contains( '_' ) && needsTerritorialDisambiguation( ( m_locale ) ) ); + const QString countryName = needsCountryName ? nativeTerritoryName( m_locale ) : QString(); m_label = needsCountryName ? longFormat.arg( languageName, countryName ) : languageName; - m_englishLabel = needsCountryName ? longFormat.arg( englishName, QLocale::countryToString( m_locale.country() ) ) - : englishName; + m_englishLabel = needsCountryName ? longFormat.arg( englishName, territoryName( m_locale ) ) : englishName; } QLocale diff --git a/src/libcalamares/locale/Translation.h b/src/libcalamares/locale/Translation.h index 3483a1ca2..14fb33c02 100644 --- a/src/libcalamares/locale/Translation.h +++ b/src/libcalamares/locale/Translation.h @@ -105,7 +105,15 @@ public: QLocale::Language language() const { return m_locale.language(); } /// @brief Convenience accessor to the country part (if any) of the locale - QLocale::Country country() const { return m_locale.country(); } + QLocale::Country country() const + { +#if QT_VERSION < QT_VERSION_CHECK( 6, 6, 0 ) + + return m_locale.country(); +#else + return m_locale.territory(); +#endif + } /** @brief Get a Qt locale for the given @p localeName *