From 8ea4091c7b6d14e52367ef2f8bda8dfee2ec23b5 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Sun, 4 Aug 2019 22:56:41 +0200 Subject: [PATCH] [libcalamares] Add a TranslatedString class to locale/ - While QObject::tr and gettext give us translations **most** of the time via the translation mechanism, we sometimes have strings embedded in configuration files that need to be shown to people as well. Follow the .desktop style in handling this. - A key's value **might** be translated; use `key[lang]` for the translation into one of the languages that Calamares understands. Code that expects a translated (human-readable) string in a configuration file can use TranslatedString to collect all the translations of a given key, so that it displays the right string from the configuration when needed. --- src/libcalamares/CMakeLists.txt | 1 + .../locale/TranslatableConfiguration.cpp | 67 +++++++++++++++++++ .../locale/TranslatableConfiguration.h | 54 +++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 src/libcalamares/locale/TranslatableConfiguration.cpp create mode 100644 src/libcalamares/locale/TranslatableConfiguration.h 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/TranslatableConfiguration.cpp b/src/libcalamares/locale/TranslatableConfiguration.cpp new file mode 100644 index 000000000..6d4684121 --- /dev/null +++ b/src/libcalamares/locale/TranslatableConfiguration.cpp @@ -0,0 +1,67 @@ +/* === 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 = m_strings.constKeyValueBegin(); it != m_strings.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); + cDebug() << "Found translation" << key << '[' << language << ']'; + } + } + } +} + +} // namespace Locale +} // namespace CalamaresUtils diff --git a/src/libcalamares/locale/TranslatableConfiguration.h b/src/libcalamares/locale/TranslatableConfiguration.h new file mode 100644 index 000000000..8cc02fadd --- /dev/null +++ b/src/libcalamares/locale/TranslatableConfiguration.h @@ -0,0 +1,54 @@ +/* === 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 ); + private: + // Maps locale name to human-readable string, "" is English + QMap< QString, QString > m_strings; + }; +} // namespace Locale +} // namespace CalamaresUtils + +#endif