2 # -*- coding: utf-8 -*-
4 # Copyright 2005, 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 """converts funny mozilla files to properties files"""
25 from translate
.misc
import quote
26 from translate
.convert
import prop2po
27 from translate
.misc
.wStringIO
import StringIO
29 def encodepropline(line
):
30 """helper which strips off any end of line, encodes for properties file, and adds on the end of line"""
31 strippedline
= line
.rstrip("\n")
32 if line
== strippedline
:
35 ending
= line
[len(strippedline
)-len(line
):]
36 return quote
.mozillapropertiesencode(strippedline
) + ending
39 """convert a .inc file with #defines in it to a properties file"""
40 yield "# converted from #defines file\n"
42 line
= line
.decode("utf-8")
43 if line
.startswith("# "):
45 line
= line
.replace("# ", "", 1)
50 elif line
.startswith("#define"):
51 parts
= string
.split(line
.replace("#define", "", 1).strip(), maxsplit
=1)
55 key
, value
= parts
[0], ""
58 # special case: uncomment MOZ_LANGPACK_CONTRIBUTORS
59 if key
== "MOZ_LANGPACK_CONTRIBUTORS":
63 yield "%s = %s\n" % (key
, value
)
69 def it2prop(lines
, encoding
="cp1252"):
70 """convert a pseudo-properties .it file to a conventional properties file"""
71 yield "# converted from pseudo-properties .it file\n"
72 # differences: ; instead of # for comments
73 # [section] titles that we replace with # section: comments
75 line
= line
.decode(encoding
)
78 elif line
.lstrip().startswith(";"):
79 yield line
.replace(";", "#", 1)
80 elif line
.lstrip().startswith("[") and line
.rstrip().endswith("]"):
81 yield "# section: "+line
85 def funny2prop(lines
, itencoding
="cp1252"):
86 hashstarts
= len([line
for line
in lines
if line
.startswith("#")])
88 for line
in inc2prop(lines
):
89 yield encodepropline(line
)
91 for line
in it2prop(lines
, encoding
=itencoding
):
92 yield encodepropline(line
)
94 def inc2po(inputfile
, outputfile
, templatefile
, encoding
=None, pot
=False, duplicatestyle
="msgid_comment"):
95 """wraps prop2po but converts input/template files to properties first"""
96 inputlines
= inputfile
.readlines()
97 inputproplines
= [encodepropline(line
) for line
in inc2prop(inputlines
)]
98 inputpropfile
= StringIO("".join(inputproplines
))
99 if templatefile
is not None:
100 templatelines
= templatefile
.readlines()
101 templateproplines
= [encodepropline(line
) for line
in inc2prop(templatelines
)]
102 templatepropfile
= StringIO("".join(templateproplines
))
104 templatepropfile
= None
105 return prop2po
.convertprop(inputpropfile
, outputfile
, templatepropfile
, pot
=pot
, duplicatestyle
=duplicatestyle
)
107 def it2po(inputfile
, outputfile
, templatefile
, encoding
="cp1252", pot
=False, duplicatestyle
="msgid_comment"):
108 """wraps prop2po but converts input/template files to properties first"""
109 inputlines
= inputfile
.readlines()
110 inputproplines
= [encodepropline(line
) for line
in it2prop(inputlines
, encoding
=encoding
)]
111 inputpropfile
= StringIO("".join(inputproplines
))
112 if templatefile
is not None:
113 templatelines
= templatefile
.readlines()
114 templateproplines
= [encodepropline(line
) for line
in it2prop(templatelines
, encoding
=encoding
)]
115 templatepropfile
= StringIO("".join(templateproplines
))
117 templatepropfile
= None
118 return prop2po
.convertprop(inputpropfile
, outputfile
, templatepropfile
, pot
=pot
, duplicatestyle
=duplicatestyle
)
120 def ini2po(inputfile
, outputfile
, templatefile
, encoding
="UTF-8", pot
=False, duplicatestyle
="msgid_comment"):
121 return it2po(inputfile
=inputfile
, outputfile
=outputfile
, templatefile
=templatefile
, encoding
=encoding
, pot
=pot
, duplicatestyle
=duplicatestyle
)
125 lines
= sys
.stdin
.readlines()
126 for line
in funny2prop(lines
):
127 sys
.stdout
.write(line
)
129 if __name__
== "__main__":