Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/string/translation.cpp
9903 views
1
/**************************************************************************/
2
/* translation.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "translation.h"
32
33
#include "core/os/thread.h"
34
#include "core/string/translation_server.h"
35
36
Dictionary Translation::_get_messages() const {
37
Dictionary d;
38
for (const KeyValue<StringName, StringName> &E : translation_map) {
39
d[E.key] = E.value;
40
}
41
return d;
42
}
43
44
Vector<String> Translation::_get_message_list() const {
45
Vector<String> msgs;
46
msgs.resize(translation_map.size());
47
int idx = 0;
48
for (const KeyValue<StringName, StringName> &E : translation_map) {
49
msgs.set(idx, E.key);
50
idx += 1;
51
}
52
53
return msgs;
54
}
55
56
Vector<String> Translation::get_translated_message_list() const {
57
Vector<String> msgs;
58
msgs.resize(translation_map.size());
59
int idx = 0;
60
for (const KeyValue<StringName, StringName> &E : translation_map) {
61
msgs.set(idx, E.value);
62
idx += 1;
63
}
64
65
return msgs;
66
}
67
68
void Translation::_set_messages(const Dictionary &p_messages) {
69
for (const KeyValue<Variant, Variant> &kv : p_messages) {
70
translation_map[kv.key] = kv.value;
71
}
72
}
73
74
void Translation::set_locale(const String &p_locale) {
75
locale = TranslationServer::get_singleton()->standardize_locale(p_locale);
76
}
77
78
void Translation::add_message(const StringName &p_src_text, const StringName &p_xlated_text, const StringName &p_context) {
79
translation_map[p_src_text] = p_xlated_text;
80
}
81
82
void Translation::add_plural_message(const StringName &p_src_text, const Vector<String> &p_plural_xlated_texts, const StringName &p_context) {
83
WARN_PRINT("Translation class doesn't handle plural messages. Calling add_plural_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles plurals, such as TranslationPO class");
84
ERR_FAIL_COND_MSG(p_plural_xlated_texts.is_empty(), "Parameter vector p_plural_xlated_texts passed in is empty.");
85
translation_map[p_src_text] = p_plural_xlated_texts[0];
86
}
87
88
StringName Translation::get_message(const StringName &p_src_text, const StringName &p_context) const {
89
StringName ret;
90
if (GDVIRTUAL_CALL(_get_message, p_src_text, p_context, ret)) {
91
return ret;
92
}
93
94
if (p_context != StringName()) {
95
WARN_PRINT("Translation class doesn't handle context. Using context in get_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles context, such as TranslationPO class");
96
}
97
98
HashMap<StringName, StringName>::ConstIterator E = translation_map.find(p_src_text);
99
if (!E) {
100
return StringName();
101
}
102
103
return E->value;
104
}
105
106
StringName Translation::get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context) const {
107
StringName ret;
108
if (GDVIRTUAL_CALL(_get_plural_message, p_src_text, p_plural_text, p_n, p_context, ret)) {
109
return ret;
110
}
111
112
WARN_PRINT("Translation class doesn't handle plural messages. Calling get_plural_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles plurals, such as TranslationPO class");
113
return get_message(p_src_text);
114
}
115
116
void Translation::erase_message(const StringName &p_src_text, const StringName &p_context) {
117
if (p_context != StringName()) {
118
WARN_PRINT("Translation class doesn't handle context. Using context in erase_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles context, such as TranslationPO class");
119
}
120
121
translation_map.erase(p_src_text);
122
}
123
124
void Translation::get_message_list(List<StringName> *r_messages) const {
125
for (const KeyValue<StringName, StringName> &E : translation_map) {
126
r_messages->push_back(E.key);
127
}
128
}
129
130
int Translation::get_message_count() const {
131
return translation_map.size();
132
}
133
134
void Translation::_bind_methods() {
135
ClassDB::bind_method(D_METHOD("set_locale", "locale"), &Translation::set_locale);
136
ClassDB::bind_method(D_METHOD("get_locale"), &Translation::get_locale);
137
ClassDB::bind_method(D_METHOD("add_message", "src_message", "xlated_message", "context"), &Translation::add_message, DEFVAL(StringName()));
138
ClassDB::bind_method(D_METHOD("add_plural_message", "src_message", "xlated_messages", "context"), &Translation::add_plural_message, DEFVAL(StringName()));
139
ClassDB::bind_method(D_METHOD("get_message", "src_message", "context"), &Translation::get_message, DEFVAL(StringName()));
140
ClassDB::bind_method(D_METHOD("get_plural_message", "src_message", "src_plural_message", "n", "context"), &Translation::get_plural_message, DEFVAL(StringName()));
141
ClassDB::bind_method(D_METHOD("erase_message", "src_message", "context"), &Translation::erase_message, DEFVAL(StringName()));
142
ClassDB::bind_method(D_METHOD("get_message_list"), &Translation::_get_message_list);
143
ClassDB::bind_method(D_METHOD("get_translated_message_list"), &Translation::get_translated_message_list);
144
ClassDB::bind_method(D_METHOD("get_message_count"), &Translation::get_message_count);
145
ClassDB::bind_method(D_METHOD("_set_messages", "messages"), &Translation::_set_messages);
146
ClassDB::bind_method(D_METHOD("_get_messages"), &Translation::_get_messages);
147
148
GDVIRTUAL_BIND(_get_plural_message, "src_message", "src_plural_message", "n", "context");
149
GDVIRTUAL_BIND(_get_message, "src_message", "context");
150
151
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "messages", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_messages", "_get_messages");
152
ADD_PROPERTY(PropertyInfo(Variant::STRING, "locale"), "set_locale", "get_locale");
153
}
154
155