[libcalamares] Implement getting the string
This commit is contained in:
parent
50d74c4eca
commit
a9292d0c75
@ -21,8 +21,8 @@
|
|||||||
#include "locale/LabelModel.h"
|
#include "locale/LabelModel.h"
|
||||||
#include "locale/TranslatableConfiguration.h"
|
#include "locale/TranslatableConfiguration.h"
|
||||||
|
|
||||||
#include "utils/Logger.h"
|
|
||||||
#include "CalamaresVersion.h"
|
#include "CalamaresVersion.h"
|
||||||
|
#include "utils/Logger.h"
|
||||||
|
|
||||||
#include <QtTest/QtTest>
|
#include <QtTest/QtTest>
|
||||||
|
|
||||||
@ -88,60 +88,64 @@ LocaleTests::testEsperanto()
|
|||||||
static const QStringList&
|
static const QStringList&
|
||||||
someLanguages()
|
someLanguages()
|
||||||
{
|
{
|
||||||
static QStringList languages{ "nl", "de", "da", "nb", "sr@latin", "ar", "ru" };
|
static QStringList languages { "nl", "de", "da", "nb", "sr@latin", "ar", "ru" };
|
||||||
return languages;
|
return languages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void LocaleTests::testTranslatableLanguages()
|
void
|
||||||
|
LocaleTests::testTranslatableLanguages()
|
||||||
{
|
{
|
||||||
QStringList availableLanguages = QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';' );
|
QStringList availableLanguages = QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';' );
|
||||||
cDebug() << "Translation languages:" << availableLanguages;
|
cDebug() << "Translation languages:" << availableLanguages;
|
||||||
for ( const auto& language: someLanguages() )
|
for ( const auto& language : someLanguages() )
|
||||||
{
|
{
|
||||||
// Could be QVERIFY, but then we don't see what language code fails
|
// Could be QVERIFY, but then we don't see what language code fails
|
||||||
QCOMPARE( availableLanguages.contains( language ) ? language : QString(), language );
|
QCOMPARE( availableLanguages.contains( language ) ? language : QString(), language );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LocaleTests::testTranslatableConfig1()
|
void
|
||||||
|
LocaleTests::testTranslatableConfig1()
|
||||||
{
|
{
|
||||||
CalamaresUtils::Locale::TranslatedString ts1( "Hello" );
|
CalamaresUtils::Locale::TranslatedString ts1( "Hello" );
|
||||||
QCOMPARE( ts1.count(), 1 );
|
QCOMPARE( ts1.count(), 1 );
|
||||||
|
|
||||||
QCOMPARE( ts1.get(), "Hello" );
|
QCOMPARE( ts1.get(), "Hello" );
|
||||||
QCOMPARE( ts1.get( QLocale("nl")), "Hello" );
|
QCOMPARE( ts1.get( QLocale( "nl" ) ), "Hello" );
|
||||||
|
|
||||||
QVariantMap map;
|
QVariantMap map;
|
||||||
map.insert( "description", "description (no language)" );
|
map.insert( "description", "description (no language)" );
|
||||||
CalamaresUtils::Locale::TranslatedString ts2(map, "description");
|
CalamaresUtils::Locale::TranslatedString ts2( map, "description" );
|
||||||
QCOMPARE( ts2.count(), 1 );
|
QCOMPARE( ts2.count(), 1 );
|
||||||
|
|
||||||
QCOMPARE( ts2.get(), "description (no language)");
|
QCOMPARE( ts2.get(), "description (no language)" );
|
||||||
QCOMPARE( ts2.get( QLocale( "nl" ) ), "description (no language)");
|
QCOMPARE( ts2.get( QLocale( "nl" ) ), "description (no language)" );
|
||||||
}
|
}
|
||||||
|
|
||||||
void LocaleTests::testTranslatableConfig2()
|
void
|
||||||
|
LocaleTests::testTranslatableConfig2()
|
||||||
{
|
{
|
||||||
QVariantMap map;
|
QVariantMap map;
|
||||||
|
|
||||||
for ( const auto& language: someLanguages() )
|
for ( const auto& language : someLanguages() )
|
||||||
{
|
{
|
||||||
map.insert( QString("description[%1]").arg(language), QString("description (language %1)").arg(language) );
|
map.insert( QString( "description[%1]" ).arg( language ),
|
||||||
|
QString( "description (language %1)" ).arg( language ) );
|
||||||
if ( language != "nl" )
|
if ( language != "nl" )
|
||||||
{
|
{
|
||||||
map.insert( QString("name[%1]").arg(language), QString("name (language %1)").arg(language) );
|
map.insert( QString( "name[%1]" ).arg( language ), QString( "name (language %1)" ).arg( language ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
CalamaresUtils::Locale::TranslatedString ts1( map, "description" );
|
||||||
QCOMPARE( ts1.get( QLocale( "nl" ) ), "description (language nl)");
|
// The +1 is because "" is always also inserted
|
||||||
|
QCOMPARE( ts1.count(), someLanguages().count() + 1 );
|
||||||
CalamaresUtils::Locale::TranslatedString ts2(map, "name");
|
|
||||||
|
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
|
// We skipped dutch this time
|
||||||
QCOMPARE( ts2.count(), someLanguages().count() );
|
QCOMPARE( ts2.count(), someLanguages().count() );
|
||||||
}
|
}
|
||||||
|
@ -30,11 +30,11 @@ namespace CalamaresUtils
|
|||||||
{
|
{
|
||||||
namespace Locale
|
namespace Locale
|
||||||
{
|
{
|
||||||
TranslatedString::TranslatedString(const QString& string)
|
TranslatedString::TranslatedString( const QString& string )
|
||||||
{
|
{
|
||||||
m_strings[QString()]=string;
|
m_strings[ QString() ] = string;
|
||||||
}
|
}
|
||||||
TranslatedString::TranslatedString(const QVariantMap& map, const QString& key)
|
TranslatedString::TranslatedString( const QVariantMap& map, const QString& key )
|
||||||
{
|
{
|
||||||
// Get the un-decorated value for the key
|
// Get the un-decorated value for the key
|
||||||
QString value = CalamaresUtils::getString( map, key );
|
QString value = CalamaresUtils::getString( map, key );
|
||||||
@ -42,11 +42,11 @@ TranslatedString::TranslatedString(const QVariantMap& map, const QString& key)
|
|||||||
{
|
{
|
||||||
value = key;
|
value = key;
|
||||||
}
|
}
|
||||||
m_strings[QString()] = value;
|
m_strings[ QString() ] = value;
|
||||||
|
|
||||||
for ( auto it = map.constKeyValueBegin(); it != map.constKeyValueEnd(); ++it )
|
for ( auto it = map.constKeyValueBegin(); it != map.constKeyValueEnd(); ++it )
|
||||||
{
|
{
|
||||||
QString subkey = (*it).first;
|
QString subkey = ( *it ).first;
|
||||||
if ( subkey == key )
|
if ( subkey == key )
|
||||||
{
|
{
|
||||||
// Already obtained, above
|
// Already obtained, above
|
||||||
@ -54,24 +54,51 @@ TranslatedString::TranslatedString(const QVariantMap& map, const QString& key)
|
|||||||
else if ( subkey.startsWith( key ) )
|
else if ( subkey.startsWith( key ) )
|
||||||
{
|
{
|
||||||
QRegularExpressionMatch match;
|
QRegularExpressionMatch match;
|
||||||
if ( subkey.indexOf( QRegularExpression("\\[([a-zA-Z_@]*)\\]"), 0, &match ) > 0 )
|
if ( subkey.indexOf( QRegularExpression( "\\[([a-zA-Z_@]*)\\]" ), 0, &match ) > 0 )
|
||||||
{
|
{
|
||||||
QString language = match.captured(1);
|
QString language = match.captured( 1 );
|
||||||
m_strings[language] = (*it).second.toString();
|
m_strings[ language ] = ( *it ).second.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QString TranslatedString::get() const
|
QString
|
||||||
|
TranslatedString::get() const
|
||||||
{
|
{
|
||||||
return get( QLocale() );
|
return get( QLocale() );
|
||||||
}
|
}
|
||||||
|
|
||||||
QString TranslatedString::get(const QLocale& locale) const
|
QString
|
||||||
|
TranslatedString::get( const QLocale& locale ) const
|
||||||
{
|
{
|
||||||
cDebug() << "Getting locale" << locale.name();
|
QString localeName = locale.name();
|
||||||
return m_strings[QString()];
|
cDebug() << "Getting locale" << localeName;
|
||||||
|
if ( m_strings.contains( localeName ) )
|
||||||
|
{
|
||||||
|
return m_strings[ localeName ];
|
||||||
|
}
|
||||||
|
int index = localeName.indexOf( '@' );
|
||||||
|
if ( index > 0 )
|
||||||
|
{
|
||||||
|
localeName.truncate( index );
|
||||||
|
if ( m_strings.contains( localeName ) )
|
||||||
|
{
|
||||||
|
return m_strings[ localeName ];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
index = localeName.indexOf( '_' );
|
||||||
|
if ( index > 0 )
|
||||||
|
{
|
||||||
|
localeName.truncate( index );
|
||||||
|
if ( m_strings.contains( localeName ) )
|
||||||
|
{
|
||||||
|
return m_strings[ localeName ];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_strings[ QString() ];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,34 +29,34 @@ namespace CalamaresUtils
|
|||||||
{
|
{
|
||||||
namespace Locale
|
namespace Locale
|
||||||
{
|
{
|
||||||
/** @brief A human-readable string from a configuration file
|
/** @brief A human-readable string from a configuration file
|
||||||
*
|
*
|
||||||
* The configuration files can contain human-readable strings,
|
* The configuration files can contain human-readable strings,
|
||||||
* but those need their own translations and are not supported
|
* but those need their own translations and are not supported
|
||||||
* by QObject::tr or anything else.
|
* by QObject::tr or anything else.
|
||||||
|
*/
|
||||||
|
class DLLEXPORT TranslatedString
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/** @brief Get all the translations connected to @p key
|
||||||
*/
|
*/
|
||||||
class DLLEXPORT TranslatedString
|
TranslatedString( const QVariantMap& map, const QString& key );
|
||||||
{
|
/** @brief Not-actually-translated string.
|
||||||
public:
|
*/
|
||||||
/** @brief Get all the translations connected to @p key
|
TranslatedString( const QString& string );
|
||||||
*/
|
|
||||||
TranslatedString( const QVariantMap& map, const QString& key );
|
int count() const { return m_strings.count(); }
|
||||||
/** @brief Not-actually-translated string.
|
|
||||||
*/
|
/// @brief Gets the string in the current locale
|
||||||
TranslatedString( const QString& string );
|
QString get() const;
|
||||||
|
|
||||||
int count() const { return m_strings.count(); }
|
/// @brief Gets the string from the given locale
|
||||||
|
QString get( const QLocale& ) const;
|
||||||
/// @brief Gets the string in the current locale
|
|
||||||
QString get() const;
|
private:
|
||||||
|
// Maps locale name to human-readable string, "" is English
|
||||||
/// @brief Gets the string from the given locale
|
QMap< QString, QString > m_strings;
|
||||||
QString get(const QLocale&) const;
|
};
|
||||||
|
|
||||||
private:
|
|
||||||
// Maps locale name to human-readable string, "" is English
|
|
||||||
QMap< QString, QString > m_strings;
|
|
||||||
};
|
|
||||||
} // namespace Locale
|
} // namespace Locale
|
||||||
} // namespace CalamaresUtils
|
} // namespace CalamaresUtils
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user