Django

Code

Changeset 907

Show
Ignore:
Timestamp:
10/17/05 09:37:26 (3 years ago)
Author:
hugo
Message:

i18n: added -a switch to make-messags.py to run xgettext on all messagefiles

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/i18n/django/bin/make-messages.py

    r884 r907  
    66import getopt 
    77 
    8 basedir = None 
     8localedir = None 
    99 
    1010if os.path.isdir(os.path.join('conf', 'locale')): 
    11     basedir = os.path.abspath(os.path.join('conf', 'locale')) 
     11    localedir = os.path.abspath(os.path.join('conf', 'locale')) 
    1212elif os.path.isdir('locale'): 
    13     basedir = os.path.abspath('locale') 
     13    localedir = os.path.abspath('locale') 
    1414else: 
    1515    print "this script should be run from the django svn tree or your project or app tree" 
    1616    sys.exit(1) 
    1717 
    18 (opts, args) = getopt.getopt(sys.argv[1:], 'l:d:v') 
     18(opts, args) = getopt.getopt(sys.argv[1:], 'l:d:va') 
    1919 
    2020lang = None 
    2121domain = 'django' 
    2222verbose = False 
     23all = False 
    2324 
    2425for o, v in opts: 
     
    2930    elif o == '-v': 
    3031        verbose = True 
     32    elif o == '-a': 
     33        all = True 
    3134 
    32 if lang is None or domain is None: 
     35if (lang is None and not all) or domain is None: 
    3336    print "usage: make-messages.py -l <language>" 
     37    print "   or: make-messages.py -a" 
    3438    sys.exit(1) 
    3539 
    36 basedir = os.path.join(basedir, lang, 'LC_MESSAGES') 
    37 if not os.path.isdir(basedir): 
    38     os.makedirs(basedir) 
     40languages = [] 
     41 
     42if lang is not None: 
     43    languages.append(lang) 
     44elif all: 
     45    languages = [el for el in os.listdir(localedir) if not el.startswith('.')] 
    3946 
    4047dot_re = re.compile('\S') 
     
    6168    return ''.join(o) 
    6269 
    63 pofile = os.path.join(basedir, '%s.po' % domain) 
    64 potfile = os.path.join(basedir, '%s.pot' % domain) 
     70for lang in languages: 
    6571 
    66 if os.path.exists(potfile): 
     72    print "processing language", lang 
     73    basedir = os.path.join(localedir, lang, 'LC_MESSAGES') 
     74    if not os.path.isdir(basedir): 
     75        os.makedirs(basedir) 
     76 
     77    pofile = os.path.join(basedir, '%s.po' % domain) 
     78    potfile = os.path.join(basedir, '%s.pot' % domain) 
     79 
     80    if os.path.exists(potfile): 
     81        os.unlink(potfile) 
     82 
     83    for (dirpath, dirnames, filenames) in os.walk("."): 
     84        for file in filenames: 
     85            if file.endswith('.py') or file.endswith('.html'): 
     86                thefile = file 
     87                if file.endswith('.html'): 
     88                    src = open(os.path.join(dirpath, file), "rb").read() 
     89                    open(os.path.join(dirpath, '%s.py' % file), "wb").write(templateize(src)) 
     90                    thefile = '%s.py' % file 
     91                if verbose: sys.stdout.write('processing file %s in %s\n' % (file, dirpath)) 
     92                cmd = 'xgettext %s -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy -o - "%s"' % ( 
     93                    os.path.exists(potfile) and '--omit-header' or '', domain, os.path.join(dirpath, thefile)) 
     94                msgs = os.popen(cmd, 'r').read() 
     95                if thefile != file: 
     96                    old = '#: '+os.path.join(dirpath, thefile)[2:] 
     97                    new = '#: '+os.path.join(dirpath, file)[2:] 
     98                    msgs = msgs.replace(old, new) 
     99                if msgs: 
     100                    open(potfile, 'ab').write(msgs) 
     101                if thefile != file: 
     102                    os.unlink(os.path.join(dirpath, thefile)) 
     103 
     104    msgs = os.popen('msguniq %s' % potfile, 'r').read() 
     105    open(potfile, 'w').write(msgs) 
     106    if os.path.exists(pofile): 
     107        msgs = os.popen('msgmerge %s %s' % (pofile, potfile), 'r').read() 
     108    open(pofile, 'wb').write(msgs) 
    67109    os.unlink(potfile) 
    68110 
    69 for (dirpath, dirnames, filenames) in os.walk("."): 
    70     for file in filenames: 
    71         if file.endswith('.py') or file.endswith('.html'): 
    72             thefile = file 
    73             if file.endswith('.html'): 
    74                 src = open(os.path.join(dirpath, file), "rb").read() 
    75                 open(os.path.join(dirpath, '%s.py' % file), "wb").write(templateize(src)) 
    76                 thefile = '%s.py' % file 
    77             if verbose: sys.stdout.write('processing file %s in %s\n' % (file, dirpath)) 
    78             cmd = 'xgettext %s -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy -o - "%s"' % ( 
    79                 os.path.exists(potfile) and '--omit-header' or '', domain, os.path.join(dirpath, thefile)) 
    80             msgs = os.popen(cmd, 'r').read() 
    81             if thefile != file: 
    82                 old = '#: '+os.path.join(dirpath, thefile)[2:] 
    83                 new = '#: '+os.path.join(dirpath, file)[2:] 
    84                 msgs = msgs.replace(old, new) 
    85             if msgs: 
    86                 open(potfile, 'ab').write(msgs) 
    87             if thefile != file: 
    88                 os.unlink(os.path.join(dirpath, thefile)) 
    89  
    90 msgs = os.popen('msguniq %s' % potfile, 'r').read() 
    91 open(potfile, 'w').write(msgs) 
    92 if os.path.exists(pofile): 
    93     msgs = os.popen('msgmerge %s %s' % (pofile, potfile), 'r').read() 
    94 open(pofile, 'wb').write(msgs) 
    95 os.unlink(potfile) 
    96