diff --git a/src/extension/extension.cpp b/src/extension/extension.cpp index 642d39d6ae0cede4016007a81367aeca1dc091e0..c9e8cbe3d40a65da3569bb4267a66a1848f6dad8 100644 --- a/src/extension/extension.cpp +++ b/src/extension/extension.cpp @@ -256,15 +256,17 @@ Extension::loaded () bool Extension::check () { - char const *inx_failure = _(" This is caused by an improper .inx file for this extension." - " An improper .inx file could have been caused by a faulty installation of Inkscape."); + auto get_inx_error_text = []() { + return _(" This is caused by an improper .inx file for this extension." + " An improper .inx file could have been caused by a faulty installation of Inkscape."); + }; if (repr == nullptr) { - printFailure(Glib::ustring(_("the XML description of it got lost.")) += inx_failure); + printFailure(Glib::ustring(_("the XML description of it got lost.")) += get_inx_error_text()); return false; } if (!imp) { - printFailure(Glib::ustring(_("no implementation was defined for the extension.")) += inx_failure); + printFailure(Glib::ustring(_("no implementation was defined for the extension.")) += get_inx_error_text()); return false; } @@ -505,10 +507,26 @@ char const *Extension::get_translation(char const *msgid, char const *msgctxt) c return msgid; } + // Possible enhancement: heterogeneous lookup + // https://stackoverflow.com/questions/49709548/c-unordered-mapstring-lookup-without-constructing-string if (msgctxt) { - return g_dpgettext2(_translationdomain, msgctxt, msgid); + auto& map = _translations_by_context[msgctxt]; + + auto iter = map.find(msgid); + if (iter != map.end()) + return iter->second.c_str(); // TODO: return std::string + // Can g_dgettext return NULL? + auto emplaced = map.emplace(msgid, g_dpgettext2(_translationdomain, msgctxt, msgid)).first; + return emplaced->second.c_str(); } else { - return g_dgettext(_translationdomain, msgid); + auto& map = _translations; + + auto iter = map.find(msgid); + if (iter != map.end()) + return iter->second.c_str(); // TODO: return std::string + // Can g_dgettext return NULL? + auto emplaced = map.emplace(msgid, g_dgettext(_translationdomain, msgid)).first; + return emplaced->second.c_str(); } } diff --git a/src/extension/extension.h b/src/extension/extension.h index 754b779622f230edc728210e0810ff6de2182b1b..f3ed66c6b74bf615e83b5a27690fcf8a4e7225ff 100644 --- a/src/extension/extension.h +++ b/src/extension/extension.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -152,6 +153,11 @@ private: std::vector _actions; /**< Processing actions */ + using string_map = std::unordered_map; + // TODO: use separate object instead of mutable? Or remove const from some methods? + mutable std::unordered_map _translations_by_context; /**< Caches of intl translations by context */ + mutable std::unordered_map _translations; /**< Cache of intl translations */ + protected: Inkscape::XML::Node *repr; /**< The XML description of the Extension */