[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.
This commit is contained in:
Adriaan de Groot 2019-05-10 14:44:54 -04:00
parent 1857952431
commit 03f88b3ed6
3 changed files with 30 additions and 0 deletions

View File

@ -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

View File

@ -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( ';') );

View File

@ -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;