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