Skip to content

Commit c5d147b

Browse files
committed
Allow configuring which translation domain Object.tr uses
1 parent 68d494e commit c5d147b

File tree

6 files changed

+94
-2
lines changed

6 files changed

+94
-2
lines changed

core/object/object.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,14 @@ void Object::initialize_class() {
15271527
initialized = true;
15281528
}
15291529

1530+
StringName Object::get_translation_domain() const {
1531+
return _translation_domain;
1532+
}
1533+
1534+
void Object::set_translation_domain(const StringName &p_domain) {
1535+
_translation_domain = p_domain;
1536+
}
1537+
15301538
String Object::tr(const StringName &p_message, const StringName &p_context) const {
15311539
if (!_can_translate || !TranslationServer::get_singleton()) {
15321540
return p_message;
@@ -1541,7 +1549,8 @@ String Object::tr(const StringName &p_message, const StringName &p_context) cons
15411549
return TranslationServer::get_singleton()->tool_translate(p_message, p_context);
15421550
}
15431551

1544-
return TranslationServer::get_singleton()->translate(p_message, p_context);
1552+
const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain(get_translation_domain());
1553+
return domain->translate(p_message, p_context);
15451554
}
15461555

15471556
String Object::tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
@@ -1562,7 +1571,8 @@ String Object::tr_n(const StringName &p_message, const StringName &p_message_plu
15621571
return TranslationServer::get_singleton()->tool_translate_plural(p_message, p_message_plural, p_n, p_context);
15631572
}
15641573

1565-
return TranslationServer::get_singleton()->translate_plural(p_message, p_message_plural, p_n, p_context);
1574+
const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain(get_translation_domain());
1575+
return domain->translate_plural(p_message, p_message_plural, p_n, p_context);
15661576
}
15671577

15681578
void Object::_clear_internal_resource_paths(const Variant &p_var) {
@@ -1714,6 +1724,8 @@ void Object::_bind_methods() {
17141724
ClassDB::bind_method(D_METHOD("can_translate_messages"), &Object::can_translate_messages);
17151725
ClassDB::bind_method(D_METHOD("tr", "message", "context"), &Object::tr, DEFVAL(StringName()));
17161726
ClassDB::bind_method(D_METHOD("tr_n", "message", "plural_message", "n", "context"), &Object::tr_n, DEFVAL(StringName()));
1727+
ClassDB::bind_method(D_METHOD("get_translation_domain"), &Object::get_translation_domain);
1728+
ClassDB::bind_method(D_METHOD("set_translation_domain", "domain"), &Object::set_translation_domain);
17171729

17181730
ClassDB::bind_method(D_METHOD("is_queued_for_deletion"), &Object::is_queued_for_deletion);
17191731
ClassDB::bind_method(D_METHOD("cancel_free"), &Object::cancel_free);

core/object/object.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,8 @@ class Object {
665665
Object(bool p_reference);
666666

667667
protected:
668+
StringName _translation_domain;
669+
668670
_FORCE_INLINE_ bool _instance_binding_reference(bool p_reference) {
669671
bool can_die = true;
670672
if (_instance_bindings) {
@@ -954,6 +956,9 @@ class Object {
954956
_FORCE_INLINE_ void set_message_translation(bool p_enable) { _can_translate = p_enable; }
955957
_FORCE_INLINE_ bool can_translate_messages() const { return _can_translate; }
956958

959+
virtual StringName get_translation_domain() const;
960+
virtual void set_translation_domain(const StringName &p_domain);
961+
957962
#ifdef TOOLS_ENABLED
958963
virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
959964
void editor_set_section_unfold(const String &p_section, bool p_unfolded);

doc/classes/Node.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,13 @@
973973
Similar to [method call_thread_safe], but for setting properties.
974974
</description>
975975
</method>
976+
<method name="set_translation_domain_inherited">
977+
<return type="void" />
978+
<description>
979+
Makes this node inherit the translation domain from its parent node. If this node has no parent, the main translation domain will be used.
980+
This is the default behavior for all nodes. Calling [method Object.set_translation_domain] disables this behavior.
981+
</description>
982+
</method>
976983
<method name="update_configuration_warnings">
977984
<return type="void" />
978985
<description>

doc/classes/Object.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,12 @@
818818
[b]Note:[/b] Due of the implementation, each [Dictionary] is formatted very similarly to the returned values of [method get_method_list].
819819
</description>
820820
</method>
821+
<method name="get_translation_domain" qualifiers="const">
822+
<return type="StringName" />
823+
<description>
824+
Returns the name of the translation domain used by [method tr] and [method tr_n]. See also [TranslationServer].
825+
</description>
826+
</method>
821827
<method name="has_meta" qualifiers="const">
822828
<return type="bool" />
823829
<param index="0" name="name" type="StringName" />
@@ -1070,6 +1076,13 @@
10701076
If a script already exists, its instance is detached, and its property values and state are lost. Built-in property values are still kept.
10711077
</description>
10721078
</method>
1079+
<method name="set_translation_domain">
1080+
<return type="void" />
1081+
<param index="0" name="domain" type="StringName" />
1082+
<description>
1083+
Sets the name of the translation domain used by [method tr] and [method tr_n]. See also [TranslationServer].
1084+
</description>
1085+
</method>
10731086
<method name="to_string">
10741087
<return type="String" />
10751088
<description>

scene/main/node.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ void Node::_notification(int p_notification) {
111111
data.auto_translate_mode = AUTO_TRANSLATE_MODE_ALWAYS;
112112
}
113113
data.is_auto_translate_dirty = true;
114+
data.is_translation_domain_dirty = true;
114115

115116
#ifdef TOOLS_ENABLED
116117
// Don't translate UI elements when they're being edited.
@@ -1320,6 +1321,51 @@ bool Node::can_auto_translate() const {
13201321
return data.is_auto_translating;
13211322
}
13221323

1324+
StringName Node::get_translation_domain() const {
1325+
ERR_READ_THREAD_GUARD_V(StringName());
1326+
1327+
if (data.is_translation_domain_inherited && data.is_translation_domain_dirty) {
1328+
const_cast<Node *>(this)->_translation_domain = data.parent ? data.parent->get_translation_domain() : StringName();
1329+
data.is_translation_domain_dirty = false;
1330+
}
1331+
return _translation_domain;
1332+
}
1333+
1334+
void Node::set_translation_domain(const StringName &p_domain) {
1335+
ERR_THREAD_GUARD
1336+
1337+
if (!data.is_translation_domain_inherited && _translation_domain == p_domain) {
1338+
return;
1339+
}
1340+
1341+
_translation_domain = p_domain;
1342+
data.is_translation_domain_inherited = false;
1343+
data.is_translation_domain_dirty = false;
1344+
_propagate_translation_domain_dirty();
1345+
}
1346+
1347+
void Node::set_translation_domain_inherited() {
1348+
ERR_THREAD_GUARD
1349+
1350+
if (data.is_translation_domain_inherited) {
1351+
return;
1352+
}
1353+
data.is_translation_domain_inherited = true;
1354+
data.is_translation_domain_dirty = true;
1355+
_propagate_translation_domain_dirty();
1356+
}
1357+
1358+
void Node::_propagate_translation_domain_dirty() {
1359+
for (KeyValue<StringName, Node *> &K : data.children) {
1360+
Node *child = K.value;
1361+
if (child->data.is_translation_domain_inherited) {
1362+
child->data.is_translation_domain_dirty = true;
1363+
child->_propagate_translation_domain_dirty();
1364+
}
1365+
}
1366+
notification(NOTIFICATION_TRANSLATION_CHANGED);
1367+
}
1368+
13231369
StringName Node::get_name() const {
13241370
return data.name;
13251371
}
@@ -3610,6 +3656,7 @@ void Node::_bind_methods() {
36103656

36113657
ClassDB::bind_method(D_METHOD("set_auto_translate_mode", "mode"), &Node::set_auto_translate_mode);
36123658
ClassDB::bind_method(D_METHOD("get_auto_translate_mode"), &Node::get_auto_translate_mode);
3659+
ClassDB::bind_method(D_METHOD("set_translation_domain_inherited"), &Node::set_translation_domain_inherited);
36133660

36143661
ClassDB::bind_method(D_METHOD("get_window"), &Node::get_window);
36153662
ClassDB::bind_method(D_METHOD("get_last_exclusive_window"), &Node::get_last_exclusive_window);

scene/main/node.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ class Node : public Object {
255255
mutable bool is_auto_translating = true;
256256
mutable bool is_auto_translate_dirty = true;
257257

258+
mutable bool is_translation_domain_inherited = true;
259+
mutable bool is_translation_domain_dirty = true;
260+
258261
mutable NodePath *path_cache = nullptr;
259262

260263
} data;
@@ -281,6 +284,7 @@ class Node : public Object {
281284
void _propagate_physics_interpolation_reset_requested(bool p_requested);
282285
void _propagate_process_owner(Node *p_owner, int p_pause_notification, int p_enabled_notification);
283286
void _propagate_groups_dirty();
287+
void _propagate_translation_domain_dirty();
284288
Array _get_node_and_resource(const NodePath &p_path);
285289

286290
void _duplicate_properties(const Node *p_root, const Node *p_original, Node *p_copy, int p_flags) const;
@@ -735,6 +739,10 @@ class Node : public Object {
735739
AutoTranslateMode get_auto_translate_mode() const;
736740
bool can_auto_translate() const;
737741

742+
virtual StringName get_translation_domain() const override;
743+
virtual void set_translation_domain(const StringName &p_domain) override;
744+
void set_translation_domain_inherited();
745+
738746
_FORCE_INLINE_ String atr(const String p_message, const StringName p_context = "") const { return can_auto_translate() ? tr(p_message, p_context) : p_message; }
739747
_FORCE_INLINE_ String atr_n(const String p_message, const StringName &p_message_plural, int p_n, const StringName p_context = "") const { return can_auto_translate() ? tr_n(p_message, p_message_plural, p_n, p_context) : p_message; }
740748

0 commit comments

Comments
 (0)