OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 '''The 'grit menufromparts' tool.''' | |
7 | |
8 import types | |
9 | |
10 from grit import grd_reader | |
11 from grit import tclib | |
12 from grit import util | |
13 from grit import xtb_reader | |
14 from grit.tool import interface | |
15 from grit.tool import transl2tc | |
16 | |
17 import grit.extern.tclib | |
18 | |
19 | |
20 class MenuTranslationsFromParts(interface.Tool): | |
21 '''One-off tool to generate translated menu messages (where each menu is kept | |
22 in a single message) based on existing translations of the individual menu | |
23 items. Was needed when changing menus from being one message per menu item | |
24 to being one message for the whole menu.''' | |
25 | |
26 def ShortDescription(self): | |
27 return ('Create translations of whole menus from existing translations of ' | |
28 'menu items.') | |
29 | |
30 def Run(self, globopt, args): | |
31 self.SetOptions(globopt) | |
32 assert len(args) == 2, "Need exactly two arguments, the XTB file and the out
put file" | |
33 | |
34 xtb_file = args[0] | |
35 output_file = args[1] | |
36 | |
37 grd = grd_reader.Parse(self.o.input, debug=self.o.extra_verbose) | |
38 grd.OnlyTheseTranslations([]) # don't load translations | |
39 grd.RunGatherers() | |
40 | |
41 xtb = {} | |
42 def Callback(msg_id, parts): | |
43 msg = [] | |
44 for part in parts: | |
45 if part[0]: | |
46 msg = [] | |
47 break # it had a placeholder so ignore it | |
48 else: | |
49 msg.append(part[1]) | |
50 if len(msg): | |
51 xtb[msg_id] = ''.join(msg) | |
52 with open(xtb_file) as f: | |
53 xtb_reader.Parse(f, Callback) | |
54 | |
55 translations = [] # list of translations as per transl2tc.WriteTranslations | |
56 for node in grd: | |
57 if node.name == 'structure' and node.attrs['type'] == 'menu': | |
58 assert len(node.GetCliques()) == 1 | |
59 message = node.GetCliques()[0].GetMessage() | |
60 translation = [] | |
61 | |
62 contents = message.GetContent() | |
63 for part in contents: | |
64 if isinstance(part, types.StringTypes): | |
65 id = grit.extern.tclib.GenerateMessageId(part) | |
66 if id not in xtb: | |
67 print "WARNING didn't find all translations for menu %s" % node.at
trs['name'] | |
68 translation = [] | |
69 break | |
70 translation.append(xtb[id]) | |
71 else: | |
72 translation.append(part.GetPresentation()) | |
73 | |
74 if len(translation): | |
75 translations.append([message.GetId(), ''.join(translation)]) | |
76 | |
77 with util.WrapOutputStream(open(output_file, 'w')) as f: | |
78 transl2tc.TranslationToTc.WriteTranslations(f, translations) | |
79 | |
OLD | NEW |