Fixed #12673 - Require a version of GNU gettext of 0.1.5 and above.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12296 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2010-01-26 15:01:58 +00:00
parent 12d3799dd2
commit df82175c17
2 changed files with 31 additions and 40 deletions

View File

@ -5,18 +5,10 @@ import glob
import warnings import warnings
from itertools import dropwhile from itertools import dropwhile
from optparse import make_option from optparse import make_option
from subprocess import PIPE, Popen
from django.core.management.base import CommandError, BaseCommand from django.core.management.base import CommandError, BaseCommand
try:
set
except NameError:
from sets import Set as set # For Python 2.3
# Intentionally silence DeprecationWarnings about os.popen3 in Python 2.6. It's
# still sensible for us to use it, since subprocess didn't exist in 2.3.
warnings.filterwarnings('ignore', category=DeprecationWarning, message=r'os\.popen3')
pythonize_re = re.compile(r'\n\s*//') pythonize_re = re.compile(r'\n\s*//')
def handle_extensions(extensions=('html',)): def handle_extensions(extensions=('html',)):
@ -76,21 +68,13 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False, extens
message = "usage: make-messages.py -l <language>\n or: make-messages.py -a\n" message = "usage: make-messages.py -l <language>\n or: make-messages.py -a\n"
raise CommandError(message) raise CommandError(message)
# xgettext versions prior to 0.15 assumed Python source files were encoded # We require gettext version 0.15 or newer.
# in iso-8859-1, and produce utf-8 output. In the case where xgettext is p = Popen('xgettext --version', shell=True, stdout=PIPE, stderr=PIPE)
# given utf-8 input (required for Django files with non-ASCII characters), match = re.search(r'(?P<major>\d+)\.(?P<minor>\d+)', p.stdout.read())
# this results in a utf-8 re-encoding of the original utf-8 that needs to be
# undone to restore the original utf-8. So we check the xgettext version
# here once and set a flag to remember if a utf-8 decoding needs to be done
# on xgettext's output for Python files. We default to assuming this isn't
# necessary if we run into any trouble determining the version.
xgettext_reencodes_utf8 = False
(stdin, stdout, stderr) = os.popen3('xgettext --version', 't')
match = re.search(r'(?P<major>\d+)\.(?P<minor>\d+)', stdout.read())
if match: if match:
xversion = (int(match.group('major')), int(match.group('minor'))) xversion = (int(match.group('major')), int(match.group('minor')))
if xversion < (0, 15): if xversion < (0, 15):
xgettext_reencodes_utf8 = True raise CommandError("Django internationalization requires GNU gettext 0.15 or newer. You are using version %s, please upgrade your gettext toolset." % match.group())
languages = [] languages = []
if locale is not None: if locale is not None:
@ -126,9 +110,9 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False, extens
thefile = '%s.py' % file thefile = '%s.py' % file
open(os.path.join(dirpath, thefile), "w").write(src) open(os.path.join(dirpath, thefile), "w").write(src)
cmd = 'xgettext -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (domain, os.path.join(dirpath, thefile)) cmd = 'xgettext -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (domain, os.path.join(dirpath, thefile))
(stdin, stdout, stderr) = os.popen3(cmd, 't') p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
msgs = stdout.read() msgs = p.stdout.read()
errors = stderr.read() errors = p.stderr.read()
if errors: if errors:
raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors)) raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
old = '#: '+os.path.join(dirpath, thefile)[2:] old = '#: '+os.path.join(dirpath, thefile)[2:]
@ -156,15 +140,12 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False, extens
sys.stdout.write('processing file %s in %s\n' % (file, dirpath)) sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
cmd = 'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % ( cmd = 'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (
domain, os.path.join(dirpath, thefile)) domain, os.path.join(dirpath, thefile))
(stdin, stdout, stderr) = os.popen3(cmd, 't') p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
msgs = stdout.read() msgs = p.stdout.read()
errors = stderr.read() errors = p.stderr.read()
if errors: if errors:
raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors)) raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
if xgettext_reencodes_utf8:
msgs = msgs.decode('utf-8').encode('iso-8859-1')
if thefile != file: if thefile != file:
old = '#: '+os.path.join(dirpath, thefile)[2:] old = '#: '+os.path.join(dirpath, thefile)[2:]
new = '#: '+os.path.join(dirpath, file)[2:] new = '#: '+os.path.join(dirpath, file)[2:]
@ -180,16 +161,16 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False, extens
os.unlink(os.path.join(dirpath, thefile)) os.unlink(os.path.join(dirpath, thefile))
if os.path.exists(potfile): if os.path.exists(potfile):
(stdin, stdout, stderr) = os.popen3('msguniq --to-code=utf-8 "%s"' % potfile, 't') p = Popen('msguniq --to-code=utf-8 "%s"' % potfile, shell=True, stdout=PIPE, stderr=PIPE)
msgs = stdout.read() msgs = p.stdout.read()
errors = stderr.read() errors = p.stderr.read()
if errors: if errors:
raise CommandError("errors happened while running msguniq\n%s" % errors) raise CommandError("errors happened while running msguniq\n%s" % errors)
open(potfile, 'w').write(msgs) open(potfile, 'w').write(msgs)
if os.path.exists(pofile): if os.path.exists(pofile):
(stdin, stdout, stderr) = os.popen3('msgmerge -q "%s" "%s"' % (pofile, potfile), 't') p = Popen('msgmerge -q "%s" "%s"' % (pofile, potfile), shell=True, stdout=PIPE, stderr=PIPE)
msgs = stdout.read() msgs = p.stdout.read()
errors = stderr.read() errors = p.stderr.read()
if errors: if errors:
raise CommandError("errors happened while running msgmerge\n%s" % errors) raise CommandError("errors happened while running msgmerge\n%s" % errors)
open(pofile, 'wb').write(msgs) open(pofile, 'wb').write(msgs)

View File

@ -512,6 +512,16 @@ creation and upkeep of these files.
The old tool ``bin/make-messages.py`` has been moved to the command The old tool ``bin/make-messages.py`` has been moved to the command
``django-admin.py makemessages`` to provide consistency throughout Django. ``django-admin.py makemessages`` to provide consistency throughout Django.
.. admonition:: Gettext utilities
The ``makemessages`` command (and ``compilemessages`` discussed later) use
commands from the GNU gettext toolset: ``xgetetxt``, ``msgfmt``,
``msgmerge`` and ``msguniq``.
.. versionchanged:: 1.2
The minimum version of the ``gettext`` utilities supported is 0.15.
To create or update a message file, run this command:: To create or update a message file, run this command::
django-admin.py makemessages -l de django-admin.py makemessages -l de
@ -1062,7 +1072,7 @@ or want to test or compile a changed message file, you will need the
* ``gettext-runtime-X.zip`` * ``gettext-runtime-X.zip``
* ``gettext-tools-X.zip`` * ``gettext-tools-X.zip``
``X`` is the version number, we recomend using ``0.15`` or higher. ``X`` is the version number, we are requiring ``0.15`` or higher.
* Extract the contents of the ``bin\`` directories in both files to the * Extract the contents of the ``bin\`` directories in both files to the
same folder on your system (i.e. ``C:\Program Files\gettext-utils``) same folder on your system (i.e. ``C:\Program Files\gettext-utils``)