fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / convert / php2po.py
blob093d56677516a3b7b2fdf859a7c2caf56fe15f9b
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2002-2006 Zuza Software Foundation
5 #
6 # This file is part of translate.
8 # translate is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # translate is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with translate; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 """convert PHP localization files to Gettext PO localization files
24 See: http://translate.sourceforge.net/wiki/toolkit/php2po for examples and
25 usage instructions
26 """
28 import sys
29 from translate.storage import po
30 from translate.storage import php
32 class php2po:
33 """convert a .php file to a .po file for handling the translation..."""
34 def convertstore(self, inputstore, duplicatestyle="msgctxt"):
35 """converts a .php file to a .po file..."""
36 outputstore = po.pofile()
37 outputheader = outputstore.makeheader(charset="UTF-8", encoding="8bit")
38 outputheader.addnote("extracted from %s" % inputstore.filename, "developer")
39 outputstore.addunit(outputheader)
40 for inputunit in inputstore.units:
41 outputunit = self.convertunit(inputunit, "developer")
42 if outputunit is not None:
43 outputstore.addunit(outputunit)
44 outputstore.removeduplicates(duplicatestyle)
45 return outputstore
47 def mergestore(self, templatestore, inputstore, blankmsgstr=False, duplicatestyle="msgctxt"):
48 """converts two .properties files to a .po file..."""
49 outputstore = po.pofile()
50 outputheader = outputstore.makeheader(charset="UTF-8", encoding="8bit")
51 outputheader.addnote("extracted from %s, %s" % (templatestore.filename, inputstore.filename), "developer")
52 outputstore.addunit(outputheader)
53 inputstore.makeindex()
54 # we try and merge the header po with any comments at the start of the properties file
55 appendedheader = 0
56 # loop through the original file, looking at units one by one
57 for templateunit in templatestore.units:
58 outputunit = self.convertunit(templateunit, "developer")
59 # try and find a translation of the same name...
60 if templateunit.name in inputstore.locationindex:
61 translatedinputunit = inputstore.locationindex[templateunit.name]
62 # Need to check that this comment is not a copy of the developer comments
63 translatedoutputunit = self.convertunit(translatedinputunit, "translator")
64 else:
65 translatedoutputunit = None
66 # if we have a valid po unit, get the translation and add it...
67 if outputunit is not None:
68 if translatedoutputunit is not None and not blankmsgstr:
69 outputunit.target = translatedoutputunit.source
70 outputstore.addunit(outputunit)
71 elif translatedoutputunit is not None:
72 print >> sys.stderr, "error converting original properties definition %s" % templateunit.name
73 outputstore.removeduplicates(duplicatestyle)
74 return outputstore
76 def convertunit(self, inputunit, origin):
77 """Converts a .php unit to a .po unit"""
78 outputunit = po.pounit(encoding="UTF-8")
79 outputunit.addnote(inputunit.getnotes(origin), origin)
80 outputunit.addlocation("".join(inputunit.getlocations()))
81 outputunit.source = inputunit.source
82 outputunit.target = ""
83 return outputunit
85 def convertphp(inputfile, outputfile, templatefile, pot=False, duplicatestyle="msgctxt"):
86 """reads in inputfile using php, converts using php2po, writes to outputfile"""
87 inputstore = php.phpfile(inputfile)
88 convertor = php2po()
89 if templatefile is None:
90 outputstore = convertor.convertstore(inputstore, duplicatestyle=duplicatestyle)
91 else:
92 templatestore = php.phpfile(templatefile)
93 outputstore = convertor.mergestore(templatestore, inputstore, blankmsgstr=pot, duplicatestyle=duplicatestyle)
94 if outputstore.isempty():
95 return 0
96 outputfile.write(str(outputstore))
97 return 1
99 def main(argv=None):
100 from translate.convert import convert
101 formats = {"php": ("po", convertphp), ("php", "php"): ("po", convertphp)}
102 parser = convert.ConvertOptionParser(formats, usetemplates=True, usepots=True, description=__doc__)
103 parser.add_duplicates_option()
104 parser.passthrough.append("pot")
105 parser.run(argv)
107 if __name__ == '__main__':
108 main()