From 4dc59c731145af5315966a853899ee111bdd0430 Mon Sep 17 00:00:00 2001 From: Damien Picard Date: Mon, 6 Mar 2023 14:24:36 +0100 Subject: [PATCH] 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. Pull Request #105195 --- .../realtime_compositor/CMakeLists.txt | 1 + .../blender/editors/space_node/node_draw.cc | 32 +++++++++++++++++-- source/blender/nodes/NOD_node_declaration.hh | 9 ++++++ .../nodes/node_composite_keyingscreen.cc | 3 +- 4 files changed, 42 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 b25f97c9c54..a9db1fa50bd 100644 --- a/source/blender/editors/space_node/node_draw.cc +++ b/source/blender/editors/space_node/node_draw.cc @@ -181,6 +181,23 @@ void ED_node_tag_update_id(ID *id) namespace blender::ed::space_node { +static const char *node_socket_get_translation_context(const bNodeSocket &socket) +{ + /* The node is not explicitly defined. */ + if (socket.runtime->declaration == nullptr) { + return nullptr; + } + + blender::StringRefNull translation_context = socket.runtime->declaration->translation_context; + + /* Default context. */ + if (translation_context.is_empty()) { + return nullptr; + } + + return translation_context.data(); +} + static void node_socket_add_tooltip_in_node_editor(const bNodeTree &ntree, const bNodeSocket &sock, uiLayout &layout); @@ -380,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); @@ -514,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)