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