1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 21   
 22  """Insert debug messages into XLIFF and Gettex PO localization files 
 23   
 24  See: http://translate.sourceforge.net/wiki/toolkit/podebug for examples and 
 25  usage instructions 
 26  """ 
 27   
 28  from translate.storage import factory 
 29  import os 
 30  import re 
 31  import md5 
 32   
 34 -    def __init__(self, format=None, rewritestyle=None, hash=None, ignoreoption=None): 
  35          if format is None: 
 36              self.format = "" 
 37          else: 
 38              self.format = format 
 39          self.rewritefunc = getattr(self, "rewrite_%s" % rewritestyle, None) 
 40          self.ignorefunc = getattr(self, "ignore_%s" % ignoreoption, None) 
 41          self.hash = hash 
  42   
 44          return "xxx%sxxx" % string 
  45   
 48   
 51   
 53          for location in locations: 
 54              if location.startswith("Common.xcu#..Common.View.Localisation"): 
 55                  return True 
 56              elif location.startswith("profile.lng#STR_DIR_MENU_NEW_"): 
 57                  return True 
 58              elif location.startswith("profile.lng#STR_DIR_MENU_WIZARD_"): 
 59                  return True 
 60          return False 
  61   
 63          if len(locations) == 1 and locations[0].lower().endswith(".accesskey"): 
 64              return True 
 65          for location in locations: 
 66              if location.endswith(".height") or location.endswith(".width") or \ 
 67                      location.endswith(".macWidth") or location.endswith(".unixWidth"): 
 68                  return True 
 69              if location == "brandShortName" or location == "brandFullName" or location == "vendorShortName": 
 70                  return True 
 71              if location.lower().endswith(".commandkey") or location.endswith(".key"): 
 72                  return True 
 73          return False 
  74   
 97   
 99          filename = self.shrinkfilename(store.filename) 
100          prefix = self.format 
101          for formatstr in re.findall("%[0-9c]*[sfFbBd]", self.format): 
102              if formatstr.endswith("s"): 
103                  formatted = self.shrinkfilename(store.filename) 
104              elif formatstr.endswith("f"): 
105                  formatted = store.filename 
106                  formatted = os.path.splitext(formatted)[0] 
107              elif formatstr.endswith("F"): 
108                  formatted = store.filename 
109              elif formatstr.endswith("b"): 
110                  formatted = os.path.basename(store.filename) 
111                  formatted = os.path.splitext(formatted)[0] 
112              elif formatstr.endswith("B"): 
113                  formatted = os.path.basename(store.filename) 
114              elif formatstr.endswith("d"): 
115                  formatted = os.path.dirname(store.filename) 
116              else: 
117                  continue 
118              formatoptions = formatstr[1:-1] 
119              if formatoptions: 
120                  if "c" in formatoptions and formatted: 
121                      formatted = formatted[0] + filter(lambda x: x.lower() not in "aeiou", formatted[1:]) 
122                  length = filter(str.isdigit, formatoptions) 
123                  if length: 
124                      formatted = formatted[:int(length)] 
125              prefix = prefix.replace(formatstr, formatted) 
126          for unit in store.units: 
127              if unit.isheader() or unit.isblank(): 
128                  continue 
129              unit = self.convertunit(unit, prefix) 
130          return store 
 131   
133          if filename.startswith("." + os.sep): 
134              filename = filename.replace("." + os.sep, "", 1) 
135          dirname = os.path.dirname(filename) 
136          dirparts = dirname.split(os.sep) 
137          if not dirparts: 
138              dirshrunk = "" 
139          else: 
140              dirshrunk = dirparts[0][:4] + "-" 
141              if len(dirparts) > 1: 
142                  dirshrunk += "".join([dirpart[0] for dirpart in dirparts[1:]]) + "-" 
143          baseshrunk = os.path.basename(filename)[:4] 
144          if "." in baseshrunk: 
145              baseshrunk = baseshrunk[:baseshrunk.find(".")] 
146          return dirshrunk + baseshrunk 
 148 -def convertpo(inputfile, outputfile, templatefile, format=None, rewritestyle=None, hash=None, ignoreoption=None): 
 149      """reads in inputfile using po, changes to have debug strings, writes to outputfile""" 
150       
151      inputstore = factory.getobject(inputfile) 
152      if inputstore.isempty(): 
153          return 0 
154      convertor = podebug(format=format, rewritestyle=rewritestyle, hash=hash, ignoreoption=ignoreoption) 
155      outputstore = convertor.convertstore(inputstore) 
156      outputfile.write(str(outputstore)) 
157      return 1 
 158   
160      from translate.convert import convert 
161      formats = {"po":("po", convertpo), "xlf":("xlf", convertpo)} 
162      parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__) 
163       
164      parser.add_option("-f", "--format", dest="format", default="[%s] ", help="specify format string") 
165      rewritestylelist = ["xxx", "en", "blank"] 
166      parser.add_option("", "--rewrite", dest="rewritestyle",  
167          type="choice", choices=rewritestylelist, metavar="STYLE", help="the translation rewrite style: %s" % ", ".join(rewritestylelist)) 
168      ignoreoptionlist = ["openoffice", "mozilla"] 
169      parser.add_option("", "--ignore", dest="ignoreoption",  
170          type="choice", choices=ignoreoptionlist, metavar="APPLICATION", help="apply tagging ignore rules for the given application: %s" % ", ".join(ignoreoptionlist)) 
171      parser.add_option("", "--hash", dest="hash", metavar="LENGTH", type="int", help="add an md5 hash to translations") 
172      parser.passthrough.append("format") 
173      parser.passthrough.append("rewritestyle") 
174      parser.passthrough.append("ignoreoption") 
175      parser.passthrough.append("hash") 
176      parser.run() 
 177   
178   
179  if __name__ == '__main__': 
180      main() 
181