summaryrefslogtreecommitdiffstatshomepage
path: root/scripts/build/msgfmt.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/build/msgfmt.py')
-rw-r--r--scripts/build/msgfmt.py83
1 files changed, 45 insertions, 38 deletions
diff --git a/scripts/build/msgfmt.py b/scripts/build/msgfmt.py
index 0ac8fd60a8d..f28c2fcf6c6 100644
--- a/scripts/build/msgfmt.py
+++ b/scripts/build/msgfmt.py
@@ -1,14 +1,6 @@
-#! /usr/bin/env python
+#!/usr/bin/env python2
# -*- coding: iso-8859-1 -*-
# Written by Martin v. Löwis <loewis@informatik.hu-berlin.de>
-#
-# Changelog: (Guilherme Polo)
-# 2008-04-11
-# - Support for files with BOM UTF8 mark.
-#
-# 2008-04-10
-# - Support for fuzzy strings in output.
-# - Bumped to version 1.1.1
"""Generate binary message catalog from textual translation description.
@@ -24,10 +16,6 @@ Options:
Specify the output file to write to. If omitted, output will go to a
file named filename.mo (based off the input file name).
- -f
- --use-fuzzy
- Use fuzzy entries in output
-
-h
--help
Print this message and exit.
@@ -35,23 +23,21 @@ Options:
-V
--version
Display version information and exit.
-
-Before using the -f (fuzzy) option, read this:
- http://www.finesheer.com:8457/cgi-bin/info2html?(gettext)Fuzzy%20Entries&lang=en
"""
-import sys
import os
+import sys
+import ast
import getopt
import struct
import array
-import codecs
-__version__ = "1.1.1"
+__version__ = "1.1"
MESSAGES = {}
+
def usage(code, msg=''):
print >> sys.stderr, __doc__
if msg:
@@ -59,13 +45,15 @@ def usage(code, msg=''):
sys.exit(code)
-def add(id, str, fuzzy, use_fuzzy):
- "Add a translation to the dictionary."
+
+def add(id, str, fuzzy):
+ "Add a non-fuzzy translation to the dictionary."
global MESSAGES
- if (not fuzzy or use_fuzzy) and str:
+ if not fuzzy and str:
MESSAGES[id] = str
+
def generate():
"Return the generated output."
global MESSAGES
@@ -108,7 +96,8 @@ def generate():
return output
-def make(filename, outfile, use_fuzzy):
+
+def make(filename, outfile):
ID = 1
STR = 2
@@ -122,8 +111,6 @@ def make(filename, outfile, use_fuzzy):
try:
lines = open(infile).readlines()
- if lines[0].startswith(codecs.BOM_UTF8):
- lines[0] = lines[0][len(codecs.BOM_UTF8):]
except IOError, msg:
print >> sys.stderr, msg
sys.exit(1)
@@ -137,7 +124,7 @@ def make(filename, outfile, use_fuzzy):
lno += 1
# If we get a comment line after a msgstr, this is a new entry
if l[0] == '#' and section == STR:
- add(msgid, msgstr, fuzzy, use_fuzzy)
+ add(msgid, msgstr, fuzzy)
section = None
fuzzy = 0
# Record a fuzzy mark
@@ -147,22 +134,44 @@ def make(filename, outfile, use_fuzzy):
if l[0] == '#':
continue
# Now we are in a msgid section, output previous section
- if l.startswith('msgid'):
+ if l.startswith('msgid') and not l.startswith('msgid_plural'):
if section == STR:
- add(msgid, msgstr, fuzzy, use_fuzzy)
+ add(msgid, msgstr, fuzzy)
section = ID
l = l[5:]
msgid = msgstr = ''
+ is_plural = False
+ # This is a message with plural forms
+ elif l.startswith('msgid_plural'):
+ if section != ID:
+ print >> sys.stderr, 'msgid_plural not preceded by msgid on %s:%d' %\
+ (infile, lno)
+ sys.exit(1)
+ l = l[12:]
+ msgid += '\0' # separator of singular and plural
+ is_plural = True
# Now we are in a msgstr section
elif l.startswith('msgstr'):
section = STR
- l = l[6:]
+ if l.startswith('msgstr['):
+ if not is_plural:
+ print >> sys.stderr, 'plural without msgid_plural on %s:%d' %\
+ (infile, lno)
+ sys.exit(1)
+ l = l.split(']', 1)[1]
+ if msgstr:
+ msgstr += '\0' # Separator of the various plural forms
+ else:
+ if is_plural:
+ print >> sys.stderr, 'indexed msgstr required for plural on %s:%d' %\
+ (infile, lno)
+ sys.exit(1)
+ l = l[6:]
# Skip empty lines
l = l.strip()
if not l:
continue
- # XXX: Does this always follow Python escape semantics?
- l = eval(l)
+ l = ast.literal_eval(l)
if section == ID:
msgid += l
elif section == STR:
@@ -174,7 +183,7 @@ def make(filename, outfile, use_fuzzy):
sys.exit(1)
# Add last entry
if section == STR:
- add(msgid, msgstr, fuzzy, use_fuzzy)
+ add(msgid, msgstr, fuzzy)
# Compute output
output = generate()
@@ -185,15 +194,15 @@ def make(filename, outfile, use_fuzzy):
print >> sys.stderr, msg
+
def main():
try:
- opts, args = getopt.getopt(sys.argv[1:], 'hVo:f',
- ['help', 'version', 'output-file=', 'use-fuzzy'])
+ opts, args = getopt.getopt(sys.argv[1:], 'hVo:',
+ ['help', 'version', 'output-file='])
except getopt.error, msg:
usage(1, msg)
outfile = None
- use_fuzzy = False
# parse options
for opt, arg in opts:
if opt in ('-h', '--help'):
@@ -201,8 +210,6 @@ def main():
elif opt in ('-V', '--version'):
print >> sys.stderr, "msgfmt.py", __version__
sys.exit(0)
- elif opt in ('-f', '--use-fuzzy'):
- use_fuzzy = True
elif opt in ('-o', '--output-file'):
outfile = arg
# do it
@@ -212,7 +219,7 @@ def main():
return
for filename in args:
- make(filename, outfile, use_fuzzy)
+ make(filename, outfile)
if __name__ == '__main__':