Merge branch 'translatable-configuration'
This commit is contained in:
commit
39b7e4c55f
@ -34,6 +34,7 @@ set( libSources
|
||||
locale/Label.cpp
|
||||
locale/LabelModel.cpp
|
||||
locale/Lookup.cpp
|
||||
locale/TranslatableConfiguration.cpp
|
||||
|
||||
# Partition service
|
||||
partition/PartitionSize.cpp
|
||||
|
@ -58,7 +58,7 @@ public:
|
||||
|
||||
/** @brief Define a sorting order.
|
||||
*
|
||||
* English (@see isEnglish() -- it means en_US) is sorted at the top.
|
||||
* Locales are sorted by their id, which means the ISO 2-letter code + country.
|
||||
*/
|
||||
bool operator<( const Label& other ) const { return m_localeId < other.m_localeId; }
|
||||
|
||||
@ -78,6 +78,7 @@ public:
|
||||
QLocale locale() const { return m_locale; }
|
||||
|
||||
QString name() const { return m_locale.name(); }
|
||||
QString id() const { return m_localeId; }
|
||||
|
||||
/// @brief Convenience accessor to the language part of the locale
|
||||
QLocale::Language language() const { return m_locale.language(); }
|
||||
|
@ -19,6 +19,9 @@
|
||||
#include "Tests.h"
|
||||
|
||||
#include "locale/LabelModel.h"
|
||||
#include "locale/TranslatableConfiguration.h"
|
||||
|
||||
#include "CalamaresVersion.h"
|
||||
#include "utils/Logger.h"
|
||||
|
||||
#include <QtTest/QtTest>
|
||||
@ -84,3 +87,84 @@ LocaleTests::testEsperanto()
|
||||
QCOMPARE( QLocale( "eo" ).language(), QLocale::Esperanto );
|
||||
#endif
|
||||
}
|
||||
|
||||
static const QStringList&
|
||||
someLanguages()
|
||||
{
|
||||
static QStringList languages { "nl", "de", "da", "nb", "sr@latin", "ar", "ru" };
|
||||
return languages;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
LocaleTests::testTranslatableLanguages()
|
||||
{
|
||||
QStringList availableLanguages = QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';' );
|
||||
cDebug() << "Translation languages:" << availableLanguages;
|
||||
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()
|
||||
{
|
||||
QCOMPARE( QLocale().name(), "C" ); // Otherwise plain get() is dubious
|
||||
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()
|
||||
{
|
||||
QCOMPARE( QLocale().name(), "C" ); // Otherwise plain get() is dubious
|
||||
QVariantMap map;
|
||||
|
||||
for ( const auto& language : someLanguages() )
|
||||
{
|
||||
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 ) );
|
||||
}
|
||||
}
|
||||
|
||||
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)" );
|
||||
for ( const auto& language : someLanguages() )
|
||||
{
|
||||
// Skip Serbian (latin) because QLocale() constructed with it
|
||||
// doesn't retain the @latin part.
|
||||
if ( language == "sr@latin" )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
// Could be QVERIFY, but then we don't see what language code fails
|
||||
QCOMPARE( ts1.get( language ) == QString( "description (language %1)" ).arg( language ) ? language : QString(),
|
||||
language );
|
||||
}
|
||||
QCOMPARE( ts1.get( QLocale( QLocale::Language::Serbian, QLocale::Script::LatinScript, QLocale::Country::Serbia ) ),
|
||||
"description (language sr@latin)" );
|
||||
|
||||
CalamaresUtils::Locale::TranslatedString ts2( map, "name" );
|
||||
// We skipped dutch this time
|
||||
QCOMPARE( ts2.count(), someLanguages().count() );
|
||||
}
|
||||
|
@ -33,6 +33,9 @@ private Q_SLOTS:
|
||||
|
||||
void testLanguageModelCount();
|
||||
void testEsperanto();
|
||||
void testTranslatableLanguages();
|
||||
void testTranslatableConfig1();
|
||||
void testTranslatableConfig2();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
112
src/libcalamares/locale/TranslatableConfiguration.cpp
Normal file
112
src/libcalamares/locale/TranslatableConfiguration.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2019, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* Calamares is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Calamares is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "TranslatableConfiguration.h"
|
||||
|
||||
#include "LabelModel.h"
|
||||
|
||||
#include "utils/Logger.h"
|
||||
#include "utils/Variant.h"
|
||||
|
||||
#include <QRegularExpression>
|
||||
#include <QRegularExpressionMatch>
|
||||
|
||||
namespace CalamaresUtils
|
||||
{
|
||||
namespace Locale
|
||||
{
|
||||
TranslatedString::TranslatedString( const QString& string )
|
||||
{
|
||||
m_strings[ QString() ] = string;
|
||||
}
|
||||
TranslatedString::TranslatedString( const QVariantMap& map, const QString& key )
|
||||
{
|
||||
// Get the un-decorated value for the key
|
||||
QString value = CalamaresUtils::getString( map, key );
|
||||
if ( value.isEmpty() )
|
||||
{
|
||||
value = key;
|
||||
}
|
||||
m_strings[ QString() ] = value;
|
||||
|
||||
for ( auto it = map.constKeyValueBegin(); it != map.constKeyValueEnd(); ++it )
|
||||
{
|
||||
QString subkey = ( *it ).first;
|
||||
if ( subkey == key )
|
||||
{
|
||||
// Already obtained, above
|
||||
}
|
||||
else if ( subkey.startsWith( key ) )
|
||||
{
|
||||
QRegularExpressionMatch match;
|
||||
if ( subkey.indexOf( QRegularExpression( "\\[([a-zA-Z_@]*)\\]" ), 0, &match ) > 0 )
|
||||
{
|
||||
QString language = match.captured( 1 );
|
||||
m_strings[ language ] = ( *it ).second.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QString
|
||||
TranslatedString::get() const
|
||||
{
|
||||
return get( QLocale() );
|
||||
}
|
||||
|
||||
QString
|
||||
TranslatedString::get( const QLocale& locale ) const
|
||||
{
|
||||
QString localeName = locale.name();
|
||||
// Special case, sr@latin doesn't have the @latin reflected in the name
|
||||
if ( locale.language() == QLocale::Language::Serbian && locale.script() == QLocale::Script::LatinScript )
|
||||
{
|
||||
localeName = QStringLiteral( "sr@latin" );
|
||||
}
|
||||
|
||||
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() ];
|
||||
}
|
||||
|
||||
|
||||
} // namespace Locale
|
||||
} // namespace CalamaresUtils
|
63
src/libcalamares/locale/TranslatableConfiguration.h
Normal file
63
src/libcalamares/locale/TranslatableConfiguration.h
Normal file
@ -0,0 +1,63 @@
|
||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2019, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* Calamares is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Calamares is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef LOCALE_TRANSLATABLECONFIGURATION_H
|
||||
#define LOCALE_TRANSLATABLECONFIGURATION_H
|
||||
|
||||
#include "DllMacro.h"
|
||||
|
||||
#include <QLocale>
|
||||
#include <QMap>
|
||||
#include <QVariant>
|
||||
|
||||
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.
|
||||
*/
|
||||
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;
|
||||
};
|
||||
} // namespace Locale
|
||||
} // namespace CalamaresUtils
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user