From ba4f86720ab77c3e38e6fa171999f361ed44b4da Mon Sep 17 00:00:00 2001 From: Damien Picard Date: Thu, 23 Feb 2023 22:00:57 +0100 Subject: [PATCH 1/3] I18n: add per-socket translation contexts for nodes In order to properly translate UI messages, they sometimes need to be disambiguated using translation contexts. Until now, node sockets had no way to specify contexts and collisions occurred. This commit adds a way to declare contexts for each socket using: .translation_context() If no context is specified, the default null context is used. --- .../realtime_compositor/CMakeLists.txt | 1 + .../blender/editors/space_node/node_draw.cc | 35 +++++++++++++++++-- source/blender/nodes/NOD_node_declaration.hh | 9 +++++ .../nodes/node_composite_keyingscreen.cc | 3 +- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/source/blender/compositor/realtime_compositor/CMakeLists.txt b/source/blender/compositor/realtime_compositor/CMakeLists.txt index 38e5c6be88c..ab87e84dc25 100644 --- a/source/blender/compositor/realtime_compositor/CMakeLists.txt +++ b/source/blender/compositor/realtime_compositor/CMakeLists.txt @@ -6,6 +6,7 @@ set(INC cached_resources ../../blenkernel ../../blenlib + ../../blentranslation ../../gpu ../../imbuf ../../makesdna diff --git a/source/blender/editors/space_node/node_draw.cc b/source/blender/editors/space_node/node_draw.cc index 31dc2695c97..d643457762c 100644 --- a/source/blender/editors/space_node/node_draw.cc +++ b/source/blender/editors/space_node/node_draw.cc @@ -181,6 +181,26 @@ void ED_node_tag_update_id(ID *id) namespace blender::ed::space_node { +static const char *node_socket_get_translation_context(const bNodeSocket &socket) +{ + std::stringstream output; + const nodes::SocketDeclaration &socket_decl = *socket.runtime->declaration; + + /* The node is not explicitly defined. */ + if (&socket_decl == nullptr) { + return nullptr; + } + + /* Default context. */ + blender::StringRef translation_context = socket_decl.translation_context; + if (translation_context.is_empty()) { + return nullptr; + } + + output << translation_context.data(); + return BLI_strdup(output.str().c_str()); +} + static void node_socket_add_tooltip_in_node_editor(const bNodeTree &ntree, const bNodeSocket &sock, uiLayout &layout); @@ -377,8 +397,14 @@ static void node_update_basis(const bContext &C, /* Align output buttons to the right. */ uiLayout *row = uiLayoutRow(layout, true); uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_RIGHT); + const char *socket_label = nodeSocketLabel(socket); - socket->typeinfo->draw((bContext *)&C, row, &sockptr, &nodeptr, IFACE_(socket_label)); + const char *socket_translation_context = node_socket_get_translation_context(*socket); + socket->typeinfo->draw((bContext *)&C, + row, + &sockptr, + &nodeptr, + CTX_IFACE_(socket_translation_context, socket_label)); node_socket_add_tooltip_in_node_editor(ntree, *socket, *row); @@ -511,7 +537,12 @@ static void node_update_basis(const bContext &C, uiLayout *row = uiLayoutRow(layout, true); const char *socket_label = nodeSocketLabel(socket); - socket->typeinfo->draw((bContext *)&C, row, &sockptr, &nodeptr, IFACE_(socket_label)); + const char *socket_translation_context = node_socket_get_translation_context(*socket); + socket->typeinfo->draw((bContext *)&C, + row, + &sockptr, + &nodeptr, + CTX_IFACE_(socket_translation_context, socket_label)); node_socket_add_tooltip_in_node_editor(ntree, *socket, *row); diff --git a/source/blender/nodes/NOD_node_declaration.hh b/source/blender/nodes/NOD_node_declaration.hh index e58c4138f40..e9eff6bf625 100644 --- a/source/blender/nodes/NOD_node_declaration.hh +++ b/source/blender/nodes/NOD_node_declaration.hh @@ -8,6 +8,8 @@ #include "BLI_string_ref.hh" #include "BLI_vector.hh" +#include "BLT_translation.h" + #include "DNA_node_types.h" struct bNode; @@ -145,6 +147,7 @@ class SocketDeclaration { std::string name; std::string identifier; std::string description; + std::string translation_context; /** Defined by whether the socket is part of the node's input or * output socket declaration list. Included here for convenience. */ eNodeSocketInOut in_out; @@ -275,6 +278,12 @@ class SocketDeclarationBuilder : public BaseSocketDeclarationBuilder { return *(Self *)this; } + Self &translation_context(std::string value = BLT_I18NCONTEXT_DEFAULT) + { + decl_->translation_context = std::move(value); + return *(Self *)this; + } + Self &no_muted_links(bool value = true) { decl_->no_mute_links = value; diff --git a/source/blender/nodes/composite/nodes/node_composite_keyingscreen.cc b/source/blender/nodes/composite/nodes/node_composite_keyingscreen.cc index 82a85a151f5..d56ab985cfd 100644 --- a/source/blender/nodes/composite/nodes/node_composite_keyingscreen.cc +++ b/source/blender/nodes/composite/nodes/node_composite_keyingscreen.cc @@ -33,7 +33,8 @@ namespace blender::nodes::node_composite_keyingscreen_cc { static void cmp_node_keyingscreen_declare(NodeDeclarationBuilder &b) { - b.add_output(N_("Screen")); + b.add_output(CTX_N_(BLT_I18NCONTEXT_ID_SCREEN, "Screen")) + .translation_context(BLT_I18NCONTEXT_ID_SCREEN); } static void node_composit_init_keyingscreen(const bContext *C, PointerRNA *ptr) -- 2.30.2 From ddc864b09c13696550f05d6ab2eaea073c80e200 Mon Sep 17 00:00:00 2001 From: Damien Picard Date: Mon, 27 Feb 2023 16:00:22 +0100 Subject: [PATCH 2/3] Address review for #105195 - Check for node runtime declaration, return early if null pointer. - Return translation_context.data() directly instead of using a temporary std:stringstream. --- source/blender/editors/space_node/node_draw.cc | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/source/blender/editors/space_node/node_draw.cc b/source/blender/editors/space_node/node_draw.cc index d643457762c..fa59c522f00 100644 --- a/source/blender/editors/space_node/node_draw.cc +++ b/source/blender/editors/space_node/node_draw.cc @@ -183,22 +183,19 @@ namespace blender::ed::space_node { static const char *node_socket_get_translation_context(const bNodeSocket &socket) { - std::stringstream output; - const nodes::SocketDeclaration &socket_decl = *socket.runtime->declaration; - /* The node is not explicitly defined. */ - if (&socket_decl == nullptr) { + if (socket.runtime->declaration == nullptr) { return nullptr; } + blender::StringRef translation_context = socket.runtime->declaration->translation_context; + /* Default context. */ - blender::StringRef translation_context = socket_decl.translation_context; if (translation_context.is_empty()) { return nullptr; } - output << translation_context.data(); - return BLI_strdup(output.str().c_str()); + return BLI_strdup(translation_context.data()); } static void node_socket_add_tooltip_in_node_editor(const bNodeTree &ntree, -- 2.30.2 From 22d36554b694060bab8ab479a7ae230c345bdb16 Mon Sep 17 00:00:00 2001 From: Damien Picard Date: Tue, 28 Feb 2023 19:54:24 +0100 Subject: [PATCH 3/3] Address review for #105195 Use a `StringRefNull` to store the translation context instead of passing it through `BLI_strdup()`. --- source/blender/editors/space_node/node_draw.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/blender/editors/space_node/node_draw.cc b/source/blender/editors/space_node/node_draw.cc index fa59c522f00..7ddd5bc32a9 100644 --- a/source/blender/editors/space_node/node_draw.cc +++ b/source/blender/editors/space_node/node_draw.cc @@ -188,14 +188,14 @@ static const char *node_socket_get_translation_context(const bNodeSocket &socket return nullptr; } - blender::StringRef translation_context = socket.runtime->declaration->translation_context; + blender::StringRefNull translation_context = socket.runtime->declaration->translation_context; /* Default context. */ if (translation_context.is_empty()) { return nullptr; } - return BLI_strdup(translation_context.data()); + return translation_context.data(); } static void node_socket_add_tooltip_in_node_editor(const bNodeTree &ntree, -- 2.30.2