Kiln » TortoiseHg » TortoiseHg
Clone URL:  
reggen.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
# reggen.py - registry file generator for Windows shell context menus # # Copyright 2009 Yuki KODAMA <endflow.net@gmail.com> # # This software may be used and distributed according to the terms of the # GNU General Public License version 2, incorporated herein by reference. import sys import os.path import glob import re import codecs # based on 'nautilus-thg.py' and 'hgtk' def _thg_path(): pfile = __file__ if pfile.endswith('.pyc'): pfile = pfile[:-1] path = os.path.dirname(os.path.dirname(os.path.realpath(pfile))) thgpath = os.path.normpath(path) testpath = os.path.join(thgpath, 'tortoisehg') if os.path.isdir(testpath) and thgpath not in sys.path: sys.path.insert(0, thgpath) _thg_path() from mercurial import demandimport demandimport.ignore.append('win32com.shell') demandimport.enable() from tortoisehg.util.menuthg import thgcmenu regkeytmpl = u'[HKEY_CURRENT_USER\\Software\\TortoiseHg\\CMenu\\%s\\%s]' regheaders = ( u'Windows Registry Editor Version 5.00', u'', u'[HKEY_CURRENT_USER\\Software\\TortoiseHg]', u'"CMenuLang"="%(lang)s"', u'', u'[HKEY_CURRENT_USER\\Software\\TortoiseHg\\CMenu]', u'', u'[HKEY_CURRENT_USER\\Software\\TortoiseHg\\CMenu\\%(lang)s]') # regex patterns used to extract strings from PO files pat_id = re.compile(u'^msgid "([^\\"]+)"') pat_str = re.compile(u'^msgstr "([^\\"]+)"') def lookup(file): def stripmsg(line, pat): m = pat.match(line) if m: return m.group(1) # acquire all translatable strings # and set fallback messages i18n = {} msgids = [] for cmenu in thgcmenu.values(): label = cmenu['label']['id'].decode('utf-8') msgids.append(label) i18n[label] = label help = cmenu['help']['id'].decode('utf-8') msgids.append(help) i18n[help] = help # lookup PO file if file: foundmsgid = False f = codecs.open(file, 'r', 'utf-8') for line in f.readlines(): line = line.rstrip(u'\r\n') if foundmsgid: msgstr = stripmsg(line, pat_str) if msgstr: i18n[msgid] = msgstr foundmsgid = False else: msgid = stripmsg(line, pat_id) if msgid and msgid in msgids: foundmsgid = True f.close() return i18n def wopen(path): newfile = codecs.open(path, 'w','utf-16-le') newfile.write(codecs.BOM_UTF16_LE.decode('utf-16-le')) def write(lines, newlines=2): if isinstance(lines, (str, unicode)): buf = lines else: buf = u'\r\n'.join(lines) buf = (buf + (u'\r\n' * newlines)) newfile.write(buf) def close(): newfile.close() return write, close # enumerate available languages langinfo = [{'code': u'en_US', 'file': None}] lang_pat = re.compile(u'([^\\.]+)\\.po$') for file in glob.glob(u'../i18n/tortoisehg/*.po'): m = lang_pat.match(os.path.basename(file)) langinfo.append({'code': m.group(1), 'file': os.path.abspath(file)}) # output REG files for lang in langinfo: write, close = wopen(u'thg-cmenu-%s.reg' % lang['code']) write([h % {'lang': lang['code']} for h in regheaders]) i18n = lookup(lang['file']) for hgcmd, cmenu in thgcmenu.items(): write(regkeytmpl % (lang['code'], hgcmd.decode('utf-8')), 1) write((u'"menuText"="%s"' % i18n[cmenu['label']['id'].decode('utf-8')], u'"helpText"="%s"' % i18n[cmenu['help']['id'].decode('utf-8')])) close()