Add save support to ts format

Julien LepillerFri Aug 23 21:59:34+0200 2019

1fe5b41

Add save support to ts format

offlate/formats/entry.py

8282
class TSEntry(Entry):
8383
    def __init__(self, entry):
8484
        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)
87103
88104
    def update(self, index, content):
89105
        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

2525
        self.tsfilename = conf["file"]
2626
        self.tscontent = self.parse(self.tsfilename)
2727
        self.conf = conf
28-
28+
        self.savedcontent = None
2929
3030
    def parse(self, filename):
3131
        result = []

3737
                if child.tag == "name":
3838
                    contextname = child.text
3939
                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)
5941
        return result
6042
6143
    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
6347
6448
    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"))
6682
6783
    def merge(self, older, callback):
6884
        pass

offlate/systems/git.py

9494
            elif resource['file_format'] == 'ts':
9595
                yamlpath = resource['filemask'].replace('*', self.lang)
9696
                translationfiles.append({'filename': yamlpath,
97-
                    'format': TSFormat({'file': path + yamlpath})})
97+
                    'format': TSFormat({'file': path + yamlpath, 'lang': self.lang})})
9898
            else:
9999
                raise Exception('Unsupported file format: {}'.format(resource['file_format']))
100100
        return translationfiles