From 1a0a883570224452ea5c412c75b4a373e02e973f Mon Sep 17 00:00:00 2001 From: Luca Bacci Date: Fri, 28 Mar 2025 14:04:47 +0100 Subject: [PATCH 1/2] Extension: Cache string translations --- src/extension/extension.cpp | 21 +++++++++++++++++++-- src/extension/extension.h | 6 ++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/extension/extension.cpp b/src/extension/extension.cpp index 642d39d6ae..997b25ee06 100644 --- a/src/extension/extension.cpp +++ b/src/extension/extension.cpp @@ -505,10 +505,27 @@ 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 + // https://devblogs.microsoft.com/oldnewthing/20190227-00/?p=101072 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 754b779622..f3ed66c6b7 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 */ -- GitLab From 604b71fb9844817fb0cb4e7d1b337a49814ce4f7 Mon Sep 17 00:00:00 2001 From: Luca Bacci Date: Fri, 28 Mar 2025 14:09:18 +0100 Subject: [PATCH 2/2] Extension: Translate string only when used --- src/extension/extension.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/extension/extension.cpp b/src/extension/extension.cpp index 997b25ee06..c9e8cbe3d4 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; } @@ -507,7 +509,6 @@ char const *Extension::get_translation(char const *msgid, char const *msgctxt) c // Possible enhancement: heterogeneous lookup // https://stackoverflow.com/questions/49709548/c-unordered-mapstring-lookup-without-constructing-string - // https://devblogs.microsoft.com/oldnewthing/20190227-00/?p=101072 if (msgctxt) { auto& map = _translations_by_context[msgctxt]; -- GitLab