diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index 19bcc921d..0aca79233 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -34,6 +34,7 @@ set( libSources locale/Label.cpp locale/LabelModel.cpp locale/Lookup.cpp + locale/TranslatableConfiguration.cpp # Partition service partition/PartitionSize.cpp diff --git a/src/libcalamares/locale/Label.h b/src/libcalamares/locale/Label.h index ab3e80ad0..0fe61d909 100644 --- a/src/libcalamares/locale/Label.h +++ b/src/libcalamares/locale/Label.h @@ -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(); } diff --git a/src/libcalamares/locale/Tests.cpp b/src/libcalamares/locale/Tests.cpp index 166154fbb..664390511 100644 --- a/src/libcalamares/locale/Tests.cpp +++ b/src/libcalamares/locale/Tests.cpp @@ -19,6 +19,9 @@ #include "Tests.h" #include "locale/LabelModel.h" +#include "locale/TranslatableConfiguration.h" + +#include "CalamaresVersion.h" #include "utils/Logger.h" #include @@ -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() ); +} diff --git a/src/libcalamares/locale/Tests.h b/src/libcalamares/locale/Tests.h index be712388f..c6949f3e4 100644 --- a/src/libcalamares/locale/Tests.h +++ b/src/libcalamares/locale/Tests.h @@ -33,6 +33,9 @@ private Q_SLOTS: void testLanguageModelCount(); void testEsperanto(); + void testTranslatableLanguages(); + void testTranslatableConfig1(); + void testTranslatableConfig2(); }; #endif diff --git a/src/libcalamares/locale/TranslatableConfiguration.cpp b/src/libcalamares/locale/TranslatableConfiguration.cpp new file mode 100644 index 000000000..82923a5fa --- /dev/null +++ b/src/libcalamares/locale/TranslatableConfiguration.cpp @@ -0,0 +1,112 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * 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 . + */ + +#include "TranslatableConfiguration.h" + +#include "LabelModel.h" + +#include "utils/Logger.h" +#include "utils/Variant.h" + +#include +#include + +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 diff --git a/src/libcalamares/locale/TranslatableConfiguration.h b/src/libcalamares/locale/TranslatableConfiguration.h new file mode 100644 index 000000000..0735a2274 --- /dev/null +++ b/src/libcalamares/locale/TranslatableConfiguration.h @@ -0,0 +1,63 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * 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 . + */ + +#ifndef LOCALE_TRANSLATABLECONFIGURATION_H +#define LOCALE_TRANSLATABLECONFIGURATION_H + +#include "DllMacro.h" + +#include +#include +#include + +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