From 683bad19fc03a2ea16ffc311456917b408d8322d Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 7 Sep 2021 17:15:30 +0200 Subject: [PATCH] i18n: introduce a "TranslationFix" This is intended to apply translations to some common Qt UI components. Example: a QMessageBox with standard buttons OK and Cancel; the text for that is determined at startup using the system locale, and later changes to the current locale or the current translation catalog, do not affect OK and Cancel. It might be possible to load a catalog with the right translation strings, except that there is no way to know what the context or catalog **is** for the strings that are used to label standard buttons: they can come from Qt base, or the platform, or the theme. Merely loading the Qt Base translations for the correct language does not help, because those translations do not contain an "OK" string with the context used for standard buttons. Do the translation by hand; then we have all of the Calamares languages covered, too, which is more than the Qt translations do. --- src/libcalamaresui/CMakeLists.txt | 1 + src/libcalamaresui/widgets/TranslationFix.cpp | 40 +++++++++++++++++++ src/libcalamaresui/widgets/TranslationFix.h | 31 ++++++++++++++ src/modules/welcome/WelcomePage.cpp | 2 + 4 files changed, 74 insertions(+) create mode 100644 src/libcalamaresui/widgets/TranslationFix.cpp create mode 100644 src/libcalamaresui/widgets/TranslationFix.h diff --git a/src/libcalamaresui/CMakeLists.txt b/src/libcalamaresui/CMakeLists.txt index 20fae9a19..a704b7484 100644 --- a/src/libcalamaresui/CMakeLists.txt +++ b/src/libcalamaresui/CMakeLists.txt @@ -29,6 +29,7 @@ set( calamaresui_SOURCES widgets/ClickableLabel.cpp widgets/FixedAspectRatioLabel.cpp widgets/PrettyRadioButton.cpp + widgets/TranslationFix.cpp widgets/WaitingWidget.cpp ${CMAKE_SOURCE_DIR}/3rdparty/waitingspinnerwidget.cpp diff --git a/src/libcalamaresui/widgets/TranslationFix.cpp b/src/libcalamaresui/widgets/TranslationFix.cpp new file mode 100644 index 000000000..6bd2900be --- /dev/null +++ b/src/libcalamaresui/widgets/TranslationFix.cpp @@ -0,0 +1,40 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2021 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + */ + +#include "TranslationFix.h" + +#include +#include +#include + +namespace Calamares +{ + +void +fixButtonLabels( QMessageBox* box ) +{ + if ( !box ) + { + return; + } + + static std::pair< decltype( QMessageBox::Ok ), const char* > maps[] + = { { QMessageBox::Ok, QT_TRANSLATE_NOOP( "StandardButtons", "&OK" ) } }; + + for ( auto [ sb, label ] : maps ) + { + auto* button = box->button( sb ); + if ( button ) + { + button->setText( QCoreApplication::translate( "StandardButtons", label ) ); + } + } +} + +} // namespace Calamares diff --git a/src/libcalamaresui/widgets/TranslationFix.h b/src/libcalamaresui/widgets/TranslationFix.h new file mode 100644 index 000000000..107dad67d --- /dev/null +++ b/src/libcalamaresui/widgets/TranslationFix.h @@ -0,0 +1,31 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2021 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + */ + +#ifndef LIBCALAMARESUI_WIDGETS_TRANSLATIONFIX_H +#define LIBCALAMARESUI_WIDGETS_TRANSLATIONFIX_H + +#include "DllMacro.h" + +class QMessageBox; + +namespace Calamares +{ + +/** @brief Fixes the labels on the standard buttons of the message box + * + * Updates OK / Cancel / Yes / No because there does not + * seem to be a way to do so in the Retranslator code + * (in libcalamares) since the translated strings may come + * from a variety of platform-plugin sources and we can't + * guess the context. + */ +void UIDLLEXPORT fixButtonLabels( QMessageBox* ); +} // namespace Calamares + +#endif diff --git a/src/modules/welcome/WelcomePage.cpp b/src/modules/welcome/WelcomePage.cpp index a82d873e9..1ea3f2429 100644 --- a/src/modules/welcome/WelcomePage.cpp +++ b/src/modules/welcome/WelcomePage.cpp @@ -26,6 +26,7 @@ #include "utils/Logger.h" #include "utils/NamedEnum.h" #include "utils/Retranslator.h" +#include "widgets/TranslationFix.h" #include #include @@ -251,6 +252,7 @@ WelcomePage::showAboutBox() .arg( Calamares::Branding::instance()->versionedName() ), QMessageBox::Ok, this ); + Calamares::fixButtonLabels( &mb ); mb.setIconPixmap( CalamaresUtils::defaultPixmap( CalamaresUtils::Squid, CalamaresUtils::Original,