Add appstore format
offlate/formats/appstore.py unknown status 1
1 | + | # Copyright (c) 2019 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 appstore format for Android translations (fastlane). """ | |
17 | + | ||
18 | + | import os | |
19 | + | from pathlib import Path | |
20 | + | ||
21 | + | from .entry import AppstoreEntry | |
22 | + | ||
23 | + | def findRecursive(directory): | |
24 | + | ans = [] | |
25 | + | for f in os.listdir(directory): | |
26 | + | f = os.path.join(directory, f) | |
27 | + | if os.path.isfile(f): | |
28 | + | ans.append(f) | |
29 | + | elif os.path.isdir(f): | |
30 | + | ans.extend(findRecursive(f)) | |
31 | + | return ans | |
32 | + | ||
33 | + | class AppstoreFormat: | |
34 | + | def __init__(self, conf): | |
35 | + | self.conf = conf | |
36 | + | self.translationpath = conf["file"] | |
37 | + | self.enpath = conf["template"] | |
38 | + | self.conf = conf | |
39 | + | self.reload() | |
40 | + | ||
41 | + | def content(self): | |
42 | + | return self.resources | |
43 | + | ||
44 | + | def save(self): | |
45 | + | for r in self.resources: | |
46 | + | with open(r.filename, 'w') as f: | |
47 | + | f.write(r.tr) | |
48 | + | ||
49 | + | def merge(self, older, callback): | |
50 | + | for r in self.resources: | |
51 | + | for oldr in older.resources: | |
52 | + | nfile = r.filename[len(self.translationpath):] | |
53 | + | ofile = oldr.filename[len(older.translationpath):] | |
54 | + | if nfile != ofile: | |
55 | + | continue | |
56 | + | ncontent = r.tr | |
57 | + | ocontent = oldr.tr | |
58 | + | if ncontent == ocontent: | |
59 | + | continue | |
60 | + | if ocontent == "": | |
61 | + | continue | |
62 | + | if ncontent == "": | |
63 | + | r.tr = oldr.tr | |
64 | + | continue | |
65 | + | r.tr = callback(r.en, ocontent, ncontent) | |
66 | + | ||
67 | + | def getExternalFiles(self): | |
68 | + | return findRecursive(self.enpath).extend(findRecursive(self.translationpath)) | |
69 | + | ||
70 | + | def reload(self): | |
71 | + | enresources = findRecursive(self.enpath) | |
72 | + | resources = [] | |
73 | + | for r in enresources: | |
74 | + | enr = r[len(self.enpath):] | |
75 | + | filename = r.replace(self.enpath, self.translationpath) | |
76 | + | if enr.endswith('.txt'): | |
77 | + | enval = open(r).read() | |
78 | + | trval = "" | |
79 | + | if os.path.isfile(filename): | |
80 | + | trval = open(filename).read() | |
81 | + | resources.append(AppstoreEntry(filename, enval, trval)) | |
82 | + | self.resources = resources |
offlate/formats/entry.py
83 | 83 | if self.parent is not None: | |
84 | 84 | self.parent.dst[self.index] = content | |
85 | 85 | ||
86 | + | class AppstoreEntry(Entry): | |
87 | + | def __init__(self, filename, en, tr): | |
88 | + | Entry.__init__(self, [en], [tr], False, False) | |
89 | + | self.en = en | |
90 | + | self.tr = tr | |
91 | + | self.filename = filename | |
92 | + | ||
93 | + | def update(self, index, content): | |
94 | + | Entry.update(self, index, content) | |
95 | + | self.tr = content | |
96 | + | ||
97 | + | ||
86 | 98 | class JSONEntry(Entry): | |
87 | 99 | def __init__(self, entry): | |
88 | 100 | Entry.__init__(self, [entry['source_string']], [entry['translation']], False, False) |
offlate/systems/git.py
24 | 24 | from ..formats.ts import TSFormat | |
25 | 25 | from ..formats.yaml import YamlFormat | |
26 | 26 | from ..formats.androidstrings import AndroidStringsFormat | |
27 | + | from ..formats.appstore import AppstoreFormat | |
27 | 28 | from ..formats.formatException import UnsupportedFormatException | |
28 | 29 | from .systemException import ProjectNotFoundSystemException | |
29 | 30 | ||
… | |||
110 | 111 | translationfiles.append({'filename': translationpath, | |
111 | 112 | 'format': AndroidStringsFormat({'file': path + translationpath, 'lang': self.lang, | |
112 | 113 | 'template': path + enpath})}) | |
114 | + | elif resource['file_format'] == 'appstore': | |
115 | + | translationpath = resource['filemask'].replace('*', self.lang) | |
116 | + | enpath = resource['template'] | |
117 | + | translationfiles.append({'filename': translationpath, | |
118 | + | 'format': AppstoreFormat({'file': path + translationpath, 'lang': self.lang, | |
119 | + | 'template': path + enpath})}) | |
113 | 120 | else: | |
114 | 121 | raise UnsupportedFormatException(resource['file_format']) | |
115 | 122 | return translationfiles |