2 # -*- coding: utf-8 -*-
4 # Copyright 2004-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 """Insert debug messages into XLIFF and Gettex PO localization files
24 See: http://translate.sourceforge.net/wiki/toolkit/podebug for examples and
28 from translate
.storage
import factory
34 def __init__(self
, format
=None, rewritestyle
=None, hash=None, ignoreoption
=None):
39 self
.rewritefunc
= getattr(self
, "rewrite_%s" % rewritestyle
, None)
40 self
.ignorefunc
= getattr(self
, "ignore_%s" % ignoreoption
, None)
43 def rewrite_xxx(self
, string
):
44 return "xxx%sxxx" % string
46 def rewrite_en(self
, string
):
49 def rewrite_blank(self
, string
):
52 def ignore_openoffice(self
, locations
):
53 for location
in locations
:
54 if location
.startswith("Common.xcu#..Common.View.Localisation"):
56 elif location
.startswith("profile.lng#STR_DIR_MENU_NEW_"):
58 elif location
.startswith("profile.lng#STR_DIR_MENU_WIZARD_"):
62 def ignore_mozilla(self
, locations
):
63 if len(locations
) == 1 and locations
[0].lower().endswith(".accesskey"):
65 for location
in locations
:
66 if location
.endswith(".height") or location
.endswith(".width") or \
67 location
.endswith(".macWidth") or location
.endswith(".unixWidth"):
69 if location
== "brandShortName" or location
== "brandFullName" or location
== "vendorShortName":
71 if location
.lower().endswith(".commandkey") or location
.endswith(".key"):
75 def convertunit(self
, unit
, prefix
):
77 if self
.ignorefunc(unit
.getlocations()):
80 if unit
.getlocations():
81 hashable
= unit
.getlocations()[0]
83 hashable
= unit
.source
84 prefix
= md5
.new(hashable
).hexdigest()[:self
.hash] + " "
86 unit
.target
= self
.rewritefunc(unit
.source
)
87 elif not unit
.istranslated():
88 unit
.target
= unit
.source
90 strings
= unit
.target
.strings
91 for i
, string
in enumerate(strings
):
92 strings
[i
] = prefix
+ string
95 unit
.target
= prefix
+ unit
.target
98 def convertstore(self
, store
):
99 filename
= self
.shrinkfilename(store
.filename
)
101 for formatstr
in re
.findall("%[0-9c]*[sfFbBd]", self
.format
):
102 if formatstr
.endswith("s"):
103 formatted
= self
.shrinkfilename(store
.filename
)
104 elif formatstr
.endswith("f"):
105 formatted
= store
.filename
106 formatted
= os
.path
.splitext(formatted
)[0]
107 elif formatstr
.endswith("F"):
108 formatted
= store
.filename
109 elif formatstr
.endswith("b"):
110 formatted
= os
.path
.basename(store
.filename
)
111 formatted
= os
.path
.splitext(formatted
)[0]
112 elif formatstr
.endswith("B"):
113 formatted
= os
.path
.basename(store
.filename
)
114 elif formatstr
.endswith("d"):
115 formatted
= os
.path
.dirname(store
.filename
)
118 formatoptions
= formatstr
[1:-1]
120 if "c" in formatoptions
and formatted
:
121 formatted
= formatted
[0] + filter(lambda x
: x
.lower() not in "aeiou", formatted
[1:])
122 length
= filter(str.isdigit
, formatoptions
)
124 formatted
= formatted
[:int(length
)]
125 prefix
= prefix
.replace(formatstr
, formatted
)
126 for unit
in store
.units
:
127 if unit
.isheader() or unit
.isblank():
129 unit
= self
.convertunit(unit
, prefix
)
132 def shrinkfilename(self
, filename
):
133 if filename
.startswith("." + os
.sep
):
134 filename
= filename
.replace("." + os
.sep
, "", 1)
135 dirname
= os
.path
.dirname(filename
)
136 dirparts
= dirname
.split(os
.sep
)
140 dirshrunk
= dirparts
[0][:4] + "-"
141 if len(dirparts
) > 1:
142 dirshrunk
+= "".join([dirpart
[0] for dirpart
in dirparts
[1:]]) + "-"
143 baseshrunk
= os
.path
.basename(filename
)[:4]
144 if "." in baseshrunk
:
145 baseshrunk
= baseshrunk
[:baseshrunk
.find(".")]
146 return dirshrunk
+ baseshrunk
148 def convertpo(inputfile
, outputfile
, templatefile
, format
=None, rewritestyle
=None, hash=None, ignoreoption
=None):
149 """reads in inputfile using po, changes to have debug strings, writes to outputfile"""
150 # note that templatefile is not used, but it is required by the converter...
151 inputstore
= factory
.getobject(inputfile
)
152 if inputstore
.isempty():
154 convertor
= podebug(format
=format
, rewritestyle
=rewritestyle
, hash=hash, ignoreoption
=ignoreoption
)
155 outputstore
= convertor
.convertstore(inputstore
)
156 outputfile
.write(str(outputstore
))
160 from translate
.convert
import convert
161 formats
= {"po":("po", convertpo
), "xlf":("xlf", convertpo
)}
162 parser
= convert
.ConvertOptionParser(formats
, usepots
=True, description
=__doc__
)
163 # TODO: add documentation on format strings...
164 parser
.add_option("-f", "--format", dest
="format", default
="[%s] ", help="specify format string")
165 rewritestylelist
= ["xxx", "en", "blank"]
166 parser
.add_option("", "--rewrite", dest
="rewritestyle",
167 type="choice", choices
=rewritestylelist
, metavar
="STYLE", help="the translation rewrite style: %s" % ", ".join(rewritestylelist
))
168 ignoreoptionlist
= ["openoffice", "mozilla"]
169 parser
.add_option("", "--ignore", dest
="ignoreoption",
170 type="choice", choices
=ignoreoptionlist
, metavar
="APPLICATION", help="apply tagging ignore rules for the given application: %s" % ", ".join(ignoreoptionlist
))
171 parser
.add_option("", "--hash", dest
="hash", metavar
="LENGTH", type="int", help="add an md5 hash to translations")
172 parser
.passthrough
.append("format")
173 parser
.passthrough
.append("rewritestyle")
174 parser
.passthrough
.append("ignoreoption")
175 parser
.passthrough
.append("hash")
179 if __name__
== '__main__':