[libcalamares] Add get() to the string

- Look up the translation of the requested string with the current
   or a specific locale. This implementation is a stub.
 - Add tests for the getter.
This commit is contained in:
Adriaan de Groot 2019-08-05 23:37:25 +02:00
parent e0edd1f3e2
commit 50d74c4eca
3 changed files with 27 additions and 1 deletions

View file

@ -109,10 +109,16 @@ void LocaleTests::testTranslatableConfig1()
CalamaresUtils::Locale::TranslatedString ts1( "Hello" );
QCOMPARE( ts1.count(), 1 );
QCOMPARE( ts1.get(), "Hello" );
QCOMPARE( ts1.get( QLocale("nl")), "Hello" );
QVariantMap map;
map.insert( "description", "description (no language)" );
CalamaresUtils::Locale::TranslatedString ts2(map, "description");
QCOMPARE( ts2.count(), 1 );
QCOMPARE( ts2.get(), "description (no language)");
QCOMPARE( ts2.get( QLocale( "nl" ) ), "description (no language)");
}
void LocaleTests::testTranslatableConfig2()
@ -131,6 +137,9 @@ void LocaleTests::testTranslatableConfig2()
CalamaresUtils::Locale::TranslatedString ts1(map, "description");
// The +1 is because "" is always also inserted
QCOMPARE( ts1.count(), someLanguages().count()+1 );
QCOMPARE( ts1.get(), "description"); // it wasn't set
QCOMPARE( ts1.get( QLocale( "nl" ) ), "description (language nl)");
CalamaresUtils::Locale::TranslatedString ts2(map, "name");
// We skipped dutch this time

View file

@ -53,7 +53,6 @@ TranslatedString::TranslatedString(const QVariantMap& map, const QString& key)
}
else if ( subkey.startsWith( key ) )
{
cDebug() << "Checking" << subkey;
QRegularExpressionMatch match;
if ( subkey.indexOf( QRegularExpression("\\[([a-zA-Z_@]*)\\]"), 0, &match ) > 0 )
{
@ -64,5 +63,17 @@ TranslatedString::TranslatedString(const QVariantMap& map, const QString& key)
}
}
QString TranslatedString::get() const
{
return get( QLocale() );
}
QString TranslatedString::get(const QLocale& locale) const
{
cDebug() << "Getting locale" << locale.name();
return m_strings[QString()];
}
} // namespace Locale
} // namespace CalamaresUtils

View file

@ -47,6 +47,12 @@ namespace Locale
int count() const { return m_strings.count(); }
/// @brief Gets the string in the current locale
QString get() const;
/// @brief Gets the string from the given locale
QString get(const QLocale&) const;
private:
// Maps locale name to human-readable string, "" is English
QMap< QString, QString > m_strings;