Ticket #6073: 6073-dont-allow-BOM-in-po-files-1.diff

File 6073-dont-allow-BOM-in-po-files-1.diff, 2.1 KB (added by Ramiro Morales, 15 years ago)

Patch implementing requirements outlined by Malcom

  • django/core/management/commands/compilemessages.py

    diff --git a/django/core/management/commands/compilemessages.py b/django/core/management/commands/compilemessages.py
    a b  
     1import codecs
    12import os
    23import sys
    34from optparse import make_option
     
    89except NameError:
    910    from sets import Set as set     # For Python 2.3
    1011
     12def has_bom(fn):
     13    f = open(fn, 'r')
     14    sample = f.read(4)
     15    return sample[:3] == '\xef\xbb\xbf' or \
     16            sample.startswith(codecs.BOM_UTF16_LE) or \
     17            sample.startswith(codecs.BOM_UTF16_BE)
     18
    1119def compile_messages(locale=None):
    1220    basedirs = [os.path.join('conf', 'locale'), 'locale']
    1321    if os.environ.get('DJANGO_SETTINGS_MODULE'):
     
    2735            for f in filenames:
    2836                if f.endswith('.po'):
    2937                    sys.stderr.write('processing file %s in %s\n' % (f, dirpath))
    30                     pf = os.path.splitext(os.path.join(dirpath, f))[0]
     38                    fn = os.path.join(dirpath, f)
     39                    if has_bom(fn):
     40                        raise CommandError("The %s file has a BOM (Byte Order Mark). Django only supports .po files encoded in UTF-8 and without any BOM." % fn)
     41                    pf = os.path.splitext(fn)[0]
    3142                    # Store the names of the .mo and .po files in an environment
    3243                    # variable, rather than doing a string replacement into the
    3344                    # command, so that we can take advantage of shell quoting, to
  • docs/topics/i18n.txt

    diff --git a/docs/topics/i18n.txt b/docs/topics/i18n.txt
    a b  
    650650   ``django-admin compilemessages`` works see `gettext on Windows`_ for more
    651651   information.
    652652
     653.. admonition:: .po files: Encoding and BOM usage.
     654
     655   Django only supports ``.po`` files encoded in UTF-8 and without any BOM
     656   (Byte Order Mark) so if your text editor adds BOM to the beginning of
     657   files by default you need to configure it to not do so.
     658
    653659.. _how-django-discovers-language-preference:
    654660
    6556613. How Django discovers language preference
Back to Top