]> git.lyx.org Git - lyx.git/blob - po/postats.py
Embedding: initialize enableCB checkbox correctly
[lyx.git] / po / postats.py
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2007 Michael Gerz <michael.gerz@teststep.org>
4 # Copyright (C) 2007 José Matos <jamatos@lyx.org>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 """
21 This script extracts some information from the po file headers (last
22 translator, revision date), generates the corresponding gmo files
23 to retrieve the number of translated/fuzzy/untranslated messages,
24 and generates a PHP web page.
25
26 Invocation:
27    postats.py po_files > "pathToWebPages"/i18n.inc
28 """
29
30 # modify this when you change version
31 # Note that an empty lyx_branch variable (ie svn trunk)
32 # will "do the right thing".
33 lyx_version="1.6.0svn"
34 lyx_branch=""
35
36 import os
37 import sys
38
39 def extract_number(line, issues, prop):
40     """
41     line is a string like
42     '588 translated messages, 1248 fuzzy translations, 2 untranslated messages.'
43     Any one of these substrings may not appear if the associated number is 0.
44
45     issues is the set of words following the number to be extracted,
46     ie, 'translated', 'fuzzy', or 'untranslated'.
47
48     extract_number returns a list with those numbers, or sets it to
49     zero if the word is not found in the string.
50     """
51
52     for issue in issues:
53         i = line.find(issue)
54
55         if i == -1:
56             prop[issue] = 0
57         else:
58             prop[issue] = int(line[:i].split()[-1])
59
60
61 def read_pofile(pofile):
62     """ Read the header of the pofile and return it as a dictionary"""
63     header = {}
64     read_header = False
65     for line in open(pofile):
66         line = line[:-1]
67         if line[:5] == 'msgid':
68             if read_header:
69                 break
70             read_header = True
71             continue
72
73         if not line or line[0] == '#' or line == 'msgstr ""' or not read_header:
74             continue
75
76         line = line.strip('"')
77         args = line.split(': ')
78         if len(args) == 1:
79             continue
80         header[args[0]] = args[1].strip()[:-2]
81
82     return header
83
84
85 def run_msgfmt(pofile):
86     """ pofile is the name of the po file.
87  The function runs msgfmt on it and returns corresponding php code.
88 """
89     if not pofile.endswith('.po'):
90         print >> sys.stderr, "%s is not a po file" % pofile
91         sys.exit(1)
92
93     dirname = os.path.dirname(pofile)
94     gmofile = pofile.replace('.po', '.gmo')
95
96     header = read_pofile(pofile)
97     charset= header['Content-Type'].split('charset=')[1]
98
99     # po file properties
100     prop = {}
101     prop["langcode"] = os.path.basename(pofile)[:-3]
102     prop["date"] = header['PO-Revision-Date'].split()[0]
103     prop["email"] = header['Last-Translator'].split('<')[1][:-1]
104     translator = header['Last-Translator'].split('<')[0].strip()
105     try:
106         prop["translator"] = translator.decode(charset).encode('ascii','xmlcharrefreplace')
107     except LookupError:
108         prop["translator"] = translator
109
110     p_in, p_out = os.popen4("msgfmt --statistics -o %s %s" % (gmofile, pofile))
111     extract_number(p_out.readline(),
112                    ('translated', 'fuzzy', 'untranslated'),
113                    prop)
114     return """
115 array ( 'langcode' => '%(langcode)s', "date" => "%(date)s",
116 "msg_tr" => %(translated)d, "msg_fu" => %(fuzzy)d, "msg_nt" => %(untranslated)d,
117 "translator" => "%(translator)s", "email" => "%(email)s")""" % prop
118
119
120 if __name__ == "__main__":
121     if lyx_branch:
122         branch_tag = "branches/%s" % lyx_branch
123     else:
124         branch_tag = "trunk"
125
126
127     print """<?php
128 // The current version
129 \$lyx_version = "%s";
130 // The branch tag
131 \$branch_tag = "%s";
132
133 // The data itself
134 \$podata = array (%s
135 )?>
136 """ % (lyx_version, branch_tag, ",".join([run_msgfmt(po) for po in sys.argv[1:]]))