diff options
author | Adriaan de Groot <groot@kde.org> | 2019-05-10 14:44:54 -0400 |
---|---|---|
committer | Adriaan de Groot <groot@kde.org> | 2019-05-10 14:44:54 -0400 |
commit | 03f88b3ed6cbf9155f6f62d941e9fd165c0e3202 (patch) | |
tree | ed612a56ee0cba37920b2d0d4e3364147162a7dd | |
parent | 18579524312687e0c013b8bb5cf6bebb18b082b1 (diff) |
[libcalamares] Support looking up translation by 2-letter country
- Looks for an available translation by 2-letter country code
and returns the row for it.
-rw-r--r-- | src/libcalamares/locale/Label.h | 12 | ||||
-rw-r--r-- | src/libcalamares/locale/LabelModel.cpp | 15 | ||||
-rw-r--r-- | src/libcalamares/locale/LabelModel.h | 3 |
3 files changed, 30 insertions, 0 deletions
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<bool( const QLocale& )> predicate ) const; int find( std::function<bool( const Label& )> 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; |