Back to index

moin  1.9.0~rc2
gettext.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-15 -*-
00002 """
00003     MoinMoin - GetText .po parser for moin 1.3.x
00004 
00005     Usage:
00006     ======
00007 
00008     Either begin your page like this:
00009     #format gettext
00010     (followed by .po file content only)
00011 
00012     Or attach the .po file and inline it:
00013     ... (wiki markup) ...
00014     inline:de.po
00015     ... (wiki markup) ...
00016 
00017     Of course, if you just attach the file, you won't be able to edit it in
00018     the wiki. So the #format method is better for online editing.
00019 
00020     Requirements:
00021     =============
00022 
00023     * requires Python 2.4 and installed "gettext" package (msgfmt)
00024     * requires wiki page content to be in config.charset, so do not put non-
00025       utf-8 content into a utf-8 wiki or it will crash.
00026 
00027     @copyright: 2005 by MoinMoin:AlexanderSchremmer
00028     @license: GNU GPL, see COPYING for details.
00029 """
00030 
00031 Dependencies = []
00032 
00033 import subprocess
00034 from MoinMoin import config
00035 
00036 class Parser:
00037     extensions = ['.po']
00038 
00039     def __init__(self, raw, request, **kw):
00040         self.raw = raw
00041         self.request = request
00042 
00043     def format(self, formatter):
00044         PIPE = subprocess.PIPE
00045         STDOUT = subprocess.STDOUT
00046         p = subprocess.Popen(("msgfmt", "-c", "--statistics", "-", ), shell=False,
00047                              stdin=PIPE, stdout=PIPE, stderr=STDOUT)
00048 
00049         charset = config.charset
00050         textin = self.raw.encode(charset)
00051         out = p.communicate(input=textin)[0]
00052         if out is None:
00053             out = ''
00054         out = out.decode(charset).replace('<stdin>:', 'input data line ')
00055 
00056         # show po file data with line numbers as msgfmt refers to them
00057         text, lineno = [], 0
00058         for l in self.raw.splitlines():
00059             lineno += 1
00060             text.append("%04d: %s" % (lineno, l))
00061 
00062         textout = [formatter.heading(1, 3),
00063                    'Gettext status messages:',
00064                    formatter.heading(0, 3),
00065                    formatter.preformatted(1),
00066                    formatter.text(out),
00067                    formatter.preformatted(0),
00068                    formatter.heading(1, 3),
00069                    'Input:',
00070                    formatter.heading(0, 3),
00071                    formatter.preformatted(1),
00072                    formatter.text("\n".join(text)),
00073                    formatter.preformatted(0),
00074                   ]
00075 
00076         self.request.write(''.join(textout))
00077