]> git.lyx.org Git - features.git/commitdiff
Add a translator as a fallback to Qt inner one
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Tue, 17 Jul 2018 22:41:09 +0000 (00:41 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Wed, 18 Jul 2018 08:13:05 +0000 (10:13 +0200)
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
src/support/Messages.cpp
src/support/Messages.h

index 25eff65ec2221a897a54dd4cb6c64a27e89428ae..e04c3924746381af59b5ee6931e851bc3a44c37c 100644 (file)
@@ -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<int, SocketNotifier *> 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
index 26a320b2d243c753c727b19947aade5ebfe0457b..c87b6b976da36d3795f59390d64c687459448d2c 100644 (file)
@@ -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
index dd24e1c5fccc29b8fba98c90d9f43d9bc26ead6f..eb14aacac59df53c166c149e43303e46e6aa72e9 100644 (file)
@@ -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?