Create a new ts file for a new language in a Qt project
CHANGELOG
7 | 7 | ||
8 | 8 | * When an error occurs while fetching a project, an error message is shown and | |
9 | 9 | the new project window re-opens with the same details as before | |
10 | + | * Create a new ts file for languages not yet supported by a Qt project | |
10 | 11 | ||
11 | 12 | === UI Changes === | |
12 | 13 |
offlate/formats/ts.py
18 | 18 | import datetime | |
19 | 19 | import os.path | |
20 | 20 | import xml.etree.ElementTree as ET | |
21 | + | import time | |
21 | 22 | from .entry import TSEntry | |
22 | 23 | ||
24 | + | from PyQt5.QtCore import * | |
25 | + | ||
26 | + | # from https://github.com/qt/qttools/blob/5.12/src/linguist/shared/numerus.cpp | |
27 | + | nplurals = { | |
28 | + | '1': [ | |
29 | + | QLocale.Bislama, | |
30 | + | QLocale.Burmese, | |
31 | + | QLocale.Chinese, | |
32 | + | QLocale.Dzongkha, | |
33 | + | QLocale.Fijian, | |
34 | + | QLocale.Guarani, | |
35 | + | QLocale.Hungarian, | |
36 | + | QLocale.Indonesian, | |
37 | + | QLocale.Japanese, | |
38 | + | QLocale.Javanese, | |
39 | + | QLocale.Korean, | |
40 | + | QLocale.Malay, | |
41 | + | QLocale.NauruLanguage, | |
42 | + | QLocale.Oromo, | |
43 | + | QLocale.Persian, | |
44 | + | QLocale.Sundanese, | |
45 | + | QLocale.Tatar, | |
46 | + | QLocale.Thai, | |
47 | + | QLocale.Tibetan, | |
48 | + | QLocale.Turkish, | |
49 | + | QLocale.Vietnamese, | |
50 | + | QLocale.Yoruba, | |
51 | + | QLocale.Zhuang, | |
52 | + | ], | |
53 | + | '2': [ | |
54 | + | QLocale.Abkhazian, | |
55 | + | QLocale.Afar, | |
56 | + | QLocale.Afrikaans, | |
57 | + | QLocale.Albanian, | |
58 | + | QLocale.Amharic, | |
59 | + | QLocale.Assamese, | |
60 | + | QLocale.Aymara, | |
61 | + | QLocale.Azerbaijani, | |
62 | + | QLocale.Bashkir, | |
63 | + | QLocale.Basque, | |
64 | + | QLocale.Bengali, | |
65 | + | QLocale.Bihari, | |
66 | + | QLocale.Bulgarian, | |
67 | + | QLocale.Catalan, | |
68 | + | QLocale.Cornish, | |
69 | + | QLocale.Corsican, | |
70 | + | QLocale.Danish, | |
71 | + | QLocale.Dutch, | |
72 | + | QLocale.English, | |
73 | + | QLocale.Esperanto, | |
74 | + | QLocale.Estonian, | |
75 | + | QLocale.Faroese, | |
76 | + | QLocale.Finnish, | |
77 | + | QLocale.Friulian, | |
78 | + | QLocale.WesternFrisian, | |
79 | + | QLocale.Galician, | |
80 | + | QLocale.Georgian, | |
81 | + | QLocale.German, | |
82 | + | QLocale.Greek, | |
83 | + | QLocale.Greenlandic, | |
84 | + | QLocale.Gujarati, | |
85 | + | QLocale.Hausa, | |
86 | + | QLocale.Hebrew, | |
87 | + | QLocale.Hindi, | |
88 | + | QLocale.Interlingua, | |
89 | + | QLocale.Interlingue, | |
90 | + | QLocale.Italian, | |
91 | + | QLocale.Kannada, | |
92 | + | QLocale.Kashmiri, | |
93 | + | QLocale.Kazakh, | |
94 | + | QLocale.Khmer, | |
95 | + | QLocale.Kinyarwanda, | |
96 | + | QLocale.Kirghiz, | |
97 | + | QLocale.Kurdish, | |
98 | + | QLocale.Lao, | |
99 | + | QLocale.Latin, | |
100 | + | QLocale.Lingala, | |
101 | + | QLocale.Luxembourgish, | |
102 | + | QLocale.Malagasy, | |
103 | + | QLocale.Malayalam, | |
104 | + | QLocale.Marathi, | |
105 | + | QLocale.Mongolian, | |
106 | + | QLocale.Nepali, | |
107 | + | QLocale.NorthernSotho, | |
108 | + | QLocale.NorwegianBokmal, | |
109 | + | QLocale.NorwegianNynorsk, | |
110 | + | QLocale.Occitan, | |
111 | + | QLocale.Oriya, | |
112 | + | QLocale.Pashto, | |
113 | + | QLocale.Portuguese, | |
114 | + | QLocale.Punjabi, | |
115 | + | QLocale.Quechua, | |
116 | + | QLocale.Romansh, | |
117 | + | QLocale.Rundi, | |
118 | + | QLocale.Shona, | |
119 | + | QLocale.Sindhi, | |
120 | + | QLocale.Sinhala, | |
121 | + | QLocale.Somali, | |
122 | + | QLocale.SouthernSotho, | |
123 | + | QLocale.Spanish, | |
124 | + | QLocale.Swahili, | |
125 | + | QLocale.Swati, | |
126 | + | QLocale.Swedish, | |
127 | + | QLocale.Tajik, | |
128 | + | QLocale.Tamil, | |
129 | + | QLocale.Telugu, | |
130 | + | QLocale.Tongan, | |
131 | + | QLocale.Tsonga, | |
132 | + | QLocale.Tswana, | |
133 | + | QLocale.Turkmen, | |
134 | + | QLocale.Uigur, | |
135 | + | QLocale.Urdu, | |
136 | + | QLocale.Uzbek, | |
137 | + | QLocale.Volapuk, | |
138 | + | QLocale.Wolof, | |
139 | + | QLocale.Xhosa, | |
140 | + | QLocale.Yiddish, | |
141 | + | QLocale.Zulu, | |
142 | + | QLocale.Armenian, | |
143 | + | QLocale.Breton, | |
144 | + | QLocale.French, | |
145 | + | QLocale.Portuguese, | |
146 | + | QLocale.Filipino, | |
147 | + | QLocale.Tigrinya, | |
148 | + | QLocale.Walloon, | |
149 | + | QLocale.Icelandic, | |
150 | + | ], | |
151 | + | '3': [ | |
152 | + | QLocale.Latvian, | |
153 | + | QLocale.Divehi, | |
154 | + | QLocale.Inuktitut, | |
155 | + | QLocale.Inupiak, | |
156 | + | QLocale.Irish, | |
157 | + | QLocale.Manx, | |
158 | + | QLocale.Maori, | |
159 | + | QLocale.NorthernSami, | |
160 | + | QLocale.Samoan, | |
161 | + | QLocale.Sanskrit, | |
162 | + | QLocale.Slovak, | |
163 | + | QLocale.Czech, | |
164 | + | QLocale.Macedonian, | |
165 | + | QLocale.Lithuanian, | |
166 | + | QLocale.Bosnian, | |
167 | + | QLocale.Belarusian, | |
168 | + | QLocale.Croatian, | |
169 | + | QLocale.Russian, | |
170 | + | QLocale.Serbian, | |
171 | + | QLocale.Ukrainian, | |
172 | + | QLocale.Polish, | |
173 | + | QLocale.Romanian, | |
174 | + | QLocale.Tagalog, | |
175 | + | ], | |
176 | + | '4': [ | |
177 | + | QLocale.Gaelic, | |
178 | + | QLocale.Slovenian, | |
179 | + | QLocale.Maltese, | |
180 | + | ], | |
181 | + | '5': [ | |
182 | + | QLocale.Welsh, | |
183 | + | ], | |
184 | + | '6': [ | |
185 | + | QLocale.Maltese, | |
186 | + | QLocale.Arabic, | |
187 | + | ] | |
188 | + | } | |
189 | + | ||
190 | + | def get_nplurals(locale): | |
191 | + | l = QLocale(locale) | |
192 | + | for nplural in nplurals.keys(): | |
193 | + | if l.language() in nplurals[nplural]: | |
194 | + | return nplural | |
195 | + | return 0 | |
196 | + | ||
23 | 197 | class TSFormat: | |
24 | 198 | def __init__(self, conf): | |
199 | + | self.conf = conf | |
25 | 200 | self.tsfilename = conf["file"] | |
201 | + | if not os.path.isfile(conf["file"]): | |
202 | + | self.createNewTS() | |
26 | 203 | self.tscontent = self.parse(self.tsfilename) | |
27 | - | self.conf = conf | |
28 | 204 | self.savedcontent = [TSEntry(x) for x in self.tscontent] | |
29 | 205 | ||
206 | + | def createNewTS(self): | |
207 | + | template = ET.parse(self.conf["template"]) | |
208 | + | content = template.getroot() | |
209 | + | root = ET.Element('TS') | |
210 | + | for context in content: | |
211 | + | if context.tag == "context": | |
212 | + | for item in context: | |
213 | + | if item.tag == "message": | |
214 | + | numerus = item.get('numerus') == 'yes' | |
215 | + | for child in item: | |
216 | + | if child.tag == "translation": | |
217 | + | child.text = "" | |
218 | + | if numerus: | |
219 | + | item.remove(child) | |
220 | + | child = ET.Element('translation') | |
221 | + | for i in range(0, int(get_nplurals(self.conf['lang']))): | |
222 | + | print("numerusform") | |
223 | + | child.append(ET.Element('numerusform')) | |
224 | + | item.append(child) | |
225 | + | break | |
226 | + | root.append(context) | |
227 | + | with open(self.tsfilename, "w+") as f: | |
228 | + | f.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n") | |
229 | + | f.write("<!DOCTYPE TS>") | |
230 | + | f.write(ET.tostring(root).decode("utf-8")) | |
231 | + | ||
30 | 232 | def parse(self, filename): | |
31 | 233 | result = [] | |
32 | 234 | content = ET.parse(filename) |
offlate/systems/git.py
93 | 93 | 'source': path + resource['template']})}) | |
94 | 94 | elif resource['file_format'] == 'ts': | |
95 | 95 | yamlpath = resource['filemask'].replace('*', self.lang) | |
96 | + | enpath = resource['filemask'].replace('*', 'en') | |
97 | + | template = None | |
98 | + | if Path(path + enpath).exists(): | |
99 | + | template = enpath | |
100 | + | else: | |
101 | + | template = Path(path).glob(resource['filemask'])[0] | |
96 | 102 | translationfiles.append({'filename': yamlpath, | |
97 | - | 'format': TSFormat({'file': path + yamlpath, 'lang': self.lang})}) | |
103 | + | 'format': TSFormat({'file': path + yamlpath, 'lang': self.lang, 'template': template})}) | |
98 | 104 | else: | |
99 | 105 | raise UnsupportedFormatException(resource['file_format']) | |
100 | 106 | return translationfiles |