]> git.lyx.org Git - lyx.git/blob - po/postats.sh
lyx_1_5.py: last part of fix for bug 3313
[lyx.git] / po / postats.sh
1 #! /bin/sh
2
3 # file postats.sh
4 #
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7 #
8 # author: Michael Gerz, michael.gerz@teststep.org
9 #
10 # This script extracts some information from the po file headers (last
11 # translator, revision date), generates the corresponding gmo files
12 # to retrieve the number of translated/fuzzy/untranslated messages,
13 # and generates a PHP web page.
14 #
15 # Invocation:
16 #    postats.sh po_files > "pathToWebPages"/i18n.php
17
18 # modifiy this when you change version
19 # Note that an empty lyx_branch variable (ie cvs HEAD)
20 # will "do the right thing".
21 lyx_version=1.5.0svn
22 lyx_branch=
23
24
25 # GNU sed and grep have real problems dealing with 8-bit characters
26 # in UTF-8 encoded environments.
27 unset LANG
28 LANGUAGE=C
29
30 warning () {
31         echo $* 1>&2
32 }
33
34
35 error () {
36         warning $*
37         exit 1
38 }
39
40
41 # $1 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 # $2 is the word following the number to be extracted,
46 # ie, 'translated', 'fuzzy', or 'untranslated'.
47 #
48 # extract_number fills var $number with this number, or sets it to zero if the
49 # word is not found in the string.
50 extract_number () {
51         test $# -eq 2 || error 'extract_number expects 2 args'
52
53         number=0
54         echo $1 | grep $2 >/dev/null || return
55         # It /is/ safe to use 'Z' as a delimiter here.
56         number=`echo $1 | sed "s/\([0-9]*\)[ ]*$2/Z\1Z/" | cut -d 'Z' -f 2`
57 }
58
59
60 # $template is used by run_msgfmt, below, to fill $output. The function extracts
61 # the appropriate values from the data.
62 template="array ( 'langcode' => 'LC',
63 \"msg_tr\" => TR, \"msg_fu\" => FU, \"msg_nt\" => NT,
64 \"translator\" => \"AUTHOR\", \"email\" => \"EMAIL\",
65 \"date\" => \"DATE\" )"
66 readonly template
67
68
69 # $1 is the name of the po file.
70 #
71 # The function runs msgfmt on it and fills var $output.
72 # All other variables created in the function are unset on exit.
73 run_msgfmt () {
74         test $# -eq 1 || error 'run_msgfmt expects 1 arg'
75
76         output=
77         test -f $1 || {
78                 warning "File $1 does not exist"
79                 return
80         }
81
82         origdir=`pwd`
83         dir=`dirname $1`
84         pofile=`basename $1`
85         gmofile=`echo $pofile | sed 's/po$/gmo/'`
86         test $pofile != '' -a $pofile != $gmofile || {
87                 warning "File $1 is not a po file"
88                 unset origdir dir pofile gmofile
89                 return
90         }
91
92         cd $dir
93         unset dir
94
95         langcode=`echo $pofile | sed 's/\.po$//'`
96
97         # Searching for a string of the form
98         # '"PO-Revision-Date: 2003-01-18 03:00+0100\n"'
99         date=`grep 'Revision-Date' $pofile | sed 's/  */ /g' | cut -d ' ' -f 2`
100
101         # Searching for a string of the form
102         # '"Last-Translator: Michael Gerz <Michael.Gerz@teststep.org>\n"'
103         translator=
104         email=
105         input=`grep "Last-Translator" $pofile` && {
106                 input=`echo $input | sed 's/  */ /g' | cut -d ' ' -f 2-`
107
108                 translator=`echo $input | cut -d '<' -f 1 | sed 's/ *$//'`
109                 email=`echo $input | cut -d '<' -f 2 | cut -d '>' -f 1 | sed -e 's/@/ () /' -e 's/\./ ! /g'`
110         }
111         unset input
112
113         # Does $translator contain 8-bit characters?
114         TAB='   '
115         echo $translator | grep "[^${TAB} -~]" >/dev/null && {
116                 # If so, grab the encoding from the po file.
117                 charset=`sed -n '/Content-Type/{s/.*charset=//;s/\\\\n" *$//p;q}' $pofile`
118                 # Use recode to generate HTML character codes for the 8-bit
119                 # characters.
120                 translator=`echo $translator | recode "${charset}..h4"` || exit 1
121                 # The ampersands in the $translator entries will mess things
122                 # up unless we escape 'em.
123                 translator=`echo $translator | sed 's/&/\\\&/g'`
124         }
125
126         # Run msgfmt on the pofile, filling $message with the raw info.
127         message=`$msgfmt --statistics -o $gmofile $pofile 2>&1 | grep "^[1-9]"` || {
128                 warning "Unable to run msgfmt successfully on file $1"
129                 cg $origdir
130                 unset origdir pofile gmofile
131                 return
132         }
133         unset pofile gmofile
134
135         extract_number "$message" 'translated'
136         translated=$number
137
138         extract_number "$message" 'fuzzy'
139         fuzzy=$number
140
141         extract_number "$message" 'untranslated'
142         untranslated=$number
143         unset message number
144
145         output=`echo "$template" | sed "s/LC/$langcode/; \
146                 s/TR/$translated/; s/FU/$fuzzy/; s/NT/$untranslated/; \
147                 s/AUTHOR/$translator/; s/EMAIL/$email/; s/DATE/$date/"`
148
149         unset langcode date translator email untranslated fuzzy translated
150         cd $origdir
151         unset origdir
152 }
153
154
155 # The head of the generated php file.
156 dump_head () {
157 cat <<EOF
158 <?
159         // What's the title of the page?
160         \$title = "LyX i18n";
161         // What's the short name of the page in the navigation bar?
162         \$item="i18n";
163         // Who is the author?
164         \$author="Michael Gerz";
165         // Full name of the file (relative path from LyX home page -- i.e., it should
166         // be "foo.php" or "bar/foo.php")
167         \$file_full="devel/i18n.php";
168
169         include("start.php");
170
171         error_reporting(E_ALL);
172 ?>
173
174 <?
175 \$podata = array (
176 EOF
177 }
178
179
180 # The foot of the generated php file.
181 dump_tail () {
182
183 test "$lyx_branch" = "" && {
184         branch_tag="trunk"
185 } || {
186         branch_tag="branches/$lyx_branch"
187 }
188
189 cat <<EOF
190 <?
191 \$lang = array(
192         'bg' => 'Bulgarian',
193         'ca' => 'Catalan',
194         'cs' => 'Czech',
195         'da' => 'Danish',
196         'de' => 'German',
197         'es' => 'Spanish',
198         'eu' => 'Basque',
199         'fi' => 'Finnish',
200         'fr' => 'French',
201         'gl' => 'Galician',
202         'he' => 'Hebrew',
203         'hu' => 'Hungarian',
204         'it' => 'Italian',
205         'ja' => 'Japanese',
206         'ko' => 'Korean',
207         'nl' => 'Dutch',
208         'nn' => 'Nynorsk',
209         'nb' => 'Norwegian',
210         'pl' => 'Polish',
211         'pt' => 'Portuguese',
212         'ro' => 'Romanian',
213         'ru' => 'Russian',
214         'sk' => 'Slovak',
215         'sl' => 'Slovenian',
216         'sv' => 'Swedish',
217         'tr' => 'Turkish',
218         'wa' => 'Walloon'
219 );
220
221 \$noOfMsg = \$podata[0]['msg_tr'] + \$podata[0]['msg_fu'] + \$podata[0]['msg_nt'];
222
223 function cmp (\$a, \$b) {
224         if (\$a['msg_tr'] == \$b['msg_tr']) {
225                 return 0;
226         }
227         return (\$a['msg_tr'] > \$b['msg_tr']) ? -1 : 1;
228 }
229
230 usort (\$podata, "cmp");
231 ?>
232
233 <p>
234         The following table details the current state of the translations of the
235         LyX GUI for the LyX development branch (currently $lyx_version).
236         Unfortunately, only a few languages are well-supported. The LyX team may,
237         therefore, decide to exclude some of the translations from a formal
238         release in order not to confuse the user with a strongly mixed-language
239         interface.
240 </p>
241 <p>
242         Explanation:
243 </p>
244 <ul>
245         <li><i>Translated:</i> The number of translated messages</li>
246         <li><i>Fuzzy:</i> The number of fuzzy messages; these are not considered
247             for LyX output but solely serve as a hint for the translators</li>
248         <li><i>Untranslated:</i> The number of untranslated messages; the
249             default language (i.e., English) will be used in the LyX outputs</li>
250 </ul>
251 <table class="center" frame="box" rules="all" border="2" cellpadding="5">
252 <thead>
253         <tr>
254                 <td>Language</td>
255                 <td>Translated</td>
256                 <td>Fuzzy</td>
257                 <td>Untranslated</td>
258                 <td>Revision Date</td>
259                 <td>Translator</td>
260         </tr>
261 </thead>
262 <tbody>
263 <?
264 while (list(\$foo,\$info) = each(\$podata)) {
265         print "<tr>";
266
267         if ( \$info['msg_tr'] > \$noOfMsg * 2 / 3 ) {
268                 \$style="style='background:#009900'";
269         } else if ( \$info['msg_tr'] > \$noOfMsg / 2 ) {
270                 \$style="style='background:#AAAA00'";
271         } else {
272                 \$style="style='background:#AA3333'";
273         }
274         print "<td \$style>" ;
275
276         print "<a href=\"http://www.lyx.org/trac/browser/lyx-devel/$branch_tag/po/" . \$info['langcode'] . ".po?format=raw\">" . \$lang[\$info['langcode']] . "</a></td>";
277
278         print "<td \$style align=\"right\">" . \$info['msg_tr'] . "</td>";
279
280         print "<td \$style align=\"right\">";
281         if (isset(\$info['msg_fu'])) {
282                 print \$info['msg_fu'];
283         } else {
284                 print "0";
285         }
286         print "</td>";
287
288         print "<td \$style align=\"right\">";
289         if (isset(\$info['msg_nt'])) {
290                 print \$info['msg_nt'];
291         } else {
292                 print "0";
293         }
294         print "</td>";
295
296         print "<td \$style align=\"center\">" . \$info['date'] . "</td>";
297
298         print "<td \$style>";
299         if (\$info['email'] == "") {
300                 print \$info['translator'];
301         } else {
302                 print "<a href=\"mailto:" . \$info['email'] . "\">" .
303                         \$info['translator'] . "</a>";
304         }
305         print "</td>";
306
307         print "</tr>\n";
308 }
309 ?>
310 </tbody>
311 </table>
312 <?
313 include("end.php");
314 ?>
315 EOF
316 }
317
318
319 # The main body of the script
320 msgfmt=`which msgfmt`
321 test $msgfmt != '' || error "Unable to find 'msgfmt'. Cannot proceed."
322
323 dump_head
324
325 while [ $# -ne 0 ]
326 do
327         run_msgfmt $1
328         shift
329         if [ $# -eq 0 ]; then
330                 echo "${output});"
331                 echo '?>'
332         else
333                 echo "${output},"
334                 echo
335         fi
336 done
337
338 dump_tail
339 # The end