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