]> git.lyx.org Git - lyx.git/blob - po/postats.sh
c7a0bd4c5faae99f679fc3eedd1b86f5674f425e
[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         'nl' => 'Dutch',
206         'nn' => 'Nynorsk',
207         'nb' => 'Norwegian',
208         'pl' => 'Polish',
209         'pt' => 'Portuguese',
210         'ro' => 'Romanian',
211         'ru' => 'Russian',
212         'sk' => 'Slovak',
213         'sl' => 'Slovenian',
214         'sv' => 'Swedish',
215         'tr' => 'Turkish',
216         'wa' => 'Walloon'
217 );
218
219 \$noOfMsg = \$podata[0]['msg_tr'] + \$podata[0]['msg_fu'] + \$podata[0]['msg_nt'];
220
221 function cmp (\$a, \$b) {
222         if (\$a['msg_tr'] == \$b['msg_tr']) {
223                 return 0;
224         }
225         return (\$a['msg_tr'] > \$b['msg_tr']) ? -1 : 1;
226 }
227
228 usort (\$podata, "cmp");
229 ?>
230
231 <p>
232         The following table details the current state of the translations of the
233         LyX GUI for the LyX stable branch (currently $lyx_version).
234         Unfortunately, only a few languages are well-supported. The LyX team may,
235         therefore, decide to exclude some of the translations from a formal
236         release in order not to confuse the user with a strongly mixed-language
237         interface.
238 </p>
239 <p>
240         Explanation:
241 </p>
242 <ul>
243         <li><i>Translated:</i> The number of translated messages</li>
244         <li><i>Fuzzy:</i> The number of fuzzy messages; these are not considered
245             for LyX output but solely serve as a hint for the translators</li>
246         <li><i>Untranslated:</i> The number of untranslated messages; the
247             default language (i.e., English) will be used in the LyX outputs</li>
248 </ul>
249 <table class="center" frame="box" rules="all" border="2" cellpadding="5">
250 <thead>
251         <tr>
252                 <td>Language</td>
253                 <td>Translated</td>
254                 <td>Fuzzy</td>
255                 <td>Untranslated</td>
256                 <td>Revision Date</td>
257                 <td>Translator</td>
258         </tr>
259 </thead>
260 <tbody>
261 <?
262 while (list(\$foo,\$info) = each(\$podata)) {
263         print "<tr>";
264
265         if ( \$info['msg_tr'] > \$noOfMsg * 2 / 3 ) {
266                 \$style="style='background:#009900'";
267         } else if ( \$info['msg_tr'] > \$noOfMsg / 2 ) {
268                 \$style="style='background:#AAAA00'";
269         } else {
270                 \$style="style='background:#AA3333'";
271         }
272         print "<td \$style>" ;
273
274         print "<a href=\"http://www.lyx.org/trac/browser/lyx-devel/$branch_tag/po/" . \$info['langcode'] . ".po?format=raw\">" . \$lang[\$info['langcode']] . "</a></td>";
275
276         print "<td \$style align=\"right\">" . \$info['msg_tr'] . "</td>";
277
278         print "<td \$style align=\"right\">";
279         if (isset(\$info['msg_fu'])) {
280                 print \$info['msg_fu'];
281         } else {
282                 print "0";
283         }
284         print "</td>";
285
286         print "<td \$style align=\"right\">";
287         if (isset(\$info['msg_nt'])) {
288                 print \$info['msg_nt'];
289         } else {
290                 print "0";
291         }
292         print "</td>";
293
294         print "<td \$style align=\"center\">" . \$info['date'] . "</td>";
295
296         print "<td \$style>";
297         if (\$info['email'] == "") {
298                 print \$info['translator'];
299         } else {
300                 print "<a href=\"mailto:" . \$info['email'] . "\">" .
301                         \$info['translator'] . "</a>";
302         }
303         print "</td>";
304
305         print "</tr>\n";
306 }
307 ?>
308 </tbody>
309 </table>
310 <?
311 include("end.php");
312 ?>
313 EOF
314 }
315
316
317 # The main body of the script
318 msgfmt=`which msgfmt`
319 test $msgfmt != '' || error "Unable to find 'msgfmt'. Cannot proceed."
320
321 dump_head
322
323 while [ $# -ne 0 ]
324 do
325         run_msgfmt $1
326         shift
327         if [ $# -eq 0 ]; then
328                 echo "${output});"
329                 echo '?>'
330         else
331                 echo "${output},"
332                 echo
333         fi
334 done
335
336 dump_tail
337 # The end