Add save support to ts format
offlate/formats/entry.py
82 | 82 | class TSEntry(Entry): | |
83 | 83 | def __init__(self, entry): | |
84 | 84 | self.entry = entry | |
85 | - | Entry.__init__(self, entry['source_string'], entry['translation'], | |
86 | - | False, False) | |
85 | + | numerus = entry.get('numerus') == 'yes' | |
86 | + | sourcestring = "" | |
87 | + | translation = [] if numerus else "" | |
88 | + | for child in entry: | |
89 | + | if child.tag == "source": | |
90 | + | sourcestring = child.text | |
91 | + | elif child.tag == "translation": | |
92 | + | if numerus: | |
93 | + | for form in child: | |
94 | + | translation.append(form.text) | |
95 | + | else: | |
96 | + | translation = child.text | |
97 | + | if numerus: | |
98 | + | sourcestring = [sourcestring, sourcestring] | |
99 | + | else: | |
100 | + | translation = [translation] | |
101 | + | sourcestring = [sourcestring] | |
102 | + | Entry.__init__(self, sourcestring, translation, False, False) | |
87 | 103 | ||
88 | 104 | def update(self, index, content): | |
89 | 105 | Entry.update(self, index, content) | |
90 | - | self.entry['translation'] = content | |
106 | + | numerus = self.entry.get('numerus') == 'yes' | |
107 | + | for child in self.entry: | |
108 | + | if child.tag == "translation": | |
109 | + | if numerus: | |
110 | + | i=0 | |
111 | + | for form in child: | |
112 | + | if i == index: | |
113 | + | form.text = content | |
114 | + | break | |
115 | + | i = i + 1 | |
116 | + | else: | |
117 | + | child.text = content |
offlate/formats/ts.py
25 | 25 | self.tsfilename = conf["file"] | |
26 | 26 | self.tscontent = self.parse(self.tsfilename) | |
27 | 27 | self.conf = conf | |
28 | - | ||
28 | + | self.savedcontent = None | |
29 | 29 | ||
30 | 30 | def parse(self, filename): | |
31 | 31 | result = [] | |
… | |||
37 | 37 | if child.tag == "name": | |
38 | 38 | contextname = child.text | |
39 | 39 | elif child.tag == "message": | |
40 | - | numerus = child.get('numerus') == 'yes' | |
41 | - | sourcestring = "" | |
42 | - | translation = [] if numerus else "" | |
43 | - | for child2 in child: | |
44 | - | if child2.tag == "source": | |
45 | - | sourcestring = child2.text | |
46 | - | elif child2.tag == "translation": | |
47 | - | if numerus: | |
48 | - | for form in child2: | |
49 | - | translation.append(form.text) | |
50 | - | else: | |
51 | - | translation = child2.text | |
52 | - | if numerus: | |
53 | - | sourcestring = [sourcestring, sourcestring] | |
54 | - | else: | |
55 | - | translation = [translation] | |
56 | - | sourcestring = [sourcestring] | |
57 | - | result.append({'source_string': sourcestring, | |
58 | - | 'translation': translation}) | |
40 | + | result.append(child) | |
59 | 41 | return result | |
60 | 42 | ||
61 | 43 | def content(self): | |
62 | - | return [TSEntry(x) for x in self.tscontent] | |
44 | + | if self.savedcontent is None: | |
45 | + | self.savedcontent = [TSEntry(x) for x in self.tscontent] | |
46 | + | return self.savedcontent | |
63 | 47 | ||
64 | 48 | def save(self): | |
65 | - | pass | |
49 | + | root = ET.Element('TS') | |
50 | + | root.set("language", self.conf["lang"]) | |
51 | + | tree = ET.ElementTree(root) | |
52 | + | content = ET.parse(self.tsfilename).getroot() | |
53 | + | for context in content: | |
54 | + | if context.tag == "context": | |
55 | + | for item in context: | |
56 | + | if item.tag == "message": | |
57 | + | numerus = item.get('numerus') == 'yes' | |
58 | + | sourcestring = "" | |
59 | + | for child in item: | |
60 | + | if child.tag == "source": | |
61 | + | sourcestring = child.text | |
62 | + | break | |
63 | + | msgstrs = [] | |
64 | + | for entry in self.savedcontent: | |
65 | + | if entry.msgids[0] == sourcestring: | |
66 | + | msgstrs = entry.msgstrs | |
67 | + | for child in item: | |
68 | + | if child.tag == "translation": | |
69 | + | if numerus: | |
70 | + | i = 0 | |
71 | + | for form in child: | |
72 | + | form.text = msgstrs[i] | |
73 | + | i += 1 | |
74 | + | else: | |
75 | + | child.text = msgstrs[0] | |
76 | + | break | |
77 | + | root.append(context) | |
78 | + | with open(self.tsfilename, "w+") as f: | |
79 | + | f.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n") | |
80 | + | f.write("<!DOCTYPE TS>") | |
81 | + | f.write(ET.tostring(root).decode("utf-8")) | |
66 | 82 | ||
67 | 83 | def merge(self, older, callback): | |
68 | 84 | pass |
offlate/systems/git.py
94 | 94 | elif resource['file_format'] == 'ts': | |
95 | 95 | yamlpath = resource['filemask'].replace('*', self.lang) | |
96 | 96 | translationfiles.append({'filename': yamlpath, | |
97 | - | 'format': TSFormat({'file': path + yamlpath})}) | |
97 | + | 'format': TSFormat({'file': path + yamlpath, 'lang': self.lang})}) | |
98 | 98 | else: | |
99 | 99 | raise Exception('Unsupported file format: {}'.format(resource['file_format'])) | |
100 | 100 | return translationfiles |