From 538d053eedc5eba786926f49d2686478f68a2ac7 Mon Sep 17 00:00:00 2001 From: Damien Picard Date: Sun, 12 Mar 2023 13:40:02 +0100 Subject: [PATCH 1/3] I18n: add per-label translation contexts for nodes In order to properly translate UI messages, they sometimes need to be disambiguated using translation contexts. Until now, node sockets using custom labels had no way to specify contexts and a collision occurred in at least one known instance. This commit adds a way to declare contexts for each socket with labels, by adding a `label_translation_context` field to the bNodeSocket struct, as well as a `translation_context` argument to the `node_sock_label()` function. If no context is specified, the default null context is used. The use of a context will enable us to fix part of #105113 in the next commit. --- source/blender/blenkernel/BKE_node_runtime.hh | 1 + source/blender/editors/space_node/node_draw.cc | 5 +++++ source/blender/nodes/intern/node_util.cc | 8 +++++++- source/blender/nodes/intern/node_util.hh | 6 +++++- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/source/blender/blenkernel/BKE_node_runtime.hh b/source/blender/blenkernel/BKE_node_runtime.hh index afebe5c63ec..276727edebe 100644 --- a/source/blender/blenkernel/BKE_node_runtime.hh +++ b/source/blender/blenkernel/BKE_node_runtime.hh @@ -201,6 +201,7 @@ class bNodeSocketRuntime : NonCopyable, NonMovable { int index_in_node = -1; int index_in_all_sockets = -1; int index_in_inout_sockets = -1; + std::string label_translation_context; }; /** diff --git a/source/blender/editors/space_node/node_draw.cc b/source/blender/editors/space_node/node_draw.cc index 963972c60af..6246b70a19a 100644 --- a/source/blender/editors/space_node/node_draw.cc +++ b/source/blender/editors/space_node/node_draw.cc @@ -191,6 +191,11 @@ namespace blender::ed::space_node { static const char *node_socket_get_translation_context(const bNodeSocket &socket) { + /* Get the context from the label if it is defined. */ + if (socket.runtime->label_translation_context[0] != '\0') { + return socket.runtime->label_translation_context.c_str(); + } + /* The node is not explicitly defined. */ if (socket.runtime->declaration == nullptr) { return nullptr; diff --git a/source/blender/nodes/intern/node_util.cc b/source/blender/nodes/intern/node_util.cc index 89b0e8e45bd..9ee4f2b62ce 100644 --- a/source/blender/nodes/intern/node_util.cc +++ b/source/blender/nodes/intern/node_util.cc @@ -73,9 +73,12 @@ void *node_initexec_curves(bNodeExecContext * /*context*/, bNode *node, bNodeIns /** \name Updates * \{ */ -void node_sock_label(bNodeSocket *sock, const char *name) +void node_sock_label(bNodeSocket *sock, const char *name, const char *translation_context) { STRNCPY(sock->label, name); + if (translation_context) { + sock->runtime->label_translation_context = translation_context; + } } void node_sock_label_clear(bNodeSocket *sock) @@ -83,6 +86,9 @@ void node_sock_label_clear(bNodeSocket *sock) if (sock->label[0] != '\0') { sock->label[0] = '\0'; } + if (sock->runtime->label_translation_context[0] != '\0') { + sock->runtime->label_translation_context[0] = '\0'; + } } void node_math_update(bNodeTree *ntree, bNode *node) diff --git a/source/blender/nodes/intern/node_util.hh b/source/blender/nodes/intern/node_util.hh index 735668187b7..fe920a94336 100644 --- a/source/blender/nodes/intern/node_util.hh +++ b/source/blender/nodes/intern/node_util.hh @@ -7,6 +7,8 @@ #pragma once +#include "BLT_translation.h" + struct bNode; struct bNodeTree; @@ -30,7 +32,9 @@ void node_copy_standard_storage(bNodeTree *dest_ntree, bNode *dest_node, const b void *node_initexec_curves(bNodeExecContext *context, bNode *node, bNodeInstanceKey key); /**** Updates ****/ -void node_sock_label(bNodeSocket *sock, const char *name); +void node_sock_label(bNodeSocket *sock, + const char *name, + const char *translation_context = BLT_I18NCONTEXT_DEFAULT); void node_sock_label_clear(bNodeSocket *sock); void node_math_update(bNodeTree *ntree, bNode *node); -- 2.30.2 From 22ddff63e3a32eff40a895aaa02aea9780d47538 Mon Sep 17 00:00:00 2001 From: Damien Picard Date: Sun, 12 Mar 2023 13:45:21 +0100 Subject: [PATCH 2/3] I18n: add translation contexts to Value socket for Color Combine node This socket needs disambiguation, because it cannot be properly translated to Japanese. "Value", in the context of color, is not the same word as in the context of a numerical value, most common elsewhere in Blender. More details in #105113. --- .../nodes/composite/nodes/node_composite_sepcomb_color.cc | 4 +++- source/blender/nodes/intern/node_util.cc | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/source/blender/nodes/composite/nodes/node_composite_sepcomb_color.cc b/source/blender/nodes/composite/nodes/node_composite_sepcomb_color.cc index 20a28b590d4..5fb70a0bc72 100644 --- a/source/blender/nodes/composite/nodes/node_composite_sepcomb_color.cc +++ b/source/blender/nodes/composite/nodes/node_composite_sepcomb_color.cc @@ -2,6 +2,8 @@ #include "BLI_assert.h" +#include "BLT_translation.h" + #include "GPU_material.h" #include "COM_shader_node.hh" @@ -35,7 +37,7 @@ static void node_cmp_combsep_color_label(const ListBase *sockets, CMPNodeCombSep case CMP_NODE_COMBSEP_COLOR_HSV: node_sock_label(sock1, "Hue"); node_sock_label(sock2, "Saturation"); - node_sock_label(sock3, "Value"); + node_sock_label(sock3, "Value", BLT_I18NCONTEXT_COLOR); break; case CMP_NODE_COMBSEP_COLOR_HSL: node_sock_label(sock1, "Hue"); diff --git a/source/blender/nodes/intern/node_util.cc b/source/blender/nodes/intern/node_util.cc index 9ee4f2b62ce..ade814293d6 100644 --- a/source/blender/nodes/intern/node_util.cc +++ b/source/blender/nodes/intern/node_util.cc @@ -268,7 +268,7 @@ void node_combsep_color_label(const ListBase *sockets, NodeCombSepColorMode mode case NODE_COMBSEP_COLOR_HSV: node_sock_label(sock1, "Hue"); node_sock_label(sock2, "Saturation"); - node_sock_label(sock3, "Value"); + node_sock_label(sock3, "Value", BLT_I18NCONTEXT_COLOR); break; default: { BLI_assert_unreachable(); -- 2.30.2 From 29830181ca49541f49921e52b1d7325604f627af Mon Sep 17 00:00:00 2001 From: Damien Picard Date: Sun, 12 Mar 2023 15:44:19 +0100 Subject: [PATCH 3/3] I18n: extract messages from node sockets using custom labels Node sockets can get their names from several places: from the node declaration, or from special labels, mostly used in math nodes. These labels are now translated, but they never were extracted, so this commit adds two regex variants to extract them from the `node_socket_label()` function, with or without a translation context. Three messages previously untranslatable were added, from the math node operator types. --- scripts/modules/bl_i18n_utils/settings.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/modules/bl_i18n_utils/settings.py b/scripts/modules/bl_i18n_utils/settings.py index 2733c2e7fbd..d487c370b53 100644 --- a/scripts/modules/bl_i18n_utils/settings.py +++ b/scripts/modules/bl_i18n_utils/settings.py @@ -251,6 +251,14 @@ PYGETTEXT_KEYWORDS = (() + tuple(("{}\\((?:[^\"',]+,){{2}}\\s*" + _msg_re + r"\s*(?:\)|,)").format(it) for it in ("BKE_modifier_set_error",)) + + # Extract messages specific to node sockets declaring labels. + tuple(("{}\\((?:[^\"',]+,)\\s*" + _msg_re + r"\s*\)").format(it) + for it in ("node_sock_label",)) + + + # Same as above, but variant with a context. + tuple(("{}\\((?:[^\"',]+,)\\s*" + _msg_re + r"\s*,\s*" + _ctxt_re + r"\s*\)").format(it) + for it in ("node_sock_label",)) + + # This one is a tad more risky, but in practice would not expect a name/uid string parameter # (the second one in those functions) to ever have a comma in it, so think this is fine. tuple(("{}\\((?:[^,]+,){{2}}\\s*" + _msg_re + r"\s*(?:\)|,)").format(it) -- 2.30.2