1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 21   
 22   
 23  """convert Gettext PO localization files to PHP localization files 
 24   
 25  see: http://translate.sourceforge.net/wiki/toolkit/po2php for examples and  
 26  usage instructions 
 27  """ 
 28   
 29  from translate.misc import quote 
 30  from translate.storage import po 
 31  from translate.storage import php 
 32   
 33  eol = "\n" 
 34   
 37          self.templatefile = templatefile 
 38          self.inputdict = {} 
  39   
 49   
 51          '''make a dictionary of the translations''' 
 52          for unit in store.units: 
 53              if includefuzzy or not unit.isfuzzy(): 
 54                  for location in unit.getlocations(): 
 55                      inputstring = unit.target 
 56                      if len(inputstring.strip()) == 0: 
 57                          inputstring = unit.source 
 58                      self.inputdict[location] = inputstring 
  59   
 61          returnline = "" 
 62           
 63          if self.inmultilinemsgid: 
 64              msgid = quote.rstripeol(line).strip() 
 65               
 66              endpos = line.rfind("%s;" % self.quotechar) 
 67               
 68              if endpos >= 0 and line[endpos-1] != '\\': 
 69                  self.inmultilinemsgid = False 
 70               
 71              if self.inecho: 
 72                  returnline = line 
 73           
 74          elif line.strip()[:2] == '//' or line.strip()[:2] == '/*': 
 75              returnline = quote.rstripeol(line)+eol 
 76          else: 
 77              line = quote.rstripeol(line) 
 78              equalspos = line.find('=') 
 79               
 80              if equalspos == -1: 
 81                  returnline = quote.rstripeol(line)+eol 
 82               
 83              else: 
 84                   
 85                  key = line[:equalspos].strip() 
 86                  lookupkey = key.replace(" ", "") 
 87                   
 88                  prespace = line.lstrip()[line.lstrip().find(']')+1:equalspos] 
 89                  postspacestart = len(line[equalspos+1:]) 
 90                  postspaceend = len(line[equalspos+1:].lstrip()) 
 91                  postspace = line[equalspos+1:equalspos+(postspacestart-postspaceend)+1] 
 92                  self.quotechar = line[equalspos+(postspacestart-postspaceend)+1] 
 93                  print key 
 94                  if self.inputdict.has_key(lookupkey): 
 95                      self.inecho = 0 
 96                      value = php.phpencode(self.inputdict[lookupkey], self.quotechar) 
 97                      if isinstance(value, str): 
 98                          value = value.decode('utf8') 
 99                      returnline = key + prespace + "=" + postspace + self.quotechar + value + self.quotechar + ';' + eol 
100                  else: 
101                      self.inecho = 1 
102                      returnline = line+eol 
103                   
104                  endpos = line.rfind("%s;" % self.quotechar) 
105                   
106                  if endpos == -1 or line[endpos-1] == '\\': 
107                      self.inmultilinemsgid = True 
108          if isinstance(returnline, unicode): 
109              returnline = returnline.encode('utf-8') 
110          return returnline 
 112 -def convertphp(inputfile, outputfile, templatefile, includefuzzy=False): 
 113      inputstore = po.pofile(inputfile) 
114      if templatefile is None: 
115          raise ValueError("must have template file for php files") 
116           
117      else: 
118          convertor = rephp(templatefile) 
119      outputphplines = convertor.convertstore(inputstore, includefuzzy) 
120      outputfile.writelines(outputphplines) 
121      return 1 
 122   
123 -def main(argv=None): 
 124       
125      from translate.convert import convert 
126      formats = {("po", "php"): ("php", convertphp)} 
127      parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__) 
128      parser.add_fuzzy_option() 
129      parser.run(argv) 
 130   
131  if __name__ == '__main__': 
132      main() 
133