source: trunk/essentials/dev-lang/python/Tools/i18n/msgfmt.py

Last change on this file was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 5.4 KB
Line 
1#! /usr/bin/env python
2# -*- coding: iso-8859-1 -*-
3# Written by Martin v. Löwis <[email protected]>
4
5"""Generate binary message catalog from textual translation description.
6
7This program converts a textual Uniforum-style message catalog (.po file) into
8a binary GNU catalog (.mo file). This is essentially the same function as the
9GNU msgfmt program, however, it is a simpler implementation.
10
11Usage: msgfmt.py [OPTIONS] filename.po
12
13Options:
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
19 -h
20 --help
21 Print this message and exit.
22
23 -V
24 --version
25 Display version information and exit.
26"""
27
28import sys
29import os
30import getopt
31import struct
32import array
33
34__version__ = "1.1"
35
36MESSAGES = {}
37
38
39
40
41def usage(code, msg=''):
42 print >> sys.stderr, __doc__
43 if msg:
44 print >> sys.stderr, msg
45 sys.exit(code)
46
47
48
49
50def add(id, str, fuzzy):
51 "Add a non-fuzzy translation to the dictionary."
52 global MESSAGES
53 if not fuzzy and str:
54 MESSAGES[id] = str
55
56
57
58
59def generate():
60 "Return the generated output."
61 global MESSAGES
62 keys = MESSAGES.keys()
63 # the keys are sorted in the .mo file
64 keys.sort()
65 offsets = []
66 ids = strs = ''
67 for id in keys:
68 # For each string, we need size and file offset. Each string is NUL
69 # terminated; the NUL does not count into the size.
70 offsets.append((len(ids), len(id), len(strs), len(MESSAGES[id])))
71 ids += id + '\0'
72 strs += MESSAGES[id] + '\0'
73 output = ''
74 # The header is 7 32-bit unsigned integers. We don't use hash tables, so
75 # the keys start right after the index tables.
76 # translated string.
77 keystart = 7*4+16*len(keys)
78 # and the values start after the keys
79 valuestart = keystart + len(ids)
80 koffsets = []
81 voffsets = []
82 # The string table first has the list of keys, then the list of values.
83 # Each entry has first the size of the string, then the file offset.
84 for o1, l1, o2, l2 in offsets:
85 koffsets += [l1, o1+keystart]
86 voffsets += [l2, o2+valuestart]
87 offsets = koffsets + voffsets