]> git.evergreen-ils.org Git - working/Evergreen.git/blob - build/i18n/scripts/dojo_resource.py
902498e2e233625d05b964598c0e0a3461fdd03a
[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 codecs
32 import optparse
33 import polib
34 import re
35 import sys
36 import simplejson
37 import os.path
38
39 class DojoResource (basel10n.BaseL10N):
40     """
41     This class provides methods for extracting translatable strings from
42     Evergreen's Dojo resource bundle files, generating translatable POT files,
43     reading translated PO files, and generating an updated Dojo resource bundle
44     files with additional or changed strings.
45     """
46
47     def __init__(self):
48         self.pot = None
49         basel10n.BaseL10N.__init__(self)
50         self.msgs = {}
51
52     def get_strings(self, source):
53         """
54         Extracts translatable strings from Evergreen's Dojo resource bundles.
55         """
56         self.pothead()
57
58         # Avoid generating duplicate entries by keeping track of msgids
59         msgids = dict()
60
61         bundle = simplejson.load(codecs.open(source, encoding='utf-8', mode='r'))
62
63         for key, value in bundle.iteritems():
64             if value in msgids:
65                 msgids[str(value)].occurrences.append((os.path.basename(source), str(key)))
66             else:
67                 poe = polib.POEntry()
68                 poe.occurrences = [(os.path.basename(source), str(key))]
69                 poe.msgid = str(value)
70                 msgids[str(value)] = poe
71
72         # Now add the POEntries to our POFile
73         for poe in msgids.values():
74             self.pot.append(poe)
75
76     def create_bundle(self):
77         """
78         Creates a Dojo resource bundle file based on a translated PO file.
79         """
80
81         for entry in self.pot:
82             for occurrence in entry.occurrences:
83                 # Ack. Horrible hack because polib started insisting
84                 # that occurrences use integers for line numbers. The nerve!
85                 filename, msgkey = occurrence[0].split('.js');
86                 if entry.msgstr == '':
87                     # No translation available; use the en-US definition
88                     self.msgs[msgkey] = entry.msgid
89                 else:
90                     self.msgs[msgkey] = entry.msgstr
91
92 def main():
93     """
94     Determine what action to take
95     """
96     opts = optparse.OptionParser()
97     opts.add_option('-p', '--pot', action='store', \
98         help='Create a POT file from the specified Dojo resource bundle file', \
99         metavar='FILE')
100     opts.add_option('-c', '--create', action='store', \
101         help='Create a Dojo resource bundle file from a translated PO FILE', \
102         metavar='FILE')
103     opts.add_option('-o', '--output', dest='outfile', \
104         help='Write output to FILE (defaults to STDOUT)', metavar='FILE')
105     (options, args) = opts.parse_args()
106
107     pot = DojoResource()
108
109     # Generate a new POT file from the Dojo resource bundle file
110     if options.pot:
111         pot.get_strings(options.pot)
112         if options.outfile:
113             pot.savepot(options.outfile)
114         else:
115             sys.stdout.write(pot.pot.__str__())
116
117     # Generate an Dojo resource bundle file from a PO file
118     elif options.create:
119         pot.loadpo(options.create)
120         pot.create_bundle()
121         if options.outfile:
122             outfile = codecs.open(options.outfile, encoding='utf-8', mode='w')
123             simplejson.dump(pot.msgs, outfile, indent=4)
124         else:
125             print(simplejson.dumps(pot.msgs, indent=4))
126
127     # No options were recognized - print help and bail
128     else:
129         opts.print_help()
130
131 if __name__ == '__main__':
132     main()