1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/*************************************************************************/
/* editor_translation_parser.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "editor_translation_parser.h"
#include "core/error/error_macros.h"
#include "core/io/file_access.h"
#include "core/object/script_language.h"
#include "core/templates/rb_set.h"
EditorTranslationParser *EditorTranslationParser::singleton = nullptr;
Error EditorTranslationParserPlugin::parse_file(const String &p_path, Vector<String> *r_ids, Vector<Vector<String>> *r_ids_ctx_plural) {
Array ids;
Array ids_ctx_plural;
if (GDVIRTUAL_CALL(_parse_file, p_path, ids, ids_ctx_plural)) {
// Add user's extracted translatable messages.
for (int i = 0; i < ids.size(); i++) {
r_ids->append(ids[i]);
}
// Add user's collected translatable messages with context or plurals.
for (int i = 0; i < ids_ctx_plural.size(); i++) {
Array arr = ids_ctx_plural[i];
ERR_FAIL_COND_V_MSG(arr.size() != 3, ERR_INVALID_DATA, "Array entries written into `msgids_context_plural` in `parse_file()` method should have the form [\"message\", \"context\", \"plural message\"]");
Vector<String> id_ctx_plural;
id_ctx_plural.push_back(arr[0]);
id_ctx_plural.push_back(arr[1]);
id_ctx_plural.push_back(arr[2]);
r_ids_ctx_plural->append(id_ctx_plural);
}
return OK;
} else {
ERR_PRINT("Custom translation parser plugin's \"func parse_file(path, extracted_strings)\" is undefined.");
return ERR_UNAVAILABLE;
}
}
void EditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const {
Vector<String> extensions;
if (GDVIRTUAL_CALL(_get_recognized_extensions, extensions)) {
for (int i = 0; i < extensions.size(); i++) {
r_extensions->push_back(extensions[i]);
}
} else {
ERR_PRINT("Custom translation parser plugin's \"func get_recognized_extensions()\" is undefined.");
}
}
void EditorTranslationParserPlugin::_bind_methods() {
GDVIRTUAL_BIND(_parse_file, "path", "msgids", "msgids_context_plural");
GDVIRTUAL_BIND(_get_recognized_extensions);
}
/////////////////////////
void EditorTranslationParser::get_recognized_extensions(List<String> *r_extensions) const {
RBSet<String> extensions;
List<String> temp;
for (int i = 0; i < standard_parsers.size(); i++) {
standard_parsers[i]->get_recognized_extensions(&temp);
}
for (int i = 0; i < custom_parsers.size(); i++) {
custom_parsers[i]->get_recognized_extensions(&temp);
}
// Remove duplicates.
for (int i = 0; i < temp.size(); i++) {
extensions.insert(temp[i]);
}
for (RBSet<String>::Element *E = extensions.front(); E; E = E->next()) {
r_extensions->push_back(E->get());
}
}
bool EditorTranslationParser::can_parse(const String &p_extension) const {
List<String> extensions;
get_recognized_extensions(&extensions);
for (int i = 0; i < extensions.size(); i++) {
if (p_extension == extensions[i]) {
return true;
}
}
return false;
}
Ref<EditorTranslationParserPlugin> EditorTranslationParser::get_parser(const String &p_extension) const {
// Consider user-defined parsers first.
for (int i = 0; i < custom_parsers.size(); i++) {
List<String> temp;
custom_parsers[i]->get_recognized_extensions(&temp);
for (int j = 0; j < temp.size(); j++) {
if (temp[j] == p_extension) {
return custom_parsers[i];
}
}
}
for (int i = 0; i < standard_parsers.size(); i++) {
List<String> temp;
standard_parsers[i]->get_recognized_extensions(&temp);
for (int j = 0; j < temp.size(); j++) {
if (temp[j] == p_extension) {
return standard_parsers[i];
}
}
}
WARN_PRINT("No translation parser available for \"" + p_extension + "\" extension.");
return nullptr;
}
void EditorTranslationParser::add_parser(const Ref<EditorTranslationParserPlugin> &p_parser, ParserType p_type) {
if (p_type == ParserType::STANDARD) {
standard_parsers.push_back(p_parser);
} else if (p_type == ParserType::CUSTOM) {
custom_parsers.push_back(p_parser);
}
}
void EditorTranslationParser::remove_parser(const Ref<EditorTranslationParserPlugin> &p_parser, ParserType p_type) {
if (p_type == ParserType::STANDARD) {
standard_parsers.erase(p_parser);
} else if (p_type == ParserType::CUSTOM) {
custom_parsers.erase(p_parser);
}
}
void EditorTranslationParser::clean_parsers() {
standard_parsers.clear();
custom_parsers.clear();
}
EditorTranslationParser *EditorTranslationParser::get_singleton() {
if (!singleton) {
singleton = memnew(EditorTranslationParser);
}
return singleton;
}
EditorTranslationParser::EditorTranslationParser() {
}
EditorTranslationParser::~EditorTranslationParser() {
memdelete(singleton);
singleton = nullptr;
}
|