summaryrefslogtreecommitdiff
path: root/admin/bin/templ2po.py
blob: 4f4d91429574b862ac80d22f17957b7d4940c085 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#! /usr/bin/env python
# Move language templates to po file.
# This should be one time task of source code transition.
# Code stolen from pygettext.py
# by Tokio Kikuchi <tkikuchi@is.kochi-u.ac.jp>

"""templ2po.py -- convert mailman language to po file.

Usage: templ2po.py language ...

Options:

    -h, --help

Language is in IANA format.
"""

import sys
import os
import getopt

try:
    import paths
    from Mailman.i18n import _
except ImportError:
    def _(s): return s

EMPTYSTRING = ''


def usage(code, msg=''):
    if code:
        fd = sys.stderr
    else:
        fd = sys.stdout
    print >> fd, _(__doc__) % globals()
    if msg:
        print >> fd, msg
    sys.exit(code)



escapes = []

def make_escapes(pass_iso8859):
    global escapes
    if pass_iso8859:
        # Allow iso-8859 characters to pass through so that e.g. 'msgid
        # "H[o-umlaut]he"' would result not result in 'msgid "H\366he"'.
        # Otherwise we escape any character outside the 32..126 range.
        mod = 128
    else:
        mod = 256
    for i in range(256):
        if 32 <= (i % mod) <= 126:
            escapes.append(chr(i))
        else:
            escapes.append("\\%03o" % i)
    escapes[ord('\\')] = '\\\\'
    escapes[ord('\t')] = '\\t'
    escapes[ord('\r')] = '\\r'
    escapes[ord('\n')] = '\\n'
    escapes[ord('\"')] = '\\"'


def escape(s, eightbit):
    global escapes
    s = list(s)
    for i in range(len(s)):
        if eightbit and ord(s[i]) > 127:
            pass
        else:
            s[i] = escapes[ord(s[i])]
    return EMPTYSTRING.join(s)


def normalize(s, eightbit):
    # This converts the various Python string types into a format that is
    # appropriate for .po files, namely much closer to C style.
    lines = s.split('\n')
    if len(lines) == 1:
        s = '"' + escape(s, eightbit) + '"'
    else:
        if not lines[-1]:
            del lines[-1]
            lines[-1] = lines[-1] + '\n'
        for i in range(len(lines)):
            lines[i] = escape(lines[i], eightbit)
        lineterm = '\\n"\n"'
        s = '""\n"' + lineterm.join(lines) + '"'
    return s



def main():
    try:
        opts, args = getopt.getopt(
            sys.argv[1:],
            'h',
            ['help',]
             )
    except getopt.error, msg:
        usage(1, msg)

    # parse options
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)

    # calculate escapes
    make_escapes(0)

    for lang in args:
        filenames = os.listdir('templates/%s' % lang)
        filenames.remove('CVS')
        outfile = file('messages/%s/LC_MESSAGES/mailman.po' % lang, 'a')
        for filename in filenames:
            try:
                s = file('templates/en/%s' % filename).read()
                print >> outfile, '#: templates/en/%s:1' % filename
                print >> outfile, '#, template'
                print >> outfile, 'msgid', normalize(s, 0)
                s = file('templates/%s/%s' % (lang, filename)).read()
                print >> outfile, 'msgstr', normalize(s, 1)
                print >> outfile
            except IOError:
                continue


if __name__ == '__main__':
    main()