Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Alexander Belopolsky | e8f5832 | 2010-10-15 16:28:20 +0000 | [diff] [blame] | 2 | # Written by Martin v. Löwis <loewis@informatik.hu-berlin.de> |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 3 | |
| 4 | """Generate binary message catalog from textual translation description. |
| 5 | |
| 6 | This program converts a textual Uniforum-style message catalog (.po file) into |
| 7 | a binary GNU catalog (.mo file). This is essentially the same function as the |
Cheryl Sabella | 637a33b | 2018-11-07 09:12:20 -0500 | [diff] [blame^] | 8 | GNU msgfmt program, however, it is a simpler implementation. Currently it |
| 9 | does not handle plural forms but it does handle message contexts. |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 10 | |
| 11 | Usage: msgfmt.py [OPTIONS] filename.po |
| 12 | |
| 13 | Options: |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 14 | -o file |
| 15 | --output-file=file |
| 16 | Specify the output file to write to. If omitted, output will go to a |
| 17 | file named filename.mo (based off the input file name). |
| 18 | |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 19 | -h |
| 20 | --help |
| 21 | Print this message and exit. |
| 22 | |
| 23 | -V |
| 24 | --version |
| 25 | Display version information and exit. |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 26 | """ |
| 27 | |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 28 | import os |
Ezio Melotti | 9bf379e | 2012-11-09 11:46:19 +0100 | [diff] [blame] | 29 | import sys |
| 30 | import ast |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 31 | import getopt |
| 32 | import struct |
| 33 | import array |
Martin v. Löwis | b6b8110 | 2010-06-04 18:40:55 +0000 | [diff] [blame] | 34 | from email.parser import HeaderParser |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 35 | |
Cheryl Sabella | 637a33b | 2018-11-07 09:12:20 -0500 | [diff] [blame^] | 36 | __version__ = "1.2" |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 37 | |
| 38 | MESSAGES = {} |
| 39 | |
| 40 | |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 41 | def usage(code, msg=''): |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 42 | print(__doc__, file=sys.stderr) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 43 | if msg: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 44 | print(msg, file=sys.stderr) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 45 | sys.exit(code) |
| 46 | |
| 47 | |
Cheryl Sabella | 637a33b | 2018-11-07 09:12:20 -0500 | [diff] [blame^] | 48 | def add(ctxt, id, str, fuzzy): |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 49 | "Add a non-fuzzy translation to the dictionary." |
| 50 | global MESSAGES |
| 51 | if not fuzzy and str: |
Cheryl Sabella | 637a33b | 2018-11-07 09:12:20 -0500 | [diff] [blame^] | 52 | if ctxt is None: |
| 53 | MESSAGES[id] = str |
| 54 | else: |
| 55 | MESSAGES[b"%b\x04%b" % (ctxt, id)] = str |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 56 | |
| 57 | |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 58 | def generate(): |
| 59 | "Return the generated output." |
| 60 | global MESSAGES |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 61 | # the keys are sorted in the .mo file |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 62 | keys = sorted(MESSAGES.keys()) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 63 | offsets = [] |
Martin v. Löwis | b6b8110 | 2010-06-04 18:40:55 +0000 | [diff] [blame] | 64 | ids = strs = b'' |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 65 | for id in keys: |
| 66 | # For each string, we need size and file offset. Each string is NUL |
| 67 | # terminated; the NUL does not count into the size. |
| 68 | offsets.append((len(ids), len(id), len(strs), len(MESSAGES[id]))) |
Martin v. Löwis | b6b8110 | 2010-06-04 18:40:55 +0000 | [diff] [blame] | 69 | ids += id + b'\0' |
| 70 | strs += MESSAGES[id] + b'\0' |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 71 | output = '' |
| 72 | # The header is 7 32-bit unsigned integers. We don't use hash tables, so |
| 73 | # the keys start right after the index tables. |
| 74 | # translated string. |
| 75 | keystart = 7*4+16*len(keys) |
| 76 | # and the values start after the keys |
| 77 | valuestart = keystart + len(ids) |
| 78 | koffsets = [] |
| 79 | voffsets = [] |
| 80 | # The string table first has the list of keys, then the list of values. |
| 81 | # Each entry has first the size of the string, then the file offset. |
| 82 | for o1, l1, o2, l2 in offsets: |
| 83 | koffsets += [l1, o1+keystart] |
| 84 | voffsets += [l2, o2+valuestart] |
| 85 | offsets = koffsets + voffsets |
Martin v. Löwis | 8f0bd56 | 2003-05-09 08:59:17 +0000 | [diff] [blame] | 86 | output = struct.pack("Iiiiiii", |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 87 | 0x950412de, # Magic |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 88 | 0, # Version |
| 89 | len(keys), # # of entries |
| 90 | 7*4, # start of key index |
| 91 | 7*4+len(keys)*8, # start of value index |
| 92 | 0, 0) # size and offset of hash table |
Xtreak | a692efe | 2018-07-21 11:52:12 +0530 | [diff] [blame] | 93 | output += array.array("i", offsets).tobytes() |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 94 | output += ids |
| 95 | output += strs |
| 96 | return output |
| 97 | |
| 98 | |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 99 | def make(filename, outfile): |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 100 | ID = 1 |
| 101 | STR = 2 |
Cheryl Sabella | 637a33b | 2018-11-07 09:12:20 -0500 | [diff] [blame^] | 102 | CTXT = 3 |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 103 | |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 104 | # Compute .mo name from .po name and arguments |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 105 | if filename.endswith('.po'): |
| 106 | infile = filename |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 107 | else: |
| 108 | infile = filename + '.po' |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 109 | if outfile is None: |
| 110 | outfile = os.path.splitext(infile)[0] + '.mo' |
| 111 | |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 112 | try: |
Xtreak | a692efe | 2018-07-21 11:52:12 +0530 | [diff] [blame] | 113 | with open(infile, 'rb') as f: |
| 114 | lines = f.readlines() |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 115 | except IOError as msg: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 116 | print(msg, file=sys.stderr) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 117 | sys.exit(1) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 118 | |
Cheryl Sabella | 637a33b | 2018-11-07 09:12:20 -0500 | [diff] [blame^] | 119 | section = msgctxt = None |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 120 | fuzzy = 0 |
| 121 | |
Martin v. Löwis | b6b8110 | 2010-06-04 18:40:55 +0000 | [diff] [blame] | 122 | # Start off assuming Latin-1, so everything decodes without failure, |
| 123 | # until we know the exact encoding |
| 124 | encoding = 'latin-1' |
| 125 | |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 126 | # Parse the catalog |
| 127 | lno = 0 |
| 128 | for l in lines: |
Martin v. Löwis | b6b8110 | 2010-06-04 18:40:55 +0000 | [diff] [blame] | 129 | l = l.decode(encoding) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 130 | lno += 1 |
| 131 | # If we get a comment line after a msgstr, this is a new entry |
| 132 | if l[0] == '#' and section == STR: |
Cheryl Sabella | 637a33b | 2018-11-07 09:12:20 -0500 | [diff] [blame^] | 133 | add(msgctxt, msgid, msgstr, fuzzy) |
| 134 | section = msgctxt = None |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 135 | fuzzy = 0 |
| 136 | # Record a fuzzy mark |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 137 | if l[:2] == '#,' and 'fuzzy' in l: |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 138 | fuzzy = 1 |
| 139 | # Skip comments |
| 140 | if l[0] == '#': |
| 141 | continue |
Cheryl Sabella | 637a33b | 2018-11-07 09:12:20 -0500 | [diff] [blame^] | 142 | # Now we are in a msgid or msgctxt section, output previous section |
| 143 | if l.startswith('msgctxt'): |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 144 | if section == STR: |
Cheryl Sabella | 637a33b | 2018-11-07 09:12:20 -0500 | [diff] [blame^] | 145 | add(msgctxt, msgid, msgstr, fuzzy) |
| 146 | section = CTXT |
| 147 | l = l[7:] |
| 148 | msgctxt = b'' |
| 149 | elif l.startswith('msgid') and not l.startswith('msgid_plural'): |
| 150 | if section == STR: |
| 151 | add(msgctxt, msgid, msgstr, fuzzy) |
Martin v. Löwis | b6b8110 | 2010-06-04 18:40:55 +0000 | [diff] [blame] | 152 | if not msgid: |
| 153 | # See whether there is an encoding declaration |
| 154 | p = HeaderParser() |
| 155 | charset = p.parsestr(msgstr.decode(encoding)).get_content_charset() |
| 156 | if charset: |
| 157 | encoding = charset |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 158 | section = ID |
| 159 | l = l[5:] |
Martin v. Löwis | b6b8110 | 2010-06-04 18:40:55 +0000 | [diff] [blame] | 160 | msgid = msgstr = b'' |
Martin v. Löwis | cb081b8 | 2010-06-04 18:14:42 +0000 | [diff] [blame] | 161 | is_plural = False |
| 162 | # This is a message with plural forms |
| 163 | elif l.startswith('msgid_plural'): |
| 164 | if section != ID: |
Ezio Melotti | 7c4a7e6 | 2013-08-26 01:32:56 +0300 | [diff] [blame] | 165 | print('msgid_plural not preceded by msgid on %s:%d' % (infile, lno), |
Martin v. Löwis | cb081b8 | 2010-06-04 18:14:42 +0000 | [diff] [blame] | 166 | file=sys.stderr) |
| 167 | sys.exit(1) |
| 168 | l = l[12:] |
Martin v. Löwis | b6b8110 | 2010-06-04 18:40:55 +0000 | [diff] [blame] | 169 | msgid += b'\0' # separator of singular and plural |
Martin v. Löwis | cb081b8 | 2010-06-04 18:14:42 +0000 | [diff] [blame] | 170 | is_plural = True |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 171 | # Now we are in a msgstr section |
| 172 | elif l.startswith('msgstr'): |
| 173 | section = STR |
Martin v. Löwis | cb081b8 | 2010-06-04 18:14:42 +0000 | [diff] [blame] | 174 | if l.startswith('msgstr['): |
| 175 | if not is_plural: |
Martin v. Löwis | 25fcd39 | 2010-07-11 17:39:46 +0000 | [diff] [blame] | 176 | print('plural without msgid_plural on %s:%d' % (infile, lno), |
Martin v. Löwis | cb081b8 | 2010-06-04 18:14:42 +0000 | [diff] [blame] | 177 | file=sys.stderr) |
| 178 | sys.exit(1) |
| 179 | l = l.split(']', 1)[1] |
| 180 | if msgstr: |
Martin v. Löwis | b6b8110 | 2010-06-04 18:40:55 +0000 | [diff] [blame] | 181 | msgstr += b'\0' # Separator of the various plural forms |
Martin v. Löwis | cb081b8 | 2010-06-04 18:14:42 +0000 | [diff] [blame] | 182 | else: |
| 183 | if is_plural: |
Martin v. Löwis | 25fcd39 | 2010-07-11 17:39:46 +0000 | [diff] [blame] | 184 | print('indexed msgstr required for plural on %s:%d' % (infile, lno), |
Martin v. Löwis | cb081b8 | 2010-06-04 18:14:42 +0000 | [diff] [blame] | 185 | file=sys.stderr) |
| 186 | sys.exit(1) |
| 187 | l = l[6:] |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 188 | # Skip empty lines |
| 189 | l = l.strip() |
| 190 | if not l: |
| 191 | continue |
Ezio Melotti | 9bf379e | 2012-11-09 11:46:19 +0100 | [diff] [blame] | 192 | l = ast.literal_eval(l) |
Cheryl Sabella | 637a33b | 2018-11-07 09:12:20 -0500 | [diff] [blame^] | 193 | if section == CTXT: |
| 194 | msgctxt += l.encode(encoding) |
| 195 | elif section == ID: |
Martin v. Löwis | b6b8110 | 2010-06-04 18:40:55 +0000 | [diff] [blame] | 196 | msgid += l.encode(encoding) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 197 | elif section == STR: |
Martin v. Löwis | b6b8110 | 2010-06-04 18:40:55 +0000 | [diff] [blame] | 198 | msgstr += l.encode(encoding) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 199 | else: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 200 | print('Syntax error on %s:%d' % (infile, lno), \ |
| 201 | 'before:', file=sys.stderr) |
| 202 | print(l, file=sys.stderr) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 203 | sys.exit(1) |
| 204 | # Add last entry |
| 205 | if section == STR: |
Cheryl Sabella | 637a33b | 2018-11-07 09:12:20 -0500 | [diff] [blame^] | 206 | add(msgctxt, msgid, msgstr, fuzzy) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 207 | |
| 208 | # Compute output |
| 209 | output = generate() |
| 210 | |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 211 | try: |
Xtreak | a692efe | 2018-07-21 11:52:12 +0530 | [diff] [blame] | 212 | with open(outfile,"wb") as f: |
| 213 | f.write(output) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 214 | except IOError as msg: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 215 | print(msg, file=sys.stderr) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 216 | |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 217 | |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 218 | def main(): |
| 219 | try: |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 220 | opts, args = getopt.getopt(sys.argv[1:], 'hVo:', |
| 221 | ['help', 'version', 'output-file=']) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 222 | except getopt.error as msg: |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 223 | usage(1, msg) |
| 224 | |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 225 | outfile = None |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 226 | # parse options |
| 227 | for opt, arg in opts: |
| 228 | if opt in ('-h', '--help'): |
| 229 | usage(0) |
| 230 | elif opt in ('-V', '--version'): |
Serhiy Storchaka | c56894d | 2013-09-05 17:44:53 +0300 | [diff] [blame] | 231 | print("msgfmt.py", __version__) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 232 | sys.exit(0) |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 233 | elif opt in ('-o', '--output-file'): |
| 234 | outfile = arg |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 235 | # do it |
| 236 | if not args: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 237 | print('No input file given', file=sys.stderr) |
| 238 | print("Try `msgfmt --help' for more information.", file=sys.stderr) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 239 | return |
| 240 | |
| 241 | for filename in args: |
Barry Warsaw | 78d7dc4 | 2001-03-02 16:53:54 +0000 | [diff] [blame] | 242 | make(filename, outfile) |
Barry Warsaw | 72dacb8 | 2000-09-01 08:10:08 +0000 | [diff] [blame] | 243 | |
| 244 | |
| 245 | if __name__ == '__main__': |
| 246 | main() |