Code

Ticket #9212: 9212.diff

File 9212.diff, 1.9 KB (added by kmtracey, 6 years ago)
Line 
1Index: django/core/management/commands/makemessages.py
2===================================================================
3--- django/core/management/commands/makemessages.py     (revision 9152)
4+++ django/core/management/commands/makemessages.py     (working copy)
5@@ -75,6 +75,22 @@
6             message = "usage: make-messages.py -l <language>\n   or: make-messages.py -a\n"
7         raise CommandError(message)
8 
9+    # xgettext versions prior to 0.15 assumed Python source files were encoded
10+    # in iso-8859-1, and produce utf-8 output.  In the case where xgettext is
11+    # given utf-8 input (required for Django files with non-ASCII characters),
12+    # this results in a utf-8 re-encoding of the original utf-8 that needs to be
13+    # undone to restore the original utf-8.  So we check the xgettext version
14+    # here once and set a flag to remember if a utf-8 decoding needs to be done
15+    # on xgettext's output for Python files.  We default to assuming this isn't
16+    # necessary if we run into any trouble determining the version.
17+    xgettext_reencodes_utf8 = False
18+    (stdin, stdout, stderr) = os.popen3('xgettext --version', 't')
19+    match = re.compile(r'.*?(?P<major>\d+)\.(?P<minor>\d+)').match(stdout.read())
20+    if match:
21+        xversion = (int(match.group('major')), int(match.group('minor')))
22+        if xversion < (0, 15):
23+            xgettext_reencodes_utf8 = True
24+
25     languages = []
26     if locale is not None:
27         languages.append(locale)
28@@ -139,6 +155,10 @@
29                 errors = stderr.read()
30                 if errors:
31                     raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
32+
33+                if xgettext_reencodes_utf8:
34+                    msgs = msgs.decode('utf-8').encode('iso-8859-1')
35+
36                 if thefile != file:
37                     old = '#: '+os.path.join(dirpath, thefile)[2:]
38                     new = '#: '+os.path.join(dirpath, file)[2:]