forked from remram44/pybabel-godot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabel_godot.py
246 lines (195 loc) · 8.65 KB
/
babel_godot.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import re
__version__ = '2.0'
_godot_node = re.compile(r'^\[node name="([^"]+)" (?:type="([^"]+)")?')
_godot_property_str = re.compile(r'^([A-Za-z0-9_/]+)\s*=\s*([\[|"|&].+)$')
_godot_bus_name_property_str = re.compile(r'^bus/[0-9]+/name?')
_godot_escaped_tr = re.compile(r'^.*[^A-Za-z0-9_]tr\(&?\\"(.+?[^\\])\\"\)?')
def _godot_unquote(string):
if string[0] == '&':
string = string[1:]
if string[0] != '"' or string[-1] != '"':
return None
result = []
escaped = False
for c in string[1:-1]:
if escaped:
if c == '\\':
#result.append('\\')
continue
elif c == 'n':
result.append('\n')
elif c == 't':
result.append('\t')
else:
result.append(c)
escaped = False
else:
if c == '\\':
escaped = True
else:
result.append(c)
return ''.join(result)
def _assemble_multiline_string(line, multiline):
to_yield = []
if '", "' in line:
# Multiline string ends within an array of strings
line_parts = line.split('", "')
multiline['value'] += line_parts[0]
value = _godot_unquote('"' + multiline['value'] + '"')
if value is not None:
to_yield.append([multiline['keyword'], value])
# Take care of intermediate strings in array (not multiline)
for line_part in line_parts[1:-1]:
value = _godot_unquote('"' + line_part + '"')
if value is not None:
to_yield.append([multiline['keyword'], value])
# Continue with the last array item normally
multiline['value'] = ''
line = line_parts[-1]
if not line.endswith('"\n') and not line.endswith(']\n'):
# Continuation of multiline string
multiline['value'] += line
else:
# Multiline string ends
multiline['value'] += line.strip('"\n')
value = _godot_unquote('"' + multiline['value'] + '"')
if value is not None:
to_yield.append([multiline['keyword'], value])
multiline['keyword'] = ''
multiline['value'] = ''
return to_yield
def extract_godot_scene(fileobj, keywords, comment_tags, options):
"""Extract messages from Godot scene files (.tscn).
:param fileobj: the seekable, file-like object the messages should be
extracted from
:param keywords: a list of property names that should be localized, in the
format '<NodeType>/<name>' or '<name>' (example:
'Label/text')
:param comment_tags: a list of translator tags to search for and include
in the results (ignored)
:param options: a dictionary of additional options (optional)
:rtype: iterator
"""
encoding = options.get('encoding', 'utf-8')
current_node_type = None
multiline = {'keyword': '', 'value': ''}
look_for_builtin_tr = 'tr' in keywords
properties_to_translate = {}
for keyword in keywords:
if '/' in keyword:
properties_to_translate[tuple(keyword.split('/', 1))] = keyword
else:
properties_to_translate[(None, keyword)] = keyword
def check_translate_property(property):
keyword = properties_to_translate.get((current_node_type, property))
if keyword is None:
keyword = properties_to_translate.get((None, property))
return keyword
for lineno, line in enumerate(fileobj, start=1):
line = line.decode(encoding)
# Handle multiline strings
if multiline['keyword']:
to_yield = _assemble_multiline_string(line, multiline)
for item in to_yield:
yield (lineno, item[0], [item[1]], [])
continue
match = _godot_node.match(line)
if match:
# Store which kind of node we're in
current_node_type = match.group(2)
#instanced packed scenes don't have the type field,
#change current_node_type to empty string
current_node_type = current_node_type \
if current_node_type is not None else ""
elif line.startswith('['):
# We're no longer in a node
current_node_type = None
elif current_node_type is not None:
# Currently in a node, check properties
match = _godot_property_str.match(line)
if match:
property = match.group(1)
value = match.group(2)
keyword = check_translate_property(property)
if keyword:
# Beginning of multiline string
if not value.endswith('"') and not value.endswith('"]'):
multiline['keyword'] = keyword
multiline['value'] = value.strip('[ "') + '\n'
continue
# Handle array of strings
if value.startswith('[ "') or value.startswith('["'):
values = value.strip('[ "]').split('", "')
for value in values:
value = _godot_unquote('"' + value + '"')
if value is not None:
yield (lineno, keyword, [value], [])
else:
value = _godot_unquote(value)
if value is not None:
yield (lineno, keyword, [value], [])
elif look_for_builtin_tr:
# Handle Godot's tr() for built-in scripts
match = _godot_escaped_tr.match(line)
if match:
value = _godot_unquote('"' + match.group(1) + '"')
if value is not None:
yield (lineno, keyword, [value], [])
def extract_godot_resource(fileobj, keywords, comment_tags, options):
"""Extract messages from Godot resource files (.res, .tres).
:param fileobj: the seekable, file-like object the messages should be
extracted from
:param keywords: a list of property names that should be localized, in the
format 'Resource/<name>' or '<name>' (example:
'Resource/text')
:param comment_tags: a list of translator tags to search for and include
in the results (ignored)
:param options: a dictionary of additional options (optional)
:rtype: iterator
"""
encoding = options.get('encoding', 'utf-8')
multiline = {'keyword': '', 'value': ''}
properties_to_translate = {}
for keyword in keywords:
if keyword.startswith('Resource/'):
properties_to_translate[keyword[9:]] = keyword
else:
# Without this else-case, any '<name>' properties (not starting with 'Resource/') would be ignored
properties_to_translate[keyword] = keyword
def check_translate_property(property):
return properties_to_translate.get(property)
for lineno, line in enumerate(fileobj, start=1):
line = line.decode(encoding)
if line.startswith('['):
continue
# Handle multiline strings
if multiline['keyword']:
to_yield = _assemble_multiline_string(line, multiline)
for item in to_yield:
yield (lineno, item[0], [item[1]], [])
continue
match = _godot_property_str.match(line)
if match:
property = match.group(1)
# Convert "bus/{bus_idx}/name" to "bus_name" so audio bus names can be localized,
if _godot_bus_name_property_str.match(property):
property = "bus_name"
value = match.group(2)
keyword = check_translate_property(property)
if keyword and value != '""' and value != "[]" and value != "[ ]": # [ ] godot 3 remnant
# Beginning of multiline string
if not value.endswith('"') and not value.endswith('"]'):
multiline['keyword'] = keyword
multiline['value'] = value.strip('[ "') + '\n'
continue
# Handle array of strings
if value.startswith('[ "') or value.startswith('["'):
values = value.strip('[ "]').split('", "')
for value in values:
value = _godot_unquote('"' + value + '"')
if value is not None:
yield (lineno, keyword, [value], [])
else:
value = _godot_unquote(value)
if value is not None:
yield (lineno, keyword, [value], [])