2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2007 Michael Gerz <michael.gerz@teststep.org>
4 # Copyright (C) 2007 José Matos <jamatos@lyx.org>
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.
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.
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.
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.
27 postats.py lyx_version po_files > "pathToWebPages"/i18n.inc
29 from __future__ import print_function
31 # modify this when you change branch
32 # Note that an empty lyx_branch variable (ie svn trunk)
33 # will "do the right thing".
35 # these po-files will be skipped:
42 from subprocess import Popen, PIPE
46 locale.setlocale(locale.LC_ALL, 'C')
47 os.environ['LC_ALL'] = 'C'
49 def extract_number(line, issues, prop):
52 '588 translated messages, 1248 fuzzy translations, 2 untranslated messages.'
53 Any one of these substrings may not appear if the associated number is 0.
55 issues is the set of words following the number to be extracted,
56 ie, 'translated', 'fuzzy', or 'untranslated'.
58 extract_number returns a list with those numbers, or sets it to
59 zero if the word is not found in the string.
68 prop[issue] = int(line[:i].split()[-1])
71 def read_pofile(pofile):
72 """ Read the header of the pofile and return it as a dictionary"""
75 for line in codecs.open(pofile, encoding='utf8'):
77 if line[:5] == 'msgid':
83 if not line or line[0] == '#' or line == 'msgstr ""' or not read_header:
86 line = line.strip('"')
87 args = line.split(': ')
90 header[args[0]] = args[1].strip()[:-2]
95 def run_msgfmt(pofile):
96 """ pofile is the name of the po file.
97 The function runs msgfmt on it and returns corresponding php code.
99 if not pofile.endswith('.po'):
100 print("%s is not a po file" % pofile, file=sys.stderr)
103 dirname = os.path.dirname(pofile)
104 gmofile = pofile.replace('.po', '.gmo')
106 header = read_pofile(pofile)
107 charset= header['Content-Type'].split('charset=')[1]
111 prop["langcode"] = os.path.basename(pofile)[:-3]
112 prop["date"] = header['PO-Revision-Date'].split()[0]
113 prop["email"] = header['Last-Translator'].split('<')[1][:-1]
114 prop["email"] = prop["email"].replace("@", " () ")
115 prop["email"] = prop["email"].replace(".", " ! ")
116 prop["translator"] = header['Last-Translator'].split('<')[0].strip()
118 msg = subprocess.check_output(["msgfmt", "--statistics",
119 "-o", gmofile, # FIXME: do we really want a gmofile as side-effect?
120 pofile], stderr=subprocess.STDOUT)
121 if sys.version_info[0] > 2:
122 msg = msg.decode('utf8')
123 extract_number(msg, ('translated', 'fuzzy', 'untranslated'), prop)
125 array ( 'langcode' => '%(langcode)s', "date" => "%(date)s",
126 "msg_tr" => %(translated)d, "msg_fu" => %(fuzzy)d, "msg_nt" => %(untranslated)d,
127 "translator" => "%(translator)s", "email" => "%(email)s")""" % prop
130 if __name__ == "__main__":
132 branch_tag = lyx_branch
134 branch_tag = "master"
138 // The current version
145 )?>""" % (sys.argv[1], branch_tag,
146 ",".join([run_msgfmt(po) for po in sys.argv[2:]
147 if po not in ommitted])))