-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsvg2svg.py
executable file
·60 lines (49 loc) · 1.56 KB
/
svg2svg.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 2016 - Walter Bender <walter@sugarlabs.org>
# Convert tspan strings in an SVG from one langauge to another using a PO file.
# How to use: script.py svg-path po-file
# Outputs to svg-file_
import os
import re
import sys
import glob
import string
import codecs
def mine_svg_file(svg_filename, po_filename):
# Read data from the existing PO file
po_fd = codecs.open(po_filename, "r", "UTF-8")
po_dict = {}
# Build a translation dictionary from the PO file
for line in po_fd:
if line[0:5] == 'msgid':
key = line[7:-2]
elif line[0:6] == 'msgstr':
if line[8:-2] != '':
po_dict[key] = line[8:-2]
# Mine strings for l23n from the svg.
svg_list = []
svg_fd = codecs.open(svg_filename, "r", "UTF-8")
tmp = svg_filename.split('.');
output = codecs.open(tmp[0] + '_.svg', 'w', "UTF-8")
count = 1
for line in svg_fd:
tmp = line.split('</tspan>')
if len(tmp) > 1:
trans_line = ''
tmp2 = tmp[0].split('>')
for i in range(len(tmp2) - 1):
trans_line += tmp2[i]
trans_line += '>'
if tmp2[-1] in po_dict:
trans_line += po_dict[tmp2[-1]]
else:
trans_line += tmp2[-1]
trans_line += '</tspan>'
trans_line += tmp[1]
output.write(trans_line)
else:
output.write(line)
output.close()
if __name__ == '__main__':
ini = mine_svg_file(sys.argv[1], sys.argv[2])