1/**************************************************************************/
2/* translation_po.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_po.h"
32
33#include "core/io/file_access.h"
34
35#ifdef DEBUG_TRANSLATION_PO
36void TranslationPO::print_translation_map() {
37 Error err;
38 Ref<FileAccess> file = FileAccess::open("translation_map_print_test.txt", FileAccess::WRITE, &err);
39 if (err != OK) {
40 ERR_PRINT("Failed to open translation_map_print_test.txt");
41 return;
42 }
43
44 file->store_line("NPlural : " + String::num_int64(this->get_plural_forms()));
45 file->store_line("Plural rule : " + this->get_plural_rule());
46 file->store_line("");
47
48 List<StringName> context_l;
49 translation_map.get_key_list(&context_l);
50 for (const StringName &ctx : context_l) {
51 file->store_line(" ===== Context: " + String::utf8(String(ctx).utf8()) + " ===== ");
52 const HashMap<StringName, Vector<StringName>> &inner_map = translation_map[ctx];
53
54 List<StringName> id_l;
55 inner_map.get_key_list(&id_l);
56 for (List<StringName>::Element *E2 = id_l.front(); E2; E2 = E2->next()) {
57 StringName id = E2->get();
58 file->store_line("msgid: " + String::utf8(String(id).utf8()));
59 for (int i = 0; i < inner_map[id].size(); i++) {
60 file->store_line("msgstr[" + String::num_int64(i) + "]: " + String::utf8(String(inner_map[id][i]).utf8()));
61 }
62 file->store_line("");
63 }
64 }
65}
66#endif
67
68Dictionary TranslationPO::_get_messages() const {
69 // Return translation_map as a Dictionary.
70
71 Dictionary d;
72
73 for (const KeyValue<StringName, HashMap<StringName, Vector<StringName>>> &E : translation_map) {
74 Dictionary d2;
75
76 for (const KeyValue<StringName, Vector<StringName>> &E2 : E.value) {
77 d2[E2.key] = E2.value;
78 }
79
80 d[E.key] = d2;
81 }
82
83 return d;
84}
85
86void TranslationPO::_set_messages(const Dictionary &p_messages) {
87 // Construct translation_map from a Dictionary.
88
89 List<Variant> context_l;
90 p_messages.get_key_list(&context_l);
91 for (const Variant &ctx : context_l) {
92 const Dictionary &id_str_map = p_messages[ctx];
93
94 HashMap<StringName, Vector<StringName>> temp_map;
95 List<Variant> id_l;
96 id_str_map.get_key_list(&id_l);
97 for (List<Variant>::Element *E2 = id_l.front(); E2; E2 = E2->next()) {
98 StringName id = E2->get();
99 temp_map[id] = id_str_map[id];
100 }
101
102 translation_map[ctx] = temp_map;
103 }
104}
105
106Vector<String> TranslationPO::get_translated_message_list() const {
107 Vector<String> msgs;
108 for (const KeyValue<StringName, HashMap<StringName, Vector<StringName>>> &E : translation_map) {
109 if (E.key != StringName()) {
110 continue;
111 }
112
113 for (const KeyValue<StringName, Vector<StringName>> &E2 : E.value) {
114 for (const StringName &E3 : E2.value) {
115 msgs.push_back(E3);
116 }
117 }
118 }
119
120 return msgs;
121}
122
123Vector<String> TranslationPO::_get_message_list() const {
124 // Return all keys in translation_map.
125
126 List<StringName> msgs;
127 get_message_list(&msgs);
128
129 Vector<String> v;
130 for (const StringName &E : msgs) {
131 v.push_back(E);
132 }
133
134 return v;
135}
136
137int TranslationPO::_get_plural_index(int p_n) const {
138 // Get a number between [0;number of plural forms).
139
140 input_val.clear();
141 input_val.push_back(p_n);
142
143 Variant result;
144 for (int i = 0; i < equi_tests.size(); i++) {
145 Error err = expr->parse(equi_tests[i], input_name);
146 ERR_FAIL_COND_V_MSG(err != OK, 0, "Cannot parse expression. Error: " + expr->get_error_text());
147
148 result = expr->execute(input_val);
149 ERR_FAIL_COND_V_MSG(expr->has_execute_failed(), 0, "Cannot evaluate expression.");
150
151 // Last expression. Variant result will either map to a bool or an integer, in both cases returning it will give the correct plural index.
152 if (i + 1 == equi_tests.size()) {
153 return result;
154 }
155
156 if (bool(result)) {
157 return i;
158 }
159 }
160
161 ERR_FAIL_V_MSG(0, "Unexpected. Function should have returned. Please report this bug.");
162}
163
164void TranslationPO::_cache_plural_tests(const String &p_plural_rule) {
165 // Some examples of p_plural_rule passed in can have the form:
166 // "n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5" (Arabic)
167 // "n >= 2" (French) // When evaluating the last, especially careful with this one.
168 // "n != 1" (English)
169 int first_ques_mark = p_plural_rule.find("?");
170 if (first_ques_mark == -1) {
171 equi_tests.push_back(p_plural_rule.strip_edges());
172 return;
173 }
174
175 String equi_test = p_plural_rule.substr(0, first_ques_mark).strip_edges();
176 equi_tests.push_back(equi_test);
177
178 String after_colon = p_plural_rule.substr(p_plural_rule.find(":") + 1, p_plural_rule.length());
179 _cache_plural_tests(after_colon);
180}
181
182void TranslationPO::set_plural_rule(const String &p_plural_rule) {
183 // Set plural_forms and plural_rule.
184 // p_plural_rule passed in has the form "Plural-Forms: nplurals=2; plural=(n >= 2);".
185
186 int first_semi_col = p_plural_rule.find(";");
187 plural_forms = p_plural_rule.substr(p_plural_rule.find("=") + 1, first_semi_col - (p_plural_rule.find("=") + 1)).to_int();
188
189 int expression_start = p_plural_rule.find("=", first_semi_col) + 1;
190 int second_semi_col = p_plural_rule.rfind(";");
191 plural_rule = p_plural_rule.substr(expression_start, second_semi_col - expression_start);
192
193 // Setup the cache to make evaluating plural rule faster later on.
194 plural_rule = plural_rule.replacen("(", "");
195 plural_rule = plural_rule.replacen(")", "");
196 _cache_plural_tests(plural_rule);
197 expr.instantiate();
198 input_name.push_back("n");
199}
200
201void TranslationPO::add_message(const StringName &p_src_text, const StringName &p_xlated_text, const StringName &p_context) {
202 HashMap<StringName, Vector<StringName>> &map_id_str = translation_map[p_context];
203
204 if (map_id_str.has(p_src_text)) {
205 WARN_PRINT("Double translations for \"" + String(p_src_text) + "\" under the same context \"" + String(p_context) + "\" for locale \"" + get_locale() + "\".\nThere should only be one unique translation for a given string under the same context.");
206 map_id_str[p_src_text].set(0, p_xlated_text);
207 } else {
208 map_id_str[p_src_text].push_back(p_xlated_text);
209 }
210}
211
212void TranslationPO::add_plural_message(const StringName &p_src_text, const Vector<String> &p_plural_xlated_texts, const StringName &p_context) {
213 ERR_FAIL_COND_MSG(p_plural_xlated_texts.size() != plural_forms, "Trying to add plural texts that don't match the required number of plural forms for locale \"" + get_locale() + "\"");
214
215 HashMap<StringName, Vector<StringName>> &map_id_str = translation_map[p_context];
216
217 if (map_id_str.has(p_src_text)) {
218 WARN_PRINT("Double translations for \"" + p_src_text + "\" under the same context \"" + p_context + "\" for locale " + get_locale() + ".\nThere should only be one unique translation for a given string under the same context.");
219 map_id_str[p_src_text].clear();
220 }
221
222 for (int i = 0; i < p_plural_xlated_texts.size(); i++) {
223 map_id_str[p_src_text].push_back(p_plural_xlated_texts[i]);
224 }
225}
226
227int TranslationPO::get_plural_forms() const {
228 return plural_forms;
229}
230
231String TranslationPO::get_plural_rule() const {
232 return plural_rule;
233}
234
235StringName TranslationPO::get_message(const StringName &p_src_text, const StringName &p_context) const {
236 if (!translation_map.has(p_context) || !translation_map[p_context].has(p_src_text)) {
237 return StringName();
238 }
239 ERR_FAIL_COND_V_MSG(translation_map[p_context][p_src_text].is_empty(), StringName(), "Source text \"" + String(p_src_text) + "\" is registered but doesn't have a translation. Please report this bug.");
240
241 return translation_map[p_context][p_src_text][0];
242}
243
244StringName TranslationPO::get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context) const {
245 ERR_FAIL_COND_V_MSG(p_n < 0, StringName(), "N passed into translation to get a plural message should not be negative. For negative numbers, use singular translation please. Search \"gettext PO Plural Forms\" online for the documentation on translating negative numbers.");
246
247 // If the query is the same as last time, return the cached result.
248 if (p_n == last_plural_n && p_context == last_plural_context && p_src_text == last_plural_key) {
249 return translation_map[p_context][p_src_text][last_plural_mapped_index];
250 }
251
252 if (!translation_map.has(p_context) || !translation_map[p_context].has(p_src_text)) {
253 return StringName();
254 }
255 ERR_FAIL_COND_V_MSG(translation_map[p_context][p_src_text].is_empty(), StringName(), "Source text \"" + String(p_src_text) + "\" is registered but doesn't have a translation. Please report this bug.");
256
257 int plural_index = _get_plural_index(p_n);
258 ERR_FAIL_COND_V_MSG(plural_index < 0 || translation_map[p_context][p_src_text].size() < plural_index + 1, StringName(), "Plural index returned or number of plural translations is not valid. Please report this bug.");
259
260 // Cache result so that if the next entry is the same, we can return directly.
261 // _get_plural_index(p_n) can get very costly, especially when evaluating long plural-rule (Arabic)
262 last_plural_key = p_src_text;
263 last_plural_context = p_context;
264 last_plural_n = p_n;
265 last_plural_mapped_index = plural_index;
266
267 return translation_map[p_context][p_src_text][plural_index];
268}
269
270void TranslationPO::erase_message(const StringName &p_src_text, const StringName &p_context) {
271 if (!translation_map.has(p_context)) {
272 return;
273 }
274
275 translation_map[p_context].erase(p_src_text);
276}
277
278void TranslationPO::get_message_list(List<StringName> *r_messages) const {
279 // OptimizedTranslation uses this function to get the list of msgid.
280 // Return all the keys of translation_map under "" context.
281
282 for (const KeyValue<StringName, HashMap<StringName, Vector<StringName>>> &E : translation_map) {
283 if (E.key != StringName()) {
284 continue;
285 }
286
287 for (const KeyValue<StringName, Vector<StringName>> &E2 : E.value) {
288 r_messages->push_back(E2.key);
289 }
290 }
291}
292
293int TranslationPO::get_message_count() const {
294 int count = 0;
295
296 for (const KeyValue<StringName, HashMap<StringName, Vector<StringName>>> &E : translation_map) {
297 count += E.value.size();
298 }
299
300 return count;
301}
302
303void TranslationPO::_bind_methods() {
304 ClassDB::bind_method(D_METHOD("get_plural_forms"), &TranslationPO::get_plural_forms);
305 ClassDB::bind_method(D_METHOD("get_plural_rule"), &TranslationPO::get_plural_rule);
306}
307