Add initial TS format support

Julien LepillerFri Aug 23 20:01:22+0200 2019

842e392

Add initial TS format support

offlate/formats/entry.py

7878
    def update(self, index, content):
7979
        Entry.update(self, index, content)
8080
        self.entry['translation'] = content
81+
82+
class TSEntry(Entry):
83+
    def __init__(self, entry):
84+
        self.entry = entry
85+
        Entry.__init__(self, entry['source_string'], entry['translation'],
86+
                False, False)
87+
88+
    def update(self, index, content):
89+
        Entry.update(self, index, content)
90+
        self.entry['translation'] = content

offlate/formats/ts.py unknown status 1

1+
#   Copyright (c) 2018 Julien Lepiller <julien@lepiller.eu>
2+
#
3+
#   This program is free software: you can redistribute it and/or modify
4+
#   it under the terms of the GNU Affero General Public License as
5+
#   published by the Free Software Foundation, either version 3 of the
6+
#   License, or (at your option) any later version.
7+
#
8+
#   This program is distributed in the hope that it will be useful,
9+
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11+
#   GNU Affero General Public License for more details.
12+
#
13+
#   You should have received a copy of the GNU Affero General Public License
14+
#   along with this program.  If not, see <https://www.gnu.org/licenses/>.
15+
####
16+
""" The ts format for Qt applications. """
17+
18+
import datetime
19+
import os.path
20+
import xml.etree.ElementTree as ET
21+
from .entry import TSEntry
22+
23+
class TSFormat:
24+
    def __init__(self, conf):
25+
        self.tsfilename = conf["file"]
26+
        self.tscontent = self.parse(self.tsfilename)
27+
        self.conf = conf
28+
29+
30+
    def parse(self, filename):
31+
        result = []
32+
        content = ET.parse(filename)
33+
        root = content.getroot()
34+
        for context in root:
35+
            contextname = ""
36+
            for child in context:
37+
                if child.tag == "name":
38+
                    contextname = child.text
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})
59+
        return result
60+
61+
    def content(self):
62+
        return [TSEntry(x) for x in self.tscontent]
63+
64+
    def save(self):
65+
        pass
66+
67+
    def merge(self, older, callback):
68+
        pass
69+

offlate/systems/git.py

2020
from pathlib import Path
2121
2222
from translation_finder import discover
23-
from ..formats.yaml import YamlFormat
2423
from ..formats.gettext import GettextFormat
24+
from ..formats.ts import TSFormat
25+
from ..formats.yaml import YamlFormat
2526
2627
def rmdir(dir):
2728
    dir = Path(dir)

9091
                translationfiles.append({'filename': yamlpath,
9192
                    'format': YamlFormat({'dest': path + yamlpath,
9293
                        'source': path + resource['template']})})
94+
            elif resource['file_format'] == 'ts':
95+
                yamlpath = resource['filemask'].replace('*', self.lang)
96+
                translationfiles.append({'filename': yamlpath,
97+
                    'format': TSFormat({'file': path + yamlpath})})
9398
            else:
9499
                raise Exception('Unsupported file format: {}'.format(resource['file_format']))
95100
        return translationfiles

129134
    def content(self):
130135
        content = {}
131136
        for resource in self.translationfiles:
137+
            print(resource['format'])
132138
            content[resource['filename']] = resource['format'].content()
133139
        return content