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