]> git.lyx.org Git - lyx.git/blob - po/postats.sh
remove non-ascii characters from translatable strings
[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 Schmitt, michael.schmitt@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.php3
17
18
19 warning () {
20         echo $* 1>&2
21 }
22
23
24 error () {
25         warning $*
26         exit 1
27 }
28
29
30 # $1 is a string like
31 # '588 translated messages, 1248 fuzzy translations, 2 untranslated messages.'
32 # Any one of these substrings may not appear if the associated number is 0.
33
34 # $2 is the word following the number to be extracted,
35 # ie, 'translated', 'fuzzy', or 'untranslated'.
36 #
37 # extract_number fills var $number with this number, or sets it to zero if the
38 # word is not found in the string.
39 extract_number () {
40         test $# -eq 2 || error 'extract_number expects 2 args'
41
42         number=0
43         echo $1 | grep $2 >/dev/null || return
44         # It /is/ safe to use 'Z' as a delimiter here.
45         number=`echo $1 | sed "s/\([0-9]*\)[ ]*$2/Z\1Z/" | cut -d 'Z' -f 2`
46 }
47
48
49 # $template is used by run_msgfmt, below, to fill $output. The function extracts
50 # the appropriate values from the data.
51 template="array ( 'langcode' => 'LC',
52 \"msg_tr\" => TR, \"msg_fu\" => FU, \"msg_nt\" => NT,
53 \"translator\" => \"AUTHOR\", \"email\" => \"EMAIL\",
54 \"date\" => \"DATE\" )"
55 readonly template
56
57
58 # $1 is the name of the po file.
59 #
60 # The function runs msgfmt on it and fills var $output.
61 # All other variables created in the function are unset on exit.
62 run_msgfmt () {
63         test $# -eq 1 || error 'run_msgfmt expects 1 arg'
64
65         output=
66         test -f $1 || {
67                 warning "File $1 does not exist"  
68                 return
69         }
70
71         origdir=`pwd`
72         dir=`dirname $1`
73         pofile=`basename $1`
74         gmofile=`echo $pofile | sed 's/po$/gmo/'`
75         test $pofile != '' -a $pofile != $gmofile || {
76                 warning "File $1 is not a po file"  
77                 unset origdir dir pofile gmofile
78                 return
79         }
80
81         cd $dir
82         unset dir
83
84         langcode=`echo $pofile | sed 's/\.po$//'`
85
86         # Searching for a string of the form
87         # '"PO-Revision-Date: 2003-01-18 03:00+0100\n"'
88         date=`grep 'Revision-Date' $pofile | sed 's/  */ /g' | cut -d ' ' -f 2`
89
90         # Searching for a string of the form
91         # '"Last-Translator: Michael Schmitt <Michael.Schmitt@teststep.org>\n"'
92         translator=
93         email=
94         input=`grep "Last-Translator" $pofile` && {
95                 input=`echo $input | sed 's/  */ /g' | cut -d ' ' -f 2-`
96
97                 translator=`echo $input | cut -d '<' -f 1 | sed 's/ *$//'`
98                 email=`echo $input | cut -d '<' -f 2 | cut -d '>' -f 1`
99         }
100         unset input
101
102         # Run msgfmt on the pofile, filling $message with the raw info.
103         message=`$msgfmt --statistics -o $gmofile $pofile 2>&1 | grep "^[1-9]"` || {
104                 warning "Unable to run msgfmt successfully on file $1"
105                 cg $origdir
106                 unset origdir pofile gmofile
107                 return
108         }
109         unset pofile gmofile
110
111         extract_number "$message" 'translated'
112         translated=$number
113
114         extract_number "$message" 'fuzzy'
115         fuzzy=$number
116
117         extract_number "$message" 'untranslated'
118         untranslated=$number
119         unset message number
120
121         output=`echo "$template" | sed "s/LC/$langcode/; \
122                 s/TR/$translated/; s/FU/$fuzzy/; s/NT/$untranslated/; \
123                 s/AUTHOR/$translator/; s/EMAIL/$email/; s/DATE/$date/"`
124
125         unset langcode date translator email untranslated fuzzy translated
126         cd $origdir
127         unset origdir
128 }
129
130
131 # The head of the generated php file.
132 dump_head () {
133 cat <<EOF
134 <?
135         // What's the title of the page?
136         \$title = "LyX i18n";
137         // What's the short name of the page in the navigation bar?
138         \$item="i18n";
139         // Who is the author?
140         \$author="Michael Schmitt";
141         // Full name of the file (relative path from LyX home page -- i.e., it should
142         // be "foo.php3" or "bar/foo.php3")
143         \$file_full="i18n.php3";
144
145         include("start.php3");
146
147         error_reporting(E_ALL);
148 ?>
149
150 <?
151 \$podata = array (
152 EOF
153 }
154
155
156 # The foot of the generated php file.
157 dump_tail () {
158 cat <<EOF
159 <?
160 \$lang = array(
161         'bg' => 'Bulgarian',
162         'ca' => 'Catalan',
163         'cs' => 'Czech',
164         'da' => 'Danish',
165         'de' => 'German',
166         'es' => 'Spanish',
167         'eu' => 'Basque',
168         'fi' => 'Finnish',
169         'fr' => 'French',
170         'he' => 'Hebrew',
171         'hu' => 'Hungarian',
172         'it' => 'Italian',
173         'nl' => 'Dutch',
174         'no' => 'Norwegian',
175         'pl' => 'Polish',
176         'pt' => 'Portuguese',
177         'ro' => 'Romanian',
178         'ru' => 'Russian',
179         'sk' => 'Slovak',
180         'sl' => 'Slovenian',
181         'sv' => 'Swedish',
182         'tr' => 'Turkish',
183         'wa' => 'Walloon'
184 );
185
186 \$noOfMsg = \$podata[0]['msg_tr'] + \$podata[0]['msg_fu'] + \$podata[0]['msg_nt'];
187
188 function cmp (\$a, \$b) {
189         if (\$a['msg_tr'] == \$b['msg_tr']) {
190                 return 0;
191         }
192         return (\$a['msg_tr'] > \$b['msg_tr']) ? -1 : 1;
193 }
194
195 usort (\$podata, "cmp");
196 ?>
197
198 <p>
199         The following table details the current state of the translations of the
200         LyX GUI for the main LyX development branch (currently 1.3.0cvs).
201         Unfortunately, only a few languages are well-supported. The LyX term may,
202         therefore, decide to exclude some of the translations from a formal
203         release in order not to confuse the user with a strongly mixed-language
204         interface.
205 </p>
206 <p>
207         Explanation:
208 </p>
209 <ul>
210         <li><i>Translated:</i> The number of translated messages</li>
211         <li><i>Fuzzy:</i> The number of fuzzy messages; these are not considered
212             for LyX output but solely serve as a hint for the translators</li>
213         <li><i>Untranslated:</i> The number of untranslated messages; the
214             default language (i.e., English) will be used in the LyX outputs</li>
215 </ul>
216 <table class="center" frame="box" rules="all" border="2" cellpadding="5">
217 <thead>
218         <tr>
219                 <td>Language</td>
220                 <td>Translated</td>
221                 <td>Fuzzy</td>
222                 <td>Untranslated</td>
223                 <td>Revision Date</td>
224                 <td>Translator</td>
225         </tr>
226 </thead>
227 <tbody>
228 <?
229 while (list(\$foo,\$info) = each(\$podata)) {
230         print "<tr>";
231
232         if ( \$info['msg_tr'] > \$noOfMsg * 2 / 3 ) {
233                 \$style="style='background:#009900'";
234         } else if ( \$info['msg_tr'] > \$noOfMsg / 2 ) {
235                 \$style="style='background:#AAAA00'";
236         } else {
237                 \$style="style='background:#AA3333'";
238         }
239
240         print "<td \$style>" . \$lang[\$info['langcode']] . "</td>";
241
242         print "<td \$style align=\"right\">" . \$info['msg_tr'] . "</td>";
243
244         print "<td \$style align=\"right\">";
245         if (isset(\$info['msg_fu'])) {
246                 print \$info['msg_fu'];
247         } else {
248                 print "0";
249         }
250         print "</td>";
251
252         print "<td \$style align=\"right\">";
253         if (isset(\$info['msg_nt'])) {
254                 print \$info['msg_nt'];
255         } else {
256                 print "0";
257         }
258         print "</td>";
259
260         print "<td \$style align=\"center\">" . \$info['date'] . "</td>";
261
262         print "<td \$style>";
263         if (\$info['email'] == "") {
264                 print \$info['translator'];
265         } else {
266                 print "<a href=\"mailto:" . \$info['email'] . "\">" .
267                         \$info['translator'] . "</a>";
268         }
269         print "</td>";
270
271         print "</tr>\n";
272 }
273 ?>
274 </tbody>
275 </table>
276 <?
277 include("end.php3");
278 ?>
279 EOF
280 }
281
282
283 # The main body of the script
284 msgfmt=`which msgfmt`
285 test $msgfmt != '' || error "Unable to find 'msgfmt'. Cannot proceed." 
286
287 dump_head
288
289 while [ $# -ne 0 ]
290 do
291         run_msgfmt $1
292         shift
293         if [ $# -eq 0 ]; then
294                 echo "${output});"
295                 echo '?>'
296         else
297                 echo "${output},"
298                 echo
299         fi
300 done
301
302 dump_tail
303 # The end