Ticket #6435: make-messages.py

File make-messages.py, 6.1 KB (added by isoemail@…, 17 years ago)
Line 
1#!/usr/bin/env python
2
3# Need to ensure that the i18n framework is enabled
4from django.conf import settings
5settings.configure(USE_I18N = True)
6
7from django.utils.translation import templatize
8import re
9import os
10import sys
11import getopt
12
13pythonize_re = re.compile(r'\n\s*//')
14
15def make_messages():
16 localedir = None
17
18 if os.path.isdir(os.path.join('conf', 'locale')):
19 localedir = os.path.abspath(os.path.join('conf', 'locale'))
20 elif os.path.isdir('locale'):
21 localedir = os.path.abspath('locale')
22 else:
23 print "This script should be run from the django svn tree or your project or app tree."
24 print "If you did indeed run it from the svn checkout or your project or application,"
25 print "maybe you are just missing the conf/locale (in the django tree) or locale (for project"
26 print "and application) directory?"
27 print "make-messages.py doesn't create it automatically, you have to create it by hand if"
28 print "you want to enable i18n for your project or application."
29 sys.exit(1)
30
31 (opts, args) = getopt.getopt(sys.argv[1:], 'l:d:va')
32
33 lang = None
34 domain = 'django'
35 verbose = False
36 all = False
37
38 for o, v in opts:
39 if o == '-l':
40 lang = v
41 elif o == '-d':
42 domain = v
43 elif o == '-v':
44 verbose = True
45 elif o == '-a':
46 all = True
47
48 if domain not in ('django', 'djangojs'):
49 print "currently make-messages.py only supports domains 'django' and 'djangojs'"
50 sys.exit(1)
51 if (lang is None and not all) or domain is None:
52 print "usage: make-messages.py -l <language>"
53 print " or: make-messages.py -a"
54 sys.exit(1)
55
56 languages = []
57
58 if lang is not None:
59 languages.append(lang)
60 elif all:
61 languages = [el for el in os.listdir(localedir) if not el.startswith('.')]
62
63 for lang in languages:
64
65 print "processing language", lang
66 basedir = os.path.join(localedir, lang, 'LC_MESSAGES')
67 if not os.path.isdir(basedir):
68 os.makedirs(basedir)
69
70 pofile = os.path.join(basedir, '%s.po' % domain)
71 potfile = os.path.join(basedir, '%s.pot' % domain)
72
73 if os.path.exists(potfile):
74 os.unlink(potfile)
75
76 for (dirpath, dirnames, filenames) in os.walk("."):
77 for file in filenames:
78 if domain == 'djangojs' and file.endswith('.js'):
79 if verbose: sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
80 src = open(os.path.join(dirpath, file), "rb").read()
81 src = pythonize_re.sub('\n#', src)
82 open(os.path.join(dirpath, '%s.py' % file), "wb").write(src)
83 thefile = '%s.py' % file
84 cmd = 'xgettext %s -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy --from-code UTF-8 -o - "%s"' % (
85 os.path.exists(potfile) and '--omit-header' or '', domain, os.path.join(dirpath, thefile))
86 (stdin, stdout, stderr) = os.popen3(cmd, 'b')
87 msgs = stdout.read()
88 errors = stderr.read()
89 if errors:
90 print "errors happened while running xgettext on %s" % file
91 print errors
92 sys.exit(8)
93 old = '#: '+os.path.join(dirpath, thefile)[2:]
94 new = '#: '+os.path.join(dirpath, file)[2:]
95 msgs = msgs.replace(old, new)
96 if msgs:
97 open(potfile, 'ab').write(msgs)
98 os.unlink(os.path.join(dirpath, thefile))
99 elif domain == 'django' and (file.endswith('.py') or file.endswith('.html') or file.endswith('.xhtml')):
100 thefile = file
101 if file.endswith('.html') or file.endswith('.xhtml'):
102 src = open(os.path.join(dirpath, file), "rb").read()
103 open(os.path.join(dirpath, '%s.py' % file), "wb").write(templatize(src))
104 thefile = '%s.py' % file
105 if verbose: sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
106 cmd = 'xgettext %s -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy --from-code UTF-8 -o - "%s"' % (
107 os.path.exists(potfile) and '--omit-header' or '', domain, os.path.join(dirpath, thefile))
108 (stdin, stdout, stderr) = os.popen3(cmd, 'b')
109 msgs = stdout.read()
110 errors = stderr.read()
111 if errors:
112 print "errors happened while running xgettext on %s" % file
113 print errors
114 sys.exit(8)
115 if thefile != file:
116 old = '#: '+os.path.join(dirpath, thefile)[2:]
117 new = '#: '+os.path.join(dirpath, file)[2:]
118 msgs = msgs.replace(old, new)
119 if msgs:
120 open(potfile, 'ab').write(msgs)
121 if thefile != file:
122 os.unlink(os.path.join(dirpath, thefile))
123
124 if os.path.exists(potfile):
125 (stdin, stdout, stderr) = os.popen3('msguniq "%s"' % potfile, 'b')
126 msgs = stdout.read()
127 errors = stderr.read()
128 if errors:
129 print "errors happened while running msguniq"
130 print errors
131 sys.exit(8)
132 open(potfile, 'w').write(msgs)
133 if os.path.exists(pofile):
134 (stdin, stdout, stderr) = os.popen3('msgmerge -q "%s" "%s"' % (pofile, potfile), 'b')
135 msgs = stdout.read()
136 errors = stderr.read()
137 if errors:
138 print "errors happened while running msgmerge"
139 print errors
140 sys.exit(8)
141 open(pofile, 'wb').write(msgs)
142 os.unlink(potfile)
143
144if __name__ == "__main__":
145 make_messages()
Back to Top