]> git.lyx.org Git - lyx.git/blob - po/postats.py
* create global menubar on Mac without a parent. It will be shown if no GuiView exists.
[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 lyx_version po_files > "pathToWebPages"/i18n.inc
28 """
29
30 # modify this when you change branch
31 # Note that an empty lyx_branch variable (ie svn trunk)
32 # will "do the right thing".
33 lyx_branch=""
34
35 import os
36 import sys
37
38 # Reset the locale
39 import locale
40 locale.setlocale(locale.LC_ALL, 'C') 
41 os.environ['LC_ALL'] = 'C'
42
43 def extract_number(line, issues, prop):
44     """
45     line is a string like
46     '588 translated messages, 1248 fuzzy translations, 2 untranslated messages.'
47     Any one of these substrings may not appear if the associated number is 0.
48
49     issues is the set of words following the number to be extracted,
50     ie, 'translated', 'fuzzy', or 'untranslated'.
51
52     extract_number returns a list with those numbers, or sets it to
53     zero if the word is not found in the string.
54     """
55
56     for issue in issues:
57         i = line.find(issue)
58
59         if i == -1:
60             prop[issue] = 0
61         else:
62             prop[issue] = int(line[:i].split()[-1])
63
64
65 def read_pofile(pofile):
66     """ Read the header of the pofile and return it as a dictionary"""
67     header = {}
68     read_header = False
69     for line in open(pofile):
70         line = line[:-1]
71         if line[:5] == 'msgid':
72             if read_header:
73                 break
74             read_header = True
75             continue
76
77         if not line or line[0] == '#' or line == 'msgstr ""' or not read_header:
78             continue
79
80         line = line.strip('"')
81         args = line.split(': ')
82         if len(args) == 1:
83             continue
84         header[args[0]] = args[1].strip()[:-2]
85
86     return header
87
88
89 def run_msgfmt(pofile):
90     """ pofile is the name of the po file.
91  The function runs msgfmt on it and returns corresponding php code.
92 """
93     if not pofile.endswith('.po'):
94         print >> sys.stderr, "%s is not a po file" % pofile
95         sys.exit(1)
96
97     dirname = os.path.dirname(pofile)
98     gmofile = pofile.replace('.po', '.gmo')
99
100     header = read_pofile(pofile)
101     charset= header['Content-Type'].split('charset=')[1]
102
103     # po file properties
104     prop = {}
105     prop["langcode"] = os.path.basename(pofile)[:-3]
106     prop["date"] = header['PO-Revision-Date'].split()[0]
107     prop["email"] = header['Last-Translator'].split('<')[1][:-1]
108     prop["email"] = prop["email"].replace("@", " () ")
109     prop["email"] = prop["email"].replace(".", " ! ")
110     translator = header['Last-Translator'].split('<')[0].strip()
111     try:
112         prop["translator"] = translator.decode(charset).encode('ascii','xmlcharrefreplace')
113     except LookupError:
114         prop["translator"] = translator
115
116     p_in, p_out = os.popen4("msgfmt --statistics -o %s %s" % (gmofile, pofile))
117     extract_number(p_out.readline(),
118                    ('translated', 'fuzzy', 'untranslated'),
119                    prop)
120     return """
121 array ( 'langcode' => '%(langcode)s', "date" => "%(date)s",
122 "msg_tr" => %(translated)d, "msg_fu" => %(fuzzy)d, "msg_nt" => %(untranslated)d,
123 "translator" => "%(translator)s", "email" => "%(email)s")""" % prop
124
125
126 if __name__ == "__main__":
127     if lyx_branch:
128         branch_tag = "branches/%s" % lyx_branch
129     else:
130         branch_tag = "trunk"
131
132
133     print """<?php
134 // The current version
135 $lyx_version = "%s";
136 // The branch tag
137 $branch_tag = "%s";
138
139 // The data itself
140 $podata = array (%s
141 )?>
142 """ % (sys.argv[1], branch_tag, ",".join([run_msgfmt(po) for po in sys.argv[2:]]))