mirror of
https://github.com/parchlinux/calamares.git
synced 2025-02-24 19:05:46 -05:00
[libcalamares] Implement getting the string
This commit is contained in:
parent
50d74c4eca
commit
a9292d0c75
3 changed files with 98 additions and 67 deletions
|
@ -21,8 +21,8 @@
|
|||
#include "locale/LabelModel.h"
|
||||
#include "locale/TranslatableConfiguration.h"
|
||||
|
||||
#include "utils/Logger.h"
|
||||
#include "CalamaresVersion.h"
|
||||
#include "utils/Logger.h"
|
||||
|
||||
#include <QtTest/QtTest>
|
||||
|
||||
|
@ -88,60 +88,64 @@ LocaleTests::testEsperanto()
|
|||
static const QStringList&
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void LocaleTests::testTranslatableLanguages()
|
||||
|
||||
void
|
||||
LocaleTests::testTranslatableLanguages()
|
||||
{
|
||||
QStringList availableLanguages = QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';' );
|
||||
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
|
||||
QCOMPARE( availableLanguages.contains( language ) ? language : QString(), language );
|
||||
}
|
||||
}
|
||||
|
||||
void LocaleTests::testTranslatableConfig1()
|
||||
void
|
||||
LocaleTests::testTranslatableConfig1()
|
||||
{
|
||||
CalamaresUtils::Locale::TranslatedString ts1( "Hello" );
|
||||
QCOMPARE( ts1.count(), 1 );
|
||||
|
||||
|
||||
QCOMPARE( ts1.get(), "Hello" );
|
||||
QCOMPARE( ts1.get( QLocale("nl")), "Hello" );
|
||||
|
||||
QCOMPARE( ts1.get( QLocale( "nl" ) ), "Hello" );
|
||||
|
||||
QVariantMap map;
|
||||
map.insert( "description", "description (no language)" );
|
||||
CalamaresUtils::Locale::TranslatedString ts2(map, "description");
|
||||
CalamaresUtils::Locale::TranslatedString ts2( map, "description" );
|
||||
QCOMPARE( ts2.count(), 1 );
|
||||
|
||||
QCOMPARE( ts2.get(), "description (no language)");
|
||||
QCOMPARE( ts2.get( QLocale( "nl" ) ), "description (no language)");
|
||||
|
||||
QCOMPARE( ts2.get(), "description (no language)" );
|
||||
QCOMPARE( ts2.get( QLocale( "nl" ) ), "description (no language)" );
|
||||
}
|
||||
|
||||
void LocaleTests::testTranslatableConfig2()
|
||||
void
|
||||
LocaleTests::testTranslatableConfig2()
|
||||
{
|
||||
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" )
|
||||
{
|
||||
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
|
||||
QCOMPARE( ts1.get( QLocale( "nl" ) ), "description (language nl)");
|
||||
|
||||
CalamaresUtils::Locale::TranslatedString ts2(map, "name");
|
||||
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
|
||||
QCOMPARE( ts2.count(), someLanguages().count() );
|
||||
}
|
||||
|
|
|
@ -30,11 +30,11 @@ namespace CalamaresUtils
|
|||
{
|
||||
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
|
||||
QString value = CalamaresUtils::getString( map, key );
|
||||
|
@ -42,11 +42,11 @@ TranslatedString::TranslatedString(const QVariantMap& map, const QString& key)
|
|||
{
|
||||
value = key;
|
||||
}
|
||||
m_strings[QString()] = value;
|
||||
|
||||
m_strings[ QString() ] = value;
|
||||
|
||||
for ( auto it = map.constKeyValueBegin(); it != map.constKeyValueEnd(); ++it )
|
||||
{
|
||||
QString subkey = (*it).first;
|
||||
QString subkey = ( *it ).first;
|
||||
if ( subkey == key )
|
||||
{
|
||||
// Already obtained, above
|
||||
|
@ -54,24 +54,51 @@ TranslatedString::TranslatedString(const QVariantMap& map, const QString& key)
|
|||
else if ( subkey.startsWith( key ) )
|
||||
{
|
||||
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);
|
||||
m_strings[language] = (*it).second.toString();
|
||||
QString language = match.captured( 1 );
|
||||
m_strings[ language ] = ( *it ).second.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QString TranslatedString::get() const
|
||||
QString
|
||||
TranslatedString::get() const
|
||||
{
|
||||
return get( QLocale() );
|
||||
}
|
||||
|
||||
QString TranslatedString::get(const QLocale& locale) const
|
||||
QString
|
||||
TranslatedString::get( const QLocale& locale ) const
|
||||
{
|
||||
cDebug() << "Getting locale" << locale.name();
|
||||
return m_strings[QString()];
|
||||
QString localeName = locale.name();
|
||||
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
|
||||
{
|
||||
/** @brief A human-readable string from a configuration file
|
||||
*
|
||||
* The configuration files can contain human-readable strings,
|
||||
* but those need their own translations and are not supported
|
||||
* by QObject::tr or anything else.
|
||||
/** @brief A human-readable string from a configuration file
|
||||
*
|
||||
* The configuration files can contain human-readable strings,
|
||||
* but those need their own translations and are not supported
|
||||
* by QObject::tr or anything else.
|
||||
*/
|
||||
class DLLEXPORT TranslatedString
|
||||
{
|
||||
public:
|
||||
/** @brief Get all the translations connected to @p key
|
||||
*/
|
||||
class DLLEXPORT TranslatedString
|
||||
{
|
||||
public:
|
||||
/** @brief Get all the translations connected to @p key
|
||||
*/
|
||||
TranslatedString( const QVariantMap& map, const QString& key );
|
||||
/** @brief Not-actually-translated string.
|
||||
*/
|
||||
TranslatedString( const QString& string );
|
||||
|
||||
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;
|
||||
};
|
||||
TranslatedString( const QVariantMap& map, const QString& key );
|
||||
/** @brief Not-actually-translated string.
|
||||
*/
|
||||
TranslatedString( const QString& string );
|
||||
|
||||
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;
|
||||
};
|
||||
} // namespace Locale
|
||||
} // namespace CalamaresUtils
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue