2 # -*- coding: utf-8 -*-
4 # Copyright 2002-2006 Zuza Software Foundation
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
29 from translate
.storage
import po
30 from translate
.storage
import php
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
)
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
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")
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
)
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
= ""
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
)
89 if templatefile
is None:
90 outputstore
= convertor
.convertstore(inputstore
, duplicatestyle
=duplicatestyle
)
92 templatestore
= php
.phpfile(templatefile
)
93 outputstore
= convertor
.mergestore(templatestore
, inputstore
, blankmsgstr
=pot
, duplicatestyle
=duplicatestyle
)
94 if outputstore
.isempty():
96 outputfile
.write(str(outputstore
))
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")
107 if __name__
== '__main__':