/usr/share/pyshared/translate/convert/po2moz.py is in translate-toolkit 1.10.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2004-2006 Zuza Software Foundation
#
# This file is part of translate.
#
# translate is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# translate is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
"""Convert Gettext PO localization files to Mozilla .dtd and .properties files.
See: http://docs.translatehouse.org/projects/translate-toolkit/en/latest/commands/moz2po.html
for examples and usage instructions.
"""
import os.path
from translate.convert import po2dtd
from translate.convert import po2prop
from translate.convert import po2mozlang
from translate.convert import prop2mozfunny
from translate.storage import xpi
from translate.convert import convert
class MozConvertOptionParser(convert.ArchiveConvertOptionParser):
def __init__(self, formats, usetemplates=False, usepots=False,
description=None):
convert.ArchiveConvertOptionParser.__init__(self, formats, usetemplates, usepots,
description=description,
archiveformats={"xpi": xpi.XpiFile})
def initoutputarchive(self, options):
"""creates an outputarchive if required"""
if options.output and self.isarchive(options.output, 'output'):
newlang = None
newregion = None
if options.locale is not None:
if options.locale.count("-") > 1:
raise ValueError("Invalid locale: %s - should be of the form xx-YY" % options.locale)
elif "-" in options.locale:
newlang, newregion = options.locale.split("-")
else:
newlang, newregion = options.locale, ""
if options.clonexpi is not None:
originalxpi = xpi.XpiFile(options.clonexpi, "r")
options.outputarchive = originalxpi.clone(options.output, "w",
newlang=newlang,
newregion=newregion)
elif self.isarchive(options.template, 'template'):
options.outputarchive = options.templatearchive.clone(options.output, "a",
newlang=newlang,
newregion=newregion)
else:
if os.path.exists(options.output):
options.outputarchive = xpi.XpiFile(options.output, "a",
locale=newlang,
region=newregion)
else:
# FIXME: this is unlikely to work because it has no jar files
options.outputarchive = xpi.XpiFile(options.output, "w",
locale=newlang,
region=newregion)
def splitinputext(self, inputpath):
"""splits a inputpath into name and extension"""
# TODO: not sure if this should be here, was in po2moz
d, n = os.path.dirname(inputpath), os.path.basename(inputpath)
s = n.find(".")
if s == -1:
return (inputpath, "")
root = os.path.join(d, n[:s])
ext = n[s+1:]
return (root, ext)
def recursiveprocess(self, options):
"""recurse through directories and convert files"""
self.replacer.replacestring = options.locale
result = super(MozConvertOptionParser, self).recursiveprocess(options)
if self.isarchive(options.output, 'output'):
if options.progress in ('console', 'verbose'):
print "writing xpi file..."
options.outputarchive.close()
return result
def main(argv=None):
# handle command line options
formats = {("dtd.po", "dtd"): ("dtd", po2dtd.convertdtd),
("properties.po", "properties"): ("properties",
po2prop.convertmozillaprop),
("it.po", "it"): ("it", prop2mozfunny.po2it),
("ini.po", "ini"): ("ini", prop2mozfunny.po2ini),
("inc.po", "inc"): ("inc", prop2mozfunny.po2inc),
("lang.po", "lang"): ("lang", po2mozlang.convertlang),
# (None, "*"): ("*", convert.copytemplate),
("*", "*"): ("*", convert.copyinput),
"*": ("*", convert.copyinput)}
# handle search and replace
replacer = convert.Replacer("${locale}", None)
for replaceformat in ("js", "rdf", "manifest"):
formats[(None, replaceformat)] = (replaceformat,
replacer.searchreplacetemplate)
formats[(replaceformat, replaceformat)] = (replaceformat,
replacer.searchreplaceinput)
formats[replaceformat] = (replaceformat, replacer.searchreplaceinput)
parser = MozConvertOptionParser(formats, usetemplates=True, description=__doc__)
parser.add_option("-l", "--locale", dest="locale", default=None,
help="set output locale (required as this sets the directory names)",
metavar="LOCALE")
parser.add_option("", "--clonexpi", dest="clonexpi", default=None,
help="clone xpi structure from the given xpi file")
parser.add_fuzzy_option()
parser.replacer = replacer
parser.run(argv)
if __name__ == '__main__':
main()
|