From 0ef28f6a50aa5e9d4886968f688ec6edd35930e3 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 19 Feb 2020 14:02:04 +0100 Subject: [PATCH 1/4] [libcalamares] Translatable config strings use tr()-infrastructure - Allow TranslatedString to get a context parameter; if it has one, it will try to use the regular tr()-infrastructure **as fallback** for the translations from the config file itself. - This makes it possible to offer -- and translate -- some "standard" phrases in the module, while allowing the config file the knob to change strings. Using one of the standard strings gets translations for "free", while introducing something entirely new means sourcing translations for it as well. --- .../locale/TranslatableConfiguration.cpp | 17 +++++++++++++++-- .../locale/TranslatableConfiguration.h | 3 ++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/libcalamares/locale/TranslatableConfiguration.cpp b/src/libcalamares/locale/TranslatableConfiguration.cpp index bc39c398e..83199a4cc 100644 --- a/src/libcalamares/locale/TranslatableConfiguration.cpp +++ b/src/libcalamares/locale/TranslatableConfiguration.cpp @@ -23,6 +23,7 @@ #include "utils/Logger.h" #include "utils/Variant.h" +#include #include #include @@ -34,7 +35,9 @@ TranslatedString::TranslatedString( const QString& string ) { m_strings[ QString() ] = string; } -TranslatedString::TranslatedString( const QVariantMap& map, const QString& key ) + +TranslatedString::TranslatedString( const QVariantMap& map, const QString& key, const char* context ) + : m_context( context ) { // Get the un-decorated value for the key QString value = CalamaresUtils::getString( map, key ); @@ -99,7 +102,17 @@ TranslatedString::get( const QLocale& locale ) const } } - return m_strings[ QString() ]; + // If we're given a context to work with, also try the same string in + // the regular translation framework. + const QString& s = m_strings[ QString() ]; + if ( m_context ) + { + return QCoreApplication::translate( m_context, s.toLatin1().constData() ); + } + else + { + return s; + } } diff --git a/src/libcalamares/locale/TranslatableConfiguration.h b/src/libcalamares/locale/TranslatableConfiguration.h index 45679bce0..f1abb51ff 100644 --- a/src/libcalamares/locale/TranslatableConfiguration.h +++ b/src/libcalamares/locale/TranslatableConfiguration.h @@ -40,7 +40,7 @@ class DLLEXPORT TranslatedString public: /** @brief Get all the translations connected to @p key */ - TranslatedString( const QVariantMap& map, const QString& key ); + TranslatedString( const QVariantMap& map, const QString& key, const char* context = nullptr ); /** @brief Not-actually-translated string. */ TranslatedString( const QString& string ); @@ -73,6 +73,7 @@ public: private: // Maps locale name to human-readable string, "" is English QMap< QString, QString > m_strings; + const char* m_context = nullptr; }; } // namespace Locale } // namespace CalamaresUtils From a03394f177e3509ff0d4acf884b288c1aa770436 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 19 Feb 2020 14:09:04 +0100 Subject: [PATCH 2/4] [netinstall] Use tr() infrastructure for config-translations --- src/modules/netinstall/NetInstallViewStep.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/netinstall/NetInstallViewStep.cpp b/src/modules/netinstall/NetInstallViewStep.cpp index 3227ecd65..6c26061a7 100644 --- a/src/modules/netinstall/NetInstallViewStep.cpp +++ b/src/modules/netinstall/NetInstallViewStep.cpp @@ -199,10 +199,10 @@ NetInstallViewStep::setConfigurationMap( const QVariantMap& configurationMap ) if ( label.contains( "sidebar" ) ) { - m_sidebarLabel = new CalamaresUtils::Locale::TranslatedString( label, "sidebar" ); + m_sidebarLabel = new CalamaresUtils::Locale::TranslatedString( label, "sidebar", metaObject()->className() ); } if ( label.contains( "title" ) ) { - m_widget->setPageTitle( new CalamaresUtils::Locale::TranslatedString( label, "title" ) ); + m_widget->setPageTitle( new CalamaresUtils::Locale::TranslatedString( label, "title", metaObject()->className() ) ); } } From 81752b6f7c927b902a76d791c3e179320dd31492 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 19 Feb 2020 14:23:34 +0100 Subject: [PATCH 3/4] [libcalamares] Document how TranslatedString context works - Support re-using class-specific tr() calls in a standard way - Document this in the netinstall.conf which uses it --- src/libcalamares/locale/TranslatableConfiguration.h | 11 +++++++++++ src/modules/netinstall/netinstall.conf | 12 +++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/libcalamares/locale/TranslatableConfiguration.h b/src/libcalamares/locale/TranslatableConfiguration.h index f1abb51ff..d845569bc 100644 --- a/src/libcalamares/locale/TranslatableConfiguration.h +++ b/src/libcalamares/locale/TranslatableConfiguration.h @@ -39,6 +39,17 @@ class DLLEXPORT TranslatedString { public: /** @brief Get all the translations connected to @p key + * + * Gets map[key] as the "untranslated" form, and then all the + * keys of the form [lang] are taken as the translation + * for of the untranslated form. + * + * If @p context is not a nullptr, then that is taken as an + * indication to **also** use the regular QObject::tr() translation + * mechanism for these strings. It is recommended to pass in + * metaObject()->className() as context (from a QObject based class) + * to give the TranslatedString the same context as other calls + * to tr() within that class. */ TranslatedString( const QVariantMap& map, const QString& key, const char* context = nullptr ); /** @brief Not-actually-translated string. diff --git a/src/modules/netinstall/netinstall.conf b/src/modules/netinstall/netinstall.conf index ab600326e..188642683 100644 --- a/src/modules/netinstall/netinstall.conf +++ b/src/modules/netinstall/netinstall.conf @@ -30,8 +30,14 @@ required: false # If no *sidebar* values are provided, defaults to "Package selection" # and existing translations. If no *title* values are provided, no string # is displayed. +# +# The following strings are already known to Calamares and can be +# listed here in *untranslated* form (e.g. as value of *sidebar*) +# without bothering with the translations: they are picked up from +# the regular translation framework: +# - "Package selection" label: sidebar: "Package selection" - sidebar[nl]: "Pakketkeuze" - title: "Office Package" - title[nl]: "Kantoorsoftware" + # sidebar[nl]: "Pakketkeuze" + # title: "Office Package" + # title[nl]: "Kantoorsoftware" From 538779991ed4c4066a428f0a3b444fa04a95b64e Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 19 Feb 2020 14:37:47 +0100 Subject: [PATCH 4/4] [netinstall] Add some "standard" sidebar labels for the module --- src/modules/netinstall/NetInstallViewStep.cpp | 14 +++++++++++++- src/modules/netinstall/netinstall.conf | 5 +++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/modules/netinstall/NetInstallViewStep.cpp b/src/modules/netinstall/NetInstallViewStep.cpp index 6c26061a7..8de3fd74f 100644 --- a/src/modules/netinstall/NetInstallViewStep.cpp +++ b/src/modules/netinstall/NetInstallViewStep.cpp @@ -55,6 +55,17 @@ QString NetInstallViewStep::prettyName() const { return m_sidebarLabel ? m_sidebarLabel->get() : tr( "Package selection" ); + + // This is a table of "standard" labels for this module. If you use them + // in the label: sidebar: section of the config file, the existing + // translations can be used. + NOTREACHED + tr( "Package selection" ); + tr( "Office software" ); + tr( "Office package" ); + tr( "Browser software" ); + tr( "Browser package" ); + tr( "Web browser" ); } @@ -203,6 +214,7 @@ NetInstallViewStep::setConfigurationMap( const QVariantMap& configurationMap ) } if ( label.contains( "title" ) ) { - m_widget->setPageTitle( new CalamaresUtils::Locale::TranslatedString( label, "title", metaObject()->className() ) ); + m_widget->setPageTitle( + new CalamaresUtils::Locale::TranslatedString( label, "title", metaObject()->className() ) ); } } diff --git a/src/modules/netinstall/netinstall.conf b/src/modules/netinstall/netinstall.conf index 188642683..5f90fec76 100644 --- a/src/modules/netinstall/netinstall.conf +++ b/src/modules/netinstall/netinstall.conf @@ -36,6 +36,11 @@ required: false # without bothering with the translations: they are picked up from # the regular translation framework: # - "Package selection" +# - "Office software" +# - "Office package" +# - "Browser software" +# - "Browser package" +# - "Web browser" label: sidebar: "Package selection" # sidebar[nl]: "Pakketkeuze"