-
Notifications
You must be signed in to change notification settings - Fork 0
/
js2prop.py
56 lines (50 loc) · 2.02 KB
/
js2prop.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
# -*- coding: utf-8 -*-
# js2prop.py: App Inventor 2 (AI2) translation helper
# Split _messages.js into a preamble, a properties file (usable with po2prop) and a postamble
#
# The properties are represented as assignments in the .js file:
# Blockly.Msg.LANG_LISTS_LOOKUP_IN_PAIRS_INPUT = "parvis uppslagning nyckel %1 par %2 hittadesInte %3"
#
# In the .properties file this corresponds to
# Blockly.Msg.LANG_LISTS_LOOKUP_IN_PAIRS_INPUT = parvis uppslagning nyckel %1 par %2 hittadesInte %3
#
# See https://github.com/mit-cml/appinventor-sources/blob/master/appinventor/blocklyeditor/src/msg/sv/_messages.js
# Thanks to https://jis.qyv.name/ for helping out
import re
from sys import argv
def quote_leading_space(s):
if len(s) == 0 or s[0] != ' ':
return s
return '\\' + s
def main():
script, filename = argv
files = ['1-pre-' + filename, '2-' + filename + '.properties', '3-post-' + filename]
preamble, properties, postamble = 0, 1, 2
fileCount = preamble
target = open(files[fileCount], 'w')
target.truncate()
p = re.compile(r'(Blockly[^\s=]+)\s=\s\"(.*)"$', re.IGNORECASE)
with open(filename, encoding='utf-8') as inf:
for line in inf:
if fileCount == preamble:
m = p.search(line)
if m:
target.close()
fileCount = properties
target = open(files[fileCount], 'w', encoding='utf-8')
target.truncate()
else:
target.write(line)
if fileCount == properties:
m = p.search(line)
if m:
target.write(''.join([m.group(1), ' = ', quote_leading_space(m.group(2)), '\n']))
else:
target.close()
fileCount = postamble
target = open(files[fileCount], 'w')
target.truncate()
if fileCount == postamble:
target.write(line)
if __name__ == "__main__":
main()