[libcalamares] Compare translation IDs, extend find()

Allow naive comparison of translation IDs (e.g. "ca@valencia"
against other IDs) and make it easier to find one.
This commit is contained in:
Adriaan de Groot 2022-05-04 10:35:43 +02:00
parent 5307976179
commit 792c4914b0
3 changed files with 18 additions and 5 deletions

View File

@ -117,6 +117,11 @@ operator<<( QDebug& s, const Translation::Id& id )
{
return s << id.name;
}
static inline bool
operator==( const Translation::Id& lhs, const Translation::Id& rhs )
{
return lhs.name == rhs.name;
}
} // namespace Locale
} // namespace CalamaresUtils

View File

@ -103,13 +103,13 @@ TranslationsModel::find( std::function< bool( const Translation& ) > predicate )
int
TranslationsModel::find( std::function< bool( const QLocale& ) > predicate ) const
{
return find( [&]( const Translation& l ) { return predicate( l.locale() ); } );
return find( [ & ]( const Translation& l ) { return predicate( l.locale() ); } );
}
int
TranslationsModel::find( const QLocale& locale ) const
{
return find( [&]( const Translation& l ) { return locale == l.locale(); } );
return find( [ & ]( const Translation& l ) { return locale == l.locale(); } );
}
int
@ -121,13 +121,19 @@ TranslationsModel::find( const QString& countryCode ) const
}
auto c_l = countryData( countryCode );
int r = find(
[&]( const Translation& l ) { return ( l.language() == c_l.second ) && ( l.country() == c_l.first ); } );
int r = find( [ & ]( const Translation& l )
{ return ( l.language() == c_l.second ) && ( l.country() == c_l.first ); } );
if ( r >= 0 )
{
return r;
}
return find( [&]( const Translation& l ) { return l.language() == c_l.second; } );
return find( [ & ]( const Translation& l ) { return l.language() == c_l.second; } );
}
int
TranslationsModel::find( const Translation::Id& id ) const
{
return find( [ & ]( const Translation& l ) { return l.id() == id; } );
}
TranslationsModel*

View File

@ -63,6 +63,8 @@ public:
int find( const QLocale& ) const;
/// @brief Looks for an item that best matches the 2-letter country code
int find( const QString& countryCode ) const;
/// @brief Looks up a translation Id
int find( const Translation::Id& id ) const;
private:
QVector< Translation* > m_locales;