1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """convert Gettext PO localization files to Mozilla .dtd and .properties files
23
24 see: http://translate.sourceforge.net/wiki/toolkit/po2moz for examples and
25 usage instructions
26 """
27
28 import os.path
29 from translate.convert import po2dtd
30 from translate.convert import po2prop
31 from translate.convert import prop2mozfunny
32 from translate.storage import xpi
33 from translate.convert import convert
34
36 - def __init__(self, formats, usetemplates=False, usepots=False, description=None):
38
40 """creates an outputarchive if required"""
41 if options.output and self.isarchive(options.output, 'output'):
42 newlang = None
43 newregion = None
44 if options.locale is not None:
45 if options.locale.count("-") > 1:
46 raise ValueError("Invalid locale: %s - should be of the form xx-YY" % options.locale)
47 elif "-" in options.locale:
48 newlang, newregion = options.locale.split("-")
49 else:
50 newlang, newregion = options.locale, ""
51 if options.clonexpi is not None:
52 originalxpi = xpi.XpiFile(options.clonexpi, "r")
53 options.outputarchive = originalxpi.clone(options.output, "w", newlang=newlang, newregion=newregion)
54 elif self.isarchive(options.template, 'template'):
55 options.outputarchive = options.templatearchive.clone(options.output, "a", newlang=newlang, newregion=newregion)
56 else:
57 if os.path.exists(options.output):
58 options.outputarchive = xpi.XpiFile(options.output, "a", locale=newlang, region=newregion)
59 else:
60
61 options.outputarchive = xpi.XpiFile(options.output, "w", locale=newlang, region=newregion)
62
64 """splits a inputpath into name and extension"""
65
66 d, n = os.path.dirname(inputpath), os.path.basename(inputpath)
67 s = n.find(".")
68 if s == -1:
69 return (inputpath, "")
70 root = os.path.join(d, n[:s])
71 ext = n[s+1:]
72 return (root, ext)
73
83
85
86 formats = {("dtd.po", "dtd"): ("dtd", po2dtd.convertdtd),
87 ("properties.po", "properties"): ("properties", po2prop.convertmozillaprop),
88 ("it.po", "it"): ("it", prop2mozfunny.po2it),
89 ("ini.po", "ini"): ("ini", prop2mozfunny.po2ini),
90 ("inc.po", "inc"): ("inc", prop2mozfunny.po2inc),
91
92 ("*", "*"): ("*", convert.copyinput),
93 "*": ("*", convert.copyinput)}
94
95 replacer = convert.Replacer("${locale}", None)
96 for replaceformat in ("js", "rdf", "manifest"):
97 formats[(None, replaceformat)] = (replaceformat, replacer.searchreplacetemplate)
98 formats[(replaceformat, replaceformat)] = (replaceformat, replacer.searchreplaceinput)
99 formats[replaceformat] = (replaceformat, replacer.searchreplaceinput)
100 parser = MozConvertOptionParser(formats, usetemplates=True, description=__doc__)
101 parser.add_option("-l", "--locale", dest="locale", default=None,
102 help="set output locale (required as this sets the directory names)", metavar="LOCALE")
103 parser.add_option("", "--clonexpi", dest="clonexpi", default=None,
104 help="clone xpi structure from the given xpi file")
105 parser.add_fuzzy_option()
106 parser.replacer = replacer
107 parser.run(argv)
108
109
110 if __name__ == '__main__':
111 main()
112