1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 21   
 22  """convert Gettext PO localization files to Mozilla .dtd and .properties files 
 23   
 24  see: http://translate.sourceforge.net/wiki/toolkit/po2moz for examples and  
 25  usage instructions 
 26  """ 
 27   
 28  import os.path 
 29  from translate.convert import po2dtd 
 30  from translate.convert import po2prop 
 31  from translate.convert import po2html 
 32  from translate.convert import prop2mozfunny 
 33  from translate.storage import xpi 
 34  from translate.convert import convert 
 35   
 37 -    def __init__(self, formats, usetemplates=False, usepots=False, description=None): 
  39   
 41          """creates an outputarchive if required""" 
 42          if options.output and self.isarchive(options.output, 'output'): 
 43              newlang = None 
 44              newregion = None 
 45              if options.locale is not None: 
 46                  if options.locale.count("-") > 1: 
 47                      raise ValueError("Invalid locale: %s - should be of the form xx-YY" % options.locale) 
 48                  elif "-" in options.locale: 
 49                      newlang, newregion = options.locale.split("-") 
 50                  else: 
 51                      newlang, newregion = options.locale, "" 
 52              if options.clonexpi is not None: 
 53                  originalxpi = xpi.XpiFile(options.clonexpi, "r") 
 54                  options.outputarchive = originalxpi.clone(options.output, "w", newlang=newlang, newregion=newregion) 
 55              elif self.isarchive(options.template, 'template'): 
 56                  options.outputarchive = options.templatearchive.clone(options.output, "a", newlang=newlang, newregion=newregion) 
 57              else: 
 58                  if os.path.exists(options.output): 
 59                      options.outputarchive = xpi.XpiFile(options.output, "a", locale=newlang, region=newregion) 
 60                  else: 
 61                       
 62                      options.outputarchive = xpi.XpiFile(options.output, "w", locale=newlang, region=newregion) 
  63   
 65          """splits a inputpath into name and extension""" 
 66           
 67          d, n = os.path.dirname(inputpath), os.path.basename(inputpath) 
 68          s = n.find(".") 
 69          if s == '-1': 
 70              return (inputpath, "") 
 71          root = os.path.join(d, n[:s]) 
 72          ext = n[s+1:] 
 73          return (root, ext) 
  74   
 86       
 87      formats = {("dtd.po", "dtd"): ("dtd", po2dtd.convertdtd), 
 88                 ("properties.po", "properties"): ("properties", po2prop.convertmozillaprop), 
 89                 ("xhtml.po", "xhtml"): ("xhtml", po2html.converthtml), 
 90                 ("html.po", "html"): ("html", po2html.converthtml), 
 91                 ("it.po", "it"): ("it", prop2mozfunny.po2it), 
 92                 ("ini.po", "ini"): ("ini", prop2mozfunny.po2ini), 
 93                 ("inc.po", "inc"): ("inc", prop2mozfunny.po2inc), 
 94                  
 95                 ("*", "*"): ("*", convert.copyinput), 
 96                 "*": ("*", convert.copyinput)} 
 97       
 98      replacer = convert.Replacer("${locale}", None) 
 99      for replaceformat in ("js", "rdf", "manifest"): 
100          formats[(None, replaceformat)] = (replaceformat, replacer.searchreplacetemplate) 
101          formats[(replaceformat, replaceformat)] = (replaceformat, replacer.searchreplaceinput) 
102          formats[replaceformat] = (replaceformat, replacer.searchreplaceinput) 
103      parser = MozConvertOptionParser(formats, usetemplates=True, description=__doc__) 
104      parser.add_option("-l", "--locale", dest="locale", default=None, 
105          help="set output locale (required as this sets the directory names)", metavar="LOCALE") 
106      parser.add_option("", "--clonexpi", dest="clonexpi", default=None, 
107          help="clone xpi structure from the given xpi file") 
108      parser.add_fuzzy_option() 
109      parser.replacer = replacer 
110      parser.run(argv) 
 111   
112   
113  if __name__ == '__main__': 
114      main() 
115