Add initial TS format support
offlate/formats/entry.py
| 78 | 78 | def update(self, index, content): | |
| 79 | 79 | Entry.update(self, index, content) | |
| 80 | 80 | 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
| 20 | 20 | from pathlib import Path | |
| 21 | 21 | ||
| 22 | 22 | from translation_finder import discover | |
| 23 | - | from ..formats.yaml import YamlFormat | |
| 24 | 23 | from ..formats.gettext import GettextFormat | |
| 24 | + | from ..formats.ts import TSFormat | |
| 25 | + | from ..formats.yaml import YamlFormat | |
| 25 | 26 | ||
| 26 | 27 | def rmdir(dir): | |
| 27 | 28 | dir = Path(dir) | |
… | |||
| 90 | 91 | translationfiles.append({'filename': yamlpath, | |
| 91 | 92 | 'format': YamlFormat({'dest': path + yamlpath, | |
| 92 | 93 | '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})}) | |
| 93 | 98 | else: | |
| 94 | 99 | raise Exception('Unsupported file format: {}'.format(resource['file_format'])) | |
| 95 | 100 | return translationfiles | |
… | |||
| 129 | 134 | def content(self): | |
| 130 | 135 | content = {} | |
| 131 | 136 | for resource in self.translationfiles: | |
| 137 | + | print(resource['format']) | |
| 132 | 138 | content[resource['filename']] = resource['format'].content() | |
| 133 | 139 | return content | |