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.
This commit is contained in:
Jean-Marc Lasgouttes 2018-07-18 00:41:09 +02:00
parent 7b57685048
commit 30ec879d3a
3 changed files with 46 additions and 21 deletions

View 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

View 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

View 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?