forked from AvsPmod/AvsPmod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.py
456 lines (435 loc) · 15.7 KB
/
i18n.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# -*- coding: utf-8 -*-
# AvsP - an AviSynth editor
#
# Copyright 2007 Peter Jang <http://avisynth.nl/users/qwerpoi>
# 2010-2013 the AvsPmod authors <https://github.com/avspmod/avspmod>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA, or visit
# http://www.gnu.org/copyleft/gpl.html .
# i18n - internationalization and localization
#
# Dependencies:
# Python (tested on v2.6 and v2.7)
# Scripts:
# global_vars.py (application info and other shared variables)
import __builtin__
import sys
import os
import os.path
import glob
import re
import global_vars
def _(s): return s
__builtin__._ = _
pythonexe = sys.executable
pygettextpath = os.path.join(sys.prefix, 'Tools', 'i18n', 'pygettext.py')
toolsdir = 'tools'
macrosdir = 'macros'
def main(version=global_vars.version):
"""Generate __translation_new.py"""
argsList = ['avsp.py wxp.py pyavs.py pyavs_avifile.py global_vars.py']
# Get additional files to translate from the tools directory
sys.path.insert(0, toolsdir)
try:
items = __import__('ToolsMenu').menuInfo
except ImportError:
items = []
for item in items:
if len(item) == 3:
argsList.append(item)
newlines = ['messages = {\n']
messageSet = set()
for args in argsList:
newlines.extend(GenerateMessages(messageSet, args))
# Include the macros' filenames
newlines.append('\n #--- Macros ---#\n')
macro_list = []
label_list = []
global macrosdir
if not os.path.isdir(macrosdir):
macrosdir = os.path.join('..', macrosdir)
if not os.path.isdir(macrosdir):
exit('Macros directory not found')
re_macro = re.compile(r'(?:\[\s*\d+\s*\]\s+)?((?P<chr>\w)(?P=chr){2}\s+)?\s*([^\[].+?)\s*\.py')
for dirpath, dirnames, filenames in os.walk(macrosdir):
for filename in filenames:
match = re_macro.match(filename)
if match and match.group(3) != '---':
newlines.append(' "%s" : u"",\n' % match.group(3))
messageSet.add('"%s"' % match.group(3))
if not match.group('chr'):
macro_list.append('"%s" ' % os.path.abspath(os.path.join(dirpath, filename)))
label_list.append(match.group(3))
for dirname in dirnames:
newlines.append(' "%s" : u"",\n' % dirname)
# Include the macros
for macro, label in zip(macro_list, label_list):
lines = GenerateMessages(messageSet, macro)
if lines:
newlines.append('\n #--- Macro: %s ---#\n' % label)
newlines.extend(lines)
newlines.append('\n #--- Macros - Extra ---#\n')
newlines.append('}')
f = open('__translation_new.py', 'w')
f.write("new_translation_string = r'''")
f.write('version = "%s"\n\n' % version)
f.writelines(newlines)
f.write("'''")
f.close()
return True
def GenerateMessages(messageSet, args):
oldline = ''
newlines = []
if isinstance(args, basestring):
os.system('""%s" "%s" %s"' % (pythonexe, pygettextpath, args))
else:
filename, menuLabel, statusString = args
filename += '.py'
fullname = os.path.join(toolsdir, filename)
s = '\n #--- Tool: %s ---#\n' % (filename)
newlines.append(s)
s = ' "%s" : u"",\n' % (menuLabel)
newlines.append(s)
s = ' "%s" : u"",\n' % (statusString)
newlines.append(s)
os.system('""%s" "%s" %s"' % (pythonexe, pygettextpath, fullname))
f = open('messages.pot', 'r')
lines = f.readlines()
f.close()
os.remove('messages.pot')
multiline = False
for i, line in enumerate(lines):
line = line.strip()
if not multiline:
if line.startswith('msgid '):
text = line.split('msgid ')[1]
if text.strip('"'):
if oldline.startswith('#:') and text not in messageSet:
#~ comment = ' %s' % oldline
#~ s = ' %s : u"",%s\n' % (text, comment)
s = ' %s : u"",\n' % (text)
newlines.append(s)
messageSet.add(text)
else:
multiline = i
text = '"'
oldline = line
else:
if not line.startswith('msgstr '):
text += line.strip('"')
else:
text += '"'
if i - multiline > 1 and text not in messageSet:
s = ' %s : u"",\n' % (text)
newlines.append(s)
messageSet.add(text)
multiline = False
return newlines
def UpdateTranslationFile(dir, lang=None, version=global_vars.version):
try:
from __translation_new import new_translation_string
except ImportError:
if hasattr(sys,'frozen'):
raise
else:
main(version)
from __translation_new import new_translation_string
newmark = ' # New in v%s' % version
# Get the text from the translation file
file_list = ([os.path.join(dir, 'translation_%s.py' % lang)] if lang
else glob.glob(os.path.join(dir, 'translation_*.py')))
for filename in file_list:
if os.path.isfile(filename):
f = open(filename, 'r')
txt = f.read()
f.close()
else:
txt = ''
# Setup the new text...
oldMessageDict = {}
oldMessageDict2 = {}
allMessagesMatched = True
if not txt.strip():
newmark = ''
newlines = (
'# -*- coding: utf-8 -*-\n'
'\n'
'# This file is used to translate the messages used in the AvsPmod interface.\n'
'# To use it, make sure it is named "translation_lng.py" where "lng" is the \n'
'# three-letter code corresponding to the language that is translated to \n'
'# (see <http://www.loc.gov/standards/iso639-2/php/code_list.php>), \n'
'# and is placed in the "%s" subdirectory.\n' % os.path.basename(dir) +
'# \n'
'# Simply add translated messages next to each message (any untranslated \n'
'# messages will be shown in English). You can type unicode text directly \n'
'# into this document - if you do, make sure to save it in the appropriate \n'
'# format. If required, you can change the coding on the first line of this \n'
'# document to a coding appropriate for your translated language. DO NOT \n'
'# touch line breaks (\\n) and any words inside formatted strings (ie, any \n'
'# portions of the text which look like {...}, %(...)s, %(...)i, etc.)\n'
).split('\n')
extra_txt = None
else:
newlines = []
boolStartLines = True
re_mark = re.compile(r'(.*(?<![\'"])[\'"],)\s*#')
txt, extra_sep, extra_txt = txt.partition('\n #--- Macros - Extra ---#\n')
for line in txt.split('\n'):
# Copy the start lines
if line.strip() and not line.lstrip('\xef\xbb\xbf').strip().startswith('#'):
boolStartLines = False
if boolStartLines:
newlines.append(line)
# Get the key
splitline = line.split(' : ', 1)
if len(splitline) == 2:
key = splitline[0].strip()
match = re_mark.match(line)
oldMessageDict[key] = match.group(1) if match else line
# Heuristically add extra keys for similar enough messages
if splitline[1].strip().startswith('u"'):
rawkey = key.strip('"')
splitvalue = splitline[1].strip().split('"')
if rawkey.endswith(':'):
key2 = '"%s"' % rawkey.rstrip(':')
splitvalue[1] = splitvalue[1].rstrip(':')
line2 = ' ' + key2 + ' : ' + '"'.join(splitvalue)
oldMessageDict2[key2] = line2
elif rawkey.endswith(' *'):
key2 = '"%s"' % rawkey.rstrip(' *')
splitvalue[1] = splitvalue[1].rstrip(' *')
line2 = ' ' + key2 + ' : ' + '"'.join(splitvalue)
oldMessageDict2[key2] = line2
elif rawkey and (rawkey[0].isspace() or rawkey[-1].isspace()):
key2 = '"%s"' % rawkey.strip()
splitvalue[1] = splitvalue[1].strip()
line2 = ' ' + key2 + ' : ' + '"'.join(splitvalue)
oldMessageDict2[key2] = line2
# Get the new translation strings and complete the new text
curlines = new_translation_string.split('\n')
for line in curlines:
splitline = line.split(' : ')
if line.startswith(' ') and len(splitline) > 1:
key = splitline[0].strip()
if key in oldMessageDict:
newlines.append(oldMessageDict[key])
elif key in oldMessageDict2:
newlines.append(oldMessageDict2[key])
else:
newlines.append(line+newmark)
allMessagesMatched = False
else:
newlines.append(line)
if extra_txt:
newlines[-1] = extra_txt
# Overwrite the text file with the new data
f = open(filename, 'w')
f.write('\n'.join(newlines))
f.close()
else:
allMessagesMatched = True
return not allMessagesMatched
def display_name(code):
"""Return the native language name of an ISO 639-2 language code"""
return language_names.get(code, code)
language_names = {
"aar": u"Qafara",
"abk": u"Аҧсуа",
"ave": u"avesta",
"afr": u"Afrikaans",
"aka": u"akana",
"amh": u"አማርኛ",
"arg": u"aragonés",
"ara": u"العربية",
"asm": u"অসমীয়া",
"ava": u"авар мацӀ",
"aym": u"aymar aru",
"aze": u"Azərbaycanca",
"bak": u"башҡорт теле",
"bel": u"Беларуская мова",
"bul": u"български език",
"bih": u"भोजपुरी",
"bis": u"Bislama",
"bam": u"bamanankan",
"ben": u"বাংলা",
"tib": u"བོད་ཡིག",
"bre": u"brezhoneg",
"bos": u"bosanski jezik",
"cat": u"català",
"che": u"нохчийн мотт",
"cha": u"Chamoru",
"cos": u"corsu",
"cre": u"ᓀᐦᐃᔭᐍᐏᐣ",
"cze": u"čeština",
"chu": u"ѩзыкъ словѣньскъ",
"chv": u"чӑваш чӗлхи",
"wel": u"Cymraeg",
"dan": u"dansk",
"ger": u"Deutsch",
"div": u"ދިވެހިބަސ",
"dzo": u"རྫོང་ཁ",
"ewe": u"Ɛʋɛgbɛ",
"gre": u"Ελληνικά",
"eng": u"English",
"epo": u"Esperanto",
"spa": u"español",
"est": u"eesti keel",
"baq": u"euskara",
"per": u"فارسی",
"ful": u"Fulfulde",
"fin": u"suomi",
"fij": u"vosa Vakaviti",
"fao": u"føroyskt",
"fra": u"français",
"fre": u"français",
"fry": u"frysk",
"gle": u"Gaeilge",
"gla": u"Gàidhlig",
"glg": u"Galego",
"grn": u"Avañe'ẽ",
"guj": u"ગુજરાતી",
"glv": u"Gaelg",
"hau": u"Hausancī",
"heb": u"עִבְרִית",
"hin": u"हिन्दी",
"hmo": u"Hiri Motu",
"hrv": u"hrvatski jezik",
"hat": u"Kreyòl ayisyen",
"hun": u"magyar",
"arm": u"Հայերեն լեզու",
"her": u"Otjiherero",
"ina": u"interlingua",
"ind": u"Bahasa Indonesia",
"ile": u"Interlingue",
"ibo": u"Igbo",
"iii": u"ꆇꉙ",
"ipk": u"Iñupiaq",
"ido": u"Ido",
"ice": u"íslenska",
"ita": u"italiano",
"iku": u"ᐃᓄᒃᑎᑐᑦ",
"jpn": u"日本語",
"jav": u"basa Jawa (ꦧꦱꦗꦮ)",
"geo": u"ქართული ენა (kartuli ena)",
"kon": u"Kikongo",
"kik": u"Gĩkũyũ",
"kua": u"kuanyama",
"kaz": u"Қазақ тілі",
"kal": u"kalaallisut",
"khm": u"ភាសាខ្មែរ",
"kan": u"ಕನ್ನಡ",
"kor": u"한국어 (韓國語)",
"kau": u"kanuri",
"kas": u"कॉशुर",
"kur": u"Kurdî",
"kom": u"коми кыв",
"cor": u"Kernewek",
"kir": u"кыргыз тили",
"lat": u"latine",
"ltz": u"Lëtzebuergesch",
"lug": u"Luganda",
"lim": u"Limburgs",
"lin": u"lingala",
"lao": u"ພາສາລາວ",
"lit": u"lietuvių kalba",
"lub": u"",
"lav": u"latviešu valoda",
"mlg": u"Malagasy fiteny",
"mah": u"Kajin M̧ajeļ",
"mao": u"te reo Māori",
"mac": u"македонски јазик",
"mal": u"മലയാളം",
"mon": u"монгол хэл",
"mar": u"मराठी",
"may": u"bahasa Melayu",
"mlt": u"Malti",
"bur": u"မြန်မာစာ",
"nau": u"Ekakairũ Naoero",
"nob": u"bokmål",
"nde": u"isiNdebele",
"nep": u"नेपाली",
"ndo": u"Owambo",
"dut": u"Nederlands",
"nno": u"nynorsk",
"nor": u"norsk",
"nbl": u"isiNdebele",
"nav": u"Diné bizaad",
"nya": u"chiCheŵa",
"oci": u"Occitan",
"oji": u"ᐊᓂᔑᓇᐯᒧᐏᐣ (Anishinaabemowin)",
"orm": u"Afaan Oromoo",
"ori": u"ଓଡ଼ିଆ",
"oss": u"ирон ӕвзаг",
"pan": u"ਪੰਜਾਬੀ",
"pli": u"पालि",
"pol": u"polski",
"pus": u"پښتو",
"por": u"português",
"que": u"Runa Simi",
"roh": u"rumantsch grischun",
"run": u"Rundi",
"rum": u"română",
"rus": u"русский язык",
"kin": u"Ikinyarwanda",
"san": u"संस्कृतम्",
"srd": u"sardu",
"snd": u"سنڌي، سندھی",
"sme": u"sámi",
"sag": u"yângâ tî sängö",
"sin": u"සිංහල",
"slo": u"slovenčina",
"slv": u"slovenščina",
"smo": u"gagana fa'a Samoa",
"sna": u"chiShona",
"som": u"Soomaaliga",
"alb": u"Shqip",
"srp": u"српски језик",
"ssw": u"siSwati",
"sot": u"Sesotho",
"sun": u"basa Sunda",
"swe": u"svenska",
"swa": u"Kiswahili",
"tam": u"தமிழ்",
"tel": u"తెలుగు",
"tgk": u"тоҷикӣ",
"tha": u"ภาษาไทย",
"tir": u"ትግርኛ",
"tuk": u"Түркмен",
"tgl": u"Wikang Tagalog",
"tsn": u"Setswana",
"ton": u"faka-Tonga",
"tur": u"Türkçe",
"tso": u"Xitsonga",
"tat": u"татарча",
"twi": u"Twi",
"tah": u"te reo Tahiti",
"uig": u"Uyƣurqə",
"ukr": u"українська мова",
"urd": u"اردو",
"uzb": u"O'zbek",
"ven": u"Tshivenḓa",
"vie": u"Tiếng Việt",
"vol": u"Volapük",
"wln": u"walon",
"wol": u"Wolof",
"xho": u"isiXhosa",
"yid": u"ייִדיש",
"yor": u"Yorùbá",
"zha": u"Saɯ cueŋƅ",
"chi": u"漢語",
"zul": u"isiZulu",
}
if __name__ == '__main__':
main()