annotate MoinMoin/i18n/msgfmt.py @ 896:9dcd18a790ab

i18n: no .mo files anymore, we directly read .po and cache the result
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Fri, 07 Jul 2006 12:29:58 +0200
parents
children 6dd2e29acffe
rev   line source
896
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1 #!/usr/bin/env python
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
2 # -*- coding: iso-8859-1 -*-
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
3 """Generate binary message catalog from textual translation description.
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
4
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
5 This program converts a textual Uniforum-style message catalog (.po file) into
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
6 a binary GNU catalog (.mo file). This is essentially the same function as the
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
7 GNU msgfmt program, however, it is a simpler implementation.
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
8
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
9 Usage: msgfmt.py [OPTIONS] filename.po
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
10
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
11 Options:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
12 -o file
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
13 --output-file=file
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
14 Specify the output file to write to. If omitted, output will go to a
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
15 file named filename.mo (based off the input file name).
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
16
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
17 -h
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
18 --help
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
19 Print this message and exit.
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
20
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
21 -V
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
22 --version
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
23 Display version information and exit.
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
24
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
25 Written by Martin v. L÷wis <loewis@informatik.hu-berlin.de>,
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
26 refactored by Thomas Waldmann <tw AT waldmann-edv DOT de>.
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
27 """
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
28
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
29 import sys, os
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
30 import getopt, struct, array
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
31
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
32 __version__ = "1.2"
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
33
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
34 class SyntaxErrorException(Exception):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
35 """raised when having trouble parsing the po file content"""
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
36 pass
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
37
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
38 class MsgFmt(object):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
39 """transform .po -> .mo format"""
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
40 def __init__(self):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
41 self.messages = {}
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
42
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
43 def make_filenames(self, filename, outfile=None):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
44 """Compute .mo name from .po name or language"""
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
45 if filename.endswith('.po'):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
46 infile = filename
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
47 else:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
48 infile = filename + '.po'
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
49 if outfile is None:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
50 outfile = os.path.splitext(infile)[0] + '.mo'
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
51 return infile, outfile
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
52
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
53 def add(self, id, str, fuzzy):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
54 """Add a non-fuzzy translation to the dictionary."""
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
55 if not fuzzy and str:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
56 self.messages[id] = str
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
57
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
58 def read_po(self, lines):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
59 ID = 1
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
60 STR = 2
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
61 section = None
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
62 fuzzy = False
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
63 line_no = 0
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
64 # Parse the catalog
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
65 for line in lines:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
66 line_no += 1
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
67 # If we get a comment line after a msgstr, this is a new entry
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
68 if line.startswith('#') and section == STR:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
69 self.add(msgid, msgstr, fuzzy)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
70 section = None
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
71 fuzzy = False
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
72 # Record a fuzzy mark
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
73 if line.startswith('#,') and 'fuzzy' in line:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
74 fuzzy = True
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
75 # Skip comments
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
76 if line.startswith('#'):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
77 continue
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
78 # Now we are in a msgid section, output previous section
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
79 if line.startswith('msgid'):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
80 if section == STR:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
81 self.add(msgid, msgstr, fuzzy)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
82 section = ID
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
83 line = line[5:]
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
84 msgid = msgstr = ''
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
85 # Now we are in a msgstr section
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
86 elif line.startswith('msgstr'):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
87 section = STR
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
88 line = line[6:]
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
89 # Skip empty lines
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
90 line = line.strip()
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
91 if not line:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
92 continue
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
93 # XXX: Does this always follow Python escape semantics?
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
94 line = eval(line)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
95 if section == ID:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
96 msgid += line
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
97 elif section == STR:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
98 msgstr += line
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
99 else:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
100 raise SyntaxErrorException('Syntax error on %s:%d, before:\n%s' % (infile, line_no, line))
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
101 # Add last entry
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
102 if section == STR:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
103 self.add(msgid, msgstr, fuzzy)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
104
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
105 def generate_mo(self):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
106 """Return the generated output."""
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
107 keys = self.messages.keys()
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
108 # the keys are sorted in the .mo file
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
109 keys.sort()
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
110 offsets = []
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
111 ids = ''
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
112 strs = ''
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
113 for id in keys:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
114 # For each string, we need size and file offset. Each string is NUL
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
115 # terminated; the NUL does not count into the size.
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
116 offsets.append((len(ids), len(id), len(strs), len(self.messages[id])))
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
117 ids += id + '\0'
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
118 strs += self.messages[id] + '\0'
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
119 output = []
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
120 # The header is 7 32-bit unsigned integers. We don't use hash tables, so
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
121 # the keys start right after the index tables.
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
122 # translated string.
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
123 keystart = 7*4 + 16*len(keys)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
124 # and the values start after the keys
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
125 valuestart = keystart + len(ids)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
126 koffsets = []
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
127 voffsets = []
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
128 # The string table first has the list of keys, then the list of values.
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
129 # Each entry has first the size of the string, then the file offset.
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
130 for o1, l1, o2, l2 in offsets:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
131 koffsets += [l1, o1 + keystart]
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
132 voffsets += [l2, o2 + valuestart]
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
133 offsets = koffsets + voffsets
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
134 output.append(struct.pack("Iiiiiii",
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
135 0x950412deL, # Magic
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
136 0, # Version
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
137 len(keys), # # of entries
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
138 7*4, # start of key index
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
139 7*4 + len(keys)*8, # start of value index
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
140 0, 0)) # size and offset of hash table
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
141 output.append(array.array("i", offsets).tostring())
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
142 output.append(ids)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
143 output.append(strs)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
144 return ''.join(output)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
145
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
146
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
147 def make(filename, outfile):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
148 mf = MsgFmt()
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
149 infile, outfile = mf.make_filenames(filename, outfile)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
150 try:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
151 lines = file(infile).readlines()
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
152 except IOError, msg:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
153 print >> sys.stderr, msg
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
154 sys.exit(1)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
155 try:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
156 mf.read_po(lines)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
157 output = mf.generate_mo()
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
158 except SyntaxErrorException, msg:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
159 print >> sys.stderr, msg
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
160
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
161 try:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
162 open(outfile, "wb").write(output)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
163 except IOError, msg:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
164 print >> sys.stderr, msg
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
165
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
166
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
167 def usage(code, msg=''):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
168 print >> sys.stderr, __doc__
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
169 if msg:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
170 print >> sys.stderr, msg
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
171 sys.exit(code)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
172
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
173
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
174 def main():
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
175 try:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
176 opts, args = getopt.getopt(sys.argv[1:], 'hVo:', ['help', 'version', 'output-file='])
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
177 except getopt.error, msg:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
178 usage(1, msg)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
179
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
180 outfile = None
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
181 # parse options
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
182 for opt, arg in opts:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
183 if opt in ('-h', '--help'):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
184 usage(0)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
185 elif opt in ('-V', '--version'):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
186 print >> sys.stderr, "msgfmt.py", __version__
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
187 sys.exit(0)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
188 elif opt in ('-o', '--output-file'):
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
189 outfile = arg
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
190 # do it
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
191 if not args:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
192 print >> sys.stderr, 'No input file given'
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
193 print >> sys.stderr, "Try `msgfmt --help' for more information."
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
194 return
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
195
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
196 for filename in args:
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
197 make(filename, outfile)
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
198
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
199
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
200 if __name__ == '__main__':
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
201 main()
9dcd18a790ab i18n: no .mo files anymore, we directly read .po and cache the result
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
202