]> git.evergreen-ils.org Git - working/Evergreen.git/blob - build/i18n/scripts/dojo_resource.py
Handle link elements with reporter labels
[working/Evergreen.git] / build / i18n / scripts / dojo_resource.py
1 #!/usr/bin/env python
2 # dojo_resource.py
3 """
4 This class enables translation of Dojo resource bundles using gettext format.
5
6 Requires polib from http://polib.googlecode.com
7
8 Source event definitions are structured as follows:
9 {
10     MSG_ID1: "This is a message with 1 variable - ${0}.",
11     MSG_ID2: "This is a message with two variables: ${0} and ${1}."
12 }
13
14 Note that this is a deliberately limited subset of the variable substitution
15 allowed by http://api.dojotoolkit.org/jsdoc/dojo/1.2/dojo.string.substitute
16
17 """
18 # Copyright 2007 Dan Scott <dscott@laurentian.ca>
19 #
20 # This program is free software; you can redistribute it and/or
21 # modify it under the terms of the GNU General Public License
22 # as published by the Free Software Foundation; either version 2
23 # of the License, or (at your option) any later version.
24 #
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 # GNU General Public License for more details.
29
30 import basel10n
31 import optparse
32 import polib
33 import re
34 import sys
35 import simplejson
36 import os.path
37
38 class DojoResource (basel10n.BaseL10N):
39     """
40     This class provides methods for extracting translatable strings from
41     Evergreen's Dojo resource bundle files, generating translatable POT files,
42     reading translated PO files, and generating an updated Dojo resource bundle
43     files with additional or changed strings.
44     """
45
46     def __init__(self):
47         self.pot = None
48         basel10n.BaseL10N.__init__(self)
49         self.msgs = {}
50
51     def get_strings(self, source):
52         """
53         Extracts translatable strings from Evergreen's Dojo resource bundles.
54         """
55         self.pothead()
56         
57         bundle = simplejson.load(open(source, 'r'))
58
59         for key, value in bundle.iteritems():
60             poe = polib.POEntry()
61             poe.occurrences = [(os.path.basename(source), key)]
62             poe.msgid = value
63             poe.msgstr = value
64             self.pot.append(poe)
65
66     def create_bundle(self):
67         """
68         Creates a Dojo resource bundle file based on a translated PO file.
69         """
70
71         msg = "\t\"%s\": \"%s\""
72
73         for entry in self.pot:
74             for filename, msgkey in entry.occurrences:
75                 if entry.msgstr == '':
76                     # No translation available; use the en-US definition
77                     self.msgs[msgkey] = entry.msgid
78                 else:
79                     self.msgs[msgkey] = entry.msgstr
80
81 def main():
82     """
83     Determine what action to take
84     """
85     opts = optparse.OptionParser()
86     opts.add_option('-p', '--pot', action='store', \
87         help='Create a POT file from the specified Dojo resource bundle file', \
88         metavar='FILE')
89     opts.add_option('-c', '--create', action='store', \
90         help='Create a Dojo resource bundle file from a translated PO FILE', \
91         metavar='FILE')
92     opts.add_option('-o', '--output', dest='outfile', \
93         help='Write output to FILE (defaults to STDOUT)', metavar='FILE')
94     (options, args) = opts.parse_args()
95
96     pot = DojoResource()
97
98     # Generate a new POT file from the Dojo resource bundle file
99     if options.pot:
100         pot.get_strings(options.pot)
101         if options.outfile:
102             pot.savepot(options.outfile)
103         else:
104             sys.stdout.write(pot.pot.__str__())
105
106     # Generate an Dojo resource bundle file from a PO file
107     elif options.create:
108         pot.loadpo(options.create)
109         pot.create_bundle()
110         if options.outfile:
111             outfile = open(options.outfile, 'w')
112             simplejson.dump(pot.msgs, outfile, indent=4)
113         else:
114             print(simplejson.dumps(pot.msgs, indent=4))
115
116     # No options were recognized - print help and bail
117     else:
118         opts.print_help()
119
120 if __name__ == '__main__':
121     main()