]> git.lyx.org Git - lyx.git/blob - po/postats.sh
mainly cosmetics
[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.inc
17
18 # modify this when you change version
19 # Note that an empty lyx_branch variable (ie svn trunk)
20 # will "do the right thing".
21 lyx_version=1.6.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 test "$lyx_branch" = "" && {
158         branch_tag="trunk"
159 } || {
160         branch_tag="branches/$lyx_branch"
161 }
162
163 cat <<EOF
164 <?php
165 // The current version
166 \$lyx_version = "$lyx_version";
167 // The branch tag
168 \$branch_tag = "$branch_tag";
169
170 // The data itself
171 \$podata = array (
172 EOF
173 }
174
175
176 # The foot of the generated php file.
177 dump_tail () {
178
179 cat <<EOF
180 ?>
181 EOF
182 }
183
184
185 # The main body of the script
186 msgfmt=`which msgfmt`
187 test $msgfmt != '' || error "Unable to find 'msgfmt'. Cannot proceed."
188
189 dump_head
190
191 while [ $# -ne 0 ]
192 do
193         run_msgfmt $1
194         shift
195         if [ $# -eq 0 ]; then
196                 echo "${output});"
197         else
198                 echo "${output},"
199                 echo
200         fi
201 done
202
203 dump_tail
204 # The end