From 30ec879d3a01c0c2845c2cdd589858f05d031865 Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Wed, 18 Jul 2018 00:41:09 +0200 Subject: [PATCH] Add a translator as a fallback to Qt inner one This reuses code intended only for mac manus and generalizes it. The list of strings to add to po files is found in GuiTranslator::translate. This is useful now that LyX relies on QDialogButtonBox class for its dialogs. Indeed many languages are not covered natively by Qt. It is possible to enable the "locace" debug channel to see what strings are not covered and should be added to our own translation tables. In order to make things easier, a new method getIfFound() has been added to the Messages class, which returns an empty string when no translation has been found, as Qt's translate() does. --- src/frontends/qt4/GuiApplication.cpp | 49 ++++++++++++++++------------ src/support/Messages.cpp | 13 ++++++++ src/support/Messages.h | 5 ++- 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/src/frontends/qt4/GuiApplication.cpp b/src/frontends/qt4/GuiApplication.cpp index 25eff65ec2..e04c392474 100644 --- a/src/frontends/qt4/GuiApplication.cpp +++ b/src/frontends/qt4/GuiApplication.cpp @@ -664,16 +664,10 @@ public: }; -//////////////////////////////////////////////////////////////////////// -// -// Mac specific stuff goes here... -// -//////////////////////////////////////////////////////////////////////// - -class MenuTranslator : public QTranslator +class GuiTranslator : public QTranslator { public: - MenuTranslator(QObject * parent) + GuiTranslator(QObject * parent = nullptr) : QTranslator(parent) {} @@ -687,15 +681,30 @@ public: const char * /*comment*/ = 0) const #endif { - string const s = sourceText; - if (s == N_("About %1") || s == N_("Preferences") - || s == N_("Reconfigure") || s == N_("Quit %1")) - return qt_(s); - else - return QString(); +#if 0 + // Here we declare the strings that need to be translated from Qt own GUI + // This is needed to include these strings to po files + _("About %1"); + _("Preferences"); + _("Reconfigure"); + _("Quit %1")); +#endif + docstring s = getGuiMessages().getIfFound(sourceText); + // This test should eventually be removed when translations are updated + if (s.empty()) + LYXERR(Debug::LOCALE, "Missing translation for `" + << string(sourceText) << "'"); + return toqstr(s); } }; + +//////////////////////////////////////////////////////////////////////// +// +// Mac specific stuff goes here... +// +//////////////////////////////////////////////////////////////////////// + #ifdef Q_OS_MAC // QMacPasteboardMimeGraphics can only be compiled on Mac. @@ -944,8 +953,10 @@ struct GuiApplication::Private FontLoader font_loader_; /// ColorCache color_cache_; - /// + /// the built-in Qt translation mechanism QTranslator qt_trans_; + /// LyX gettext-based translation for Qt elements + GuiTranslator gui_trans_; /// QHash socket_notifiers_; /// @@ -1025,7 +1036,9 @@ GuiApplication::GuiApplication(int & argc, char ** argv) qsrand(QDateTime::currentDateTime().toTime_t()); - // Install translator for GUI elements. + // Install LyX translator for missing Qt translations + installTranslator(&d->gui_trans_); + // Install Qt native translator for GUI elements. installTranslator(&d->qt_trans_); #ifdef QPA_XCB @@ -1044,10 +1057,6 @@ GuiApplication::GuiApplication(int & argc, char ** argv) // FIXME: Do we need a lyxrc setting for this on Mac? This behaviour // seems to be the default case for applications like LyX. setQuitOnLastWindowClosed(false); - // This allows to translate the strings that appear in the LyX menu. - /// A translator suitable for the entries in the LyX menu. - /// Only needed with Qt/Mac. - installTranslator(new MenuTranslator(this)); /// setupApplescript(); #endif diff --git a/src/support/Messages.cpp b/src/support/Messages.cpp index 26a320b2d2..c87b6b976d 100644 --- a/src/support/Messages.cpp +++ b/src/support/Messages.cpp @@ -347,6 +347,19 @@ docstring const Messages::get(string const & m) const } } + +docstring const Messages::getIfFound(string const & m) const +{ + if (m.empty()) + return docstring(); + + TranslationMap::const_iterator it = trans_map_.find(m); + if (it != trans_map_.end()) + return it->second; + else + return docstring(); +} + } // namespace lyx #else // ENABLE_NLS diff --git a/src/support/Messages.h b/src/support/Messages.h index dd24e1c5fc..eb14aacac5 100644 --- a/src/support/Messages.h +++ b/src/support/Messages.h @@ -25,8 +25,11 @@ public: Messages() {} /// messages in the language \p l. Messages(std::string const & l); - /// + /// Return the tranlation of message \c msg, or the original + /// string if no context was found. Context is always removed. docstring const get(std::string const & msg) const; + /// + docstring const getIfFound(std::string const & msg) const; /// What is the language associated with this translation? std::string language() const; /// Is an (at least partial) translation of language with code \p c available?