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
35
37
38 - def __init__(self, formats, usetemplates=False, usepots=False,
39 description=None):
43
45 """creates an outputarchive if required"""
46 if options.output and self.isarchive(options.output, 'output'):
47 newlang = None
48 newregion = None
49 if options.locale is not None:
50 if options.locale.count("-") > 1:
51 raise ValueError("Invalid locale: %s - should be of the form xx-YY" % options.locale)
52 elif "-" in options.locale:
53 newlang, newregion = options.locale.split("-")
54 else:
55 newlang, newregion = options.locale, ""
56 if options.clonexpi is not None:
57 originalxpi = xpi.XpiFile(options.clonexpi, "r")
58 options.outputarchive = originalxpi.clone(options.output, "w",
59 newlang=newlang,
60 newregion=newregion)
61 elif self.isarchive(options.template, 'template'):
62 options.outputarchive = options.templatearchive.clone(options.output, "a",
63 newlang=newlang,
64 newregion=newregion)
65 else:
66 if os.path.exists(options.output):
67 options.outputarchive = xpi.XpiFile(options.output, "a",
68 locale=newlang,
69 region=newregion)
70 else:
71
72 options.outputarchive = xpi.XpiFile(options.output, "w",
73 locale=newlang,
74 region=newregion)
75
77 """splits a inputpath into name and extension"""
78
79 d, n = os.path.dirname(inputpath), os.path.basename(inputpath)
80 s = n.find(".")
81 if s == -1:
82 return (inputpath, "")
83 root = os.path.join(d, n[:s])
84 ext = n[s+1:]
85 return (root, ext)
86
96
97
99
100 formats = {("dtd.po", "dtd"): ("dtd", po2dtd.convertdtd),
101 ("properties.po", "properties"): ("properties",
102 po2prop.convertmozillaprop),
103 ("it.po", "it"): ("it", prop2mozfunny.po2it),
104 ("ini.po", "ini"): ("ini", prop2mozfunny.po2ini),
105 ("inc.po", "inc"): ("inc", prop2mozfunny.po2inc),
106
107 ("*", "*"): ("*", convert.copyinput),
108 "*": ("*", convert.copyinput)}
109
110 replacer = convert.Replacer("${locale}", None)
111 for replaceformat in ("js", "rdf", "manifest"):
112 formats[(None, replaceformat)] = (replaceformat,
113 replacer.searchreplacetemplate)
114 formats[(replaceformat, replaceformat)] = (replaceformat,
115 replacer.searchreplaceinput)
116 formats[replaceformat] = (replaceformat, replacer.searchreplaceinput)
117 parser = MozConvertOptionParser(formats, usetemplates=True, description=__doc__)
118 parser.add_option("-l", "--locale", dest="locale", default=None,
119 help="set output locale (required as this sets the directory names)",
120 metavar="LOCALE")
121 parser.add_option("", "--clonexpi", dest="clonexpi", default=None,
122 help="clone xpi structure from the given xpi file")
123 parser.add_fuzzy_option()
124 parser.replacer = replacer
125 parser.run(argv)
126
127
128 if __name__ == '__main__':
129 main()
130