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 Qt Linguist (.ts) files 
24   
25  see: http://translate.sourceforge.net/wiki/toolkit/po2ts for examples and  
26  usage instructions 
27  """ 
28   
29  from translate.storage import po 
30  from translate.storage import ts 
31   
34          """converts a .po file to .ts format (using a template .ts file if given)""" 
35          if templatefile is None:  
36              tsfile = ts.QtTsParser() 
37          else: 
38              tsfile = ts.QtTsParser(templatefile) 
39          for inputunit in inputstore.units: 
40              if inputunit.isheader() or inputunit.isblank(): 
41                  continue 
42              source = inputunit.source 
43              translation = inputunit.target 
44              comment = inputunit.getnotes("translator") 
45              transtype = None 
46              if not inputunit.istranslated(): 
47                  transtype = "unfinished" 
48              elif inputunit.getnotes("developer") == "(obsolete)": 
49                  transtype = "obsolete"  
50              if isinstance(source, str): 
51                  source = source.decode("utf-8") 
52              if isinstance(translation, str): 
53                  translation = translation.decode("utf-8") 
54              for sourcelocation in inputunit.getlocations(): 
55                  if "#" in sourcelocation: 
56                      contextname = sourcelocation[:sourcelocation.find("#")] 
57                  else: 
58                      contextname = sourcelocation 
59                  tsfile.addtranslation(contextname, source, translation, comment, transtype, createifmissing=True) 
60          return tsfile.getxml() 
 62 -def convertpo(inputfile, outputfile, templatefile): 
 63      """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout""" 
64      inputstore = po.pofile(inputfile) 
65      if inputstore.isempty(): 
66          return 0 
67      convertor = po2ts() 
68      outputstring = convertor.convertstore(inputstore, templatefile) 
69      outputfile.write(outputstring) 
70      return 1 
 71   
73      from translate.convert import convert 
74      formats = {"po": ("ts", convertpo), ("po", "ts"): ("ts", convertpo)} 
75      parser = convert.ConvertOptionParser(formats, usepots=False, usetemplates=True, description=__doc__) 
76      parser.run(argv) 
 77   
78   
79  if __name__ == '__main__': 
80      main() 
81