diff --git a/src/libcalamares/locale/Label.h b/src/libcalamares/locale/Label.h index a436d4c62..65befc6b4 100644 --- a/src/libcalamares/locale/Label.h +++ b/src/libcalamares/locale/Label.h @@ -92,6 +92,18 @@ public: return m_locale.name(); } + /// @brief Convenience accessor to the language part of the locale + 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(); + } + /** @brief Get a Qt locale for the given @p localeName * * This special-cases `sr@latin`, which is used as a translation diff --git a/src/libcalamares/locale/LabelModel.cpp b/src/libcalamares/locale/LabelModel.cpp index ac931620d..543417212 100644 --- a/src/libcalamares/locale/LabelModel.cpp +++ b/src/libcalamares/locale/LabelModel.cpp @@ -18,6 +18,8 @@ #include "LabelModel.h" +#include "Lookup.h" + #include "CalamaresVersion.h" // For the list of translations namespace CalamaresUtils::Locale @@ -106,6 +108,19 @@ LabelModel::find( const QLocale& locale ) const } ); } +int +LabelModel::find( const QString& countryCode ) const +{ + if ( countryCode.length() != 2 ) + return -1; + + auto c_l = countryData( countryCode ); + int r = find( [&]( const Label& 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; } ); +} + LabelModel* const availableTranslations() { static LabelModel model( QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';') ); diff --git a/src/libcalamares/locale/LabelModel.h b/src/libcalamares/locale/LabelModel.h index 6b4f41343..178f76343 100644 --- a/src/libcalamares/locale/LabelModel.h +++ b/src/libcalamares/locale/LabelModel.h @@ -59,7 +59,10 @@ public: */ int find( std::function predicate ) const; int find( std::function 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;