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
23 """convert Gettext PO localization files to Java/Mozilla .properties files
25 see: http://translate.sourceforge.net/wiki/toolkit/po2prop for examples and
29 from translate
.misc
import quote
30 from translate
.storage
import po
35 def __init__(self
, templatefile
):
36 self
.templatefile
= templatefile
39 def convertstore(self
, inputstore
, personality
, includefuzzy
=False):
40 self
.personality
= personality
41 self
.inmultilinemsgid
= 0
43 self
.makestoredict(inputstore
, includefuzzy
)
45 for line
in self
.templatefile
.readlines():
46 outputstr
= self
.convertline(line
)
47 outputlines
.append(outputstr
)
50 def makestoredict(self
, store
, includefuzzy
=False):
51 # make a dictionary of the translations
52 for unit
in store
.units
:
53 if includefuzzy
or not unit
.isfuzzy():
54 # there may be more than one entity due to msguniq merge
55 for entity
in unit
.getlocations():
56 propstring
= unit
.target
58 # NOTE: triple-space as a string means leave it empty (special signal)
59 if len(propstring
.strip()) == 0 and propstring
!= " ":
60 propstring
= unit
.source
61 self
.inputdict
[entity
] = propstring
63 def convertline(self
, line
):
65 # handle multiline msgid if we're in one
66 if self
.inmultilinemsgid
:
67 msgid
= quote
.rstripeol(line
).strip()
69 self
.inmultilinemsgid
= (msgid
[-1:] == '\\')
73 # otherwise, this could be a comment
74 elif line
.strip()[:1] == '#':
75 returnline
= quote
.rstripeol(line
)+eol
77 line
= quote
.rstripeol(line
)
78 equalspos
= line
.find('=')
79 # if no equals, just repeat it
81 returnline
= quote
.rstripeol(line
)+eol
82 # otherwise, this is a definition
84 # backslash at end means carry string on to next line
85 if quote
.rstripeol(line
)[-1:] == '\\':
86 self
.inmultilinemsgid
= 1
87 # now deal with the current string...
88 key
= line
[:equalspos
].strip()
89 # Calculate space around the equal sign
90 prespace
= line
.lstrip()[line
.lstrip().find(' '):equalspos
]
91 postspacestart
= len(line
[equalspos
+1:])
92 postspaceend
= len(line
[equalspos
+1:].lstrip())
93 postspace
= line
[equalspos
+1:equalspos
+(postspacestart
-postspaceend
)+1]
94 if self
.inputdict
.has_key(key
):
96 value
= self
.inputdict
[key
]
97 if isinstance(value
, str):
98 value
= value
.decode('utf8')
99 if self
.personality
== "mozilla":
100 returnline
= key
+prespace
+"="+postspace
+quote
.mozillapropertiesencode(value
)+eol
102 returnline
= key
+prespace
+"="+postspace
+quote
.javapropertiesencode(value
)+eol
105 returnline
= line
+eol
106 if isinstance(returnline
, unicode):
107 returnline
= returnline
.encode('utf-8')
110 def convertmozillaprop(inputfile
, outputfile
, templatefile
, includefuzzy
=False):
111 """Mozilla specific convertor function"""
112 return convertprop(inputfile
, outputfile
, templatefile
, personality
="mozilla", includefuzzy
=includefuzzy
)
114 def convertprop(inputfile
, outputfile
, templatefile
, personality
, includefuzzy
=False):
115 inputstore
= po
.pofile(inputfile
)
116 if templatefile
is None:
117 raise ValueError("must have template file for properties files")
118 # convertor = po2prop()
120 convertor
= reprop(templatefile
)
121 outputproplines
= convertor
.convertstore(inputstore
, personality
, includefuzzy
)
122 outputfile
.writelines(outputproplines
)
126 # handle command line options
127 from translate
.convert
import convert
128 formats
= {("po", "properties"): ("properties", convertprop
)}
129 parser
= convert
.ConvertOptionParser(formats
, usetemplates
=True, description
=__doc__
)
130 parser
.add_option("", "--personality", dest
="personality", default
="java", type="choice",
131 choices
=["java", "mozilla"],
132 help="set the output behaviour: java (default), mozilla", metavar
="TYPE")
133 parser
.add_fuzzy_option()
134 parser
.passthrough
.append("personality")
137 if __name__
== '__main__':