Wesnoth localization tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
12KB

  1. #!/bin/bash
  2. # Check translations, generate verbose statistics.
  3. # Copyright (C) 2004 Recai Oktas <roktas@omu.edu.tr>
  4. # Copyright (C) 2007-2008 Andrius Štikonas <stikonas@gmail.com> (my code can also be used under GPLv3 or any later)
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License version 2 as published by
  8. # the Free Software Foundation.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. source variables
  18. LC_NUMERIC="en_US.UTF-8"
  19. ODD_CHARS='�'
  20. CL=0 # counter for css style
  21. STATUS="OK" # Total status of translations
  22. if [ ! $MSGFMT ]; then
  23. echo -e "\E[31mThis program needs the msgfmt utility."; tput sgr0
  24. exit 1
  25. fi
  26. if [ ! $ISUTF8 ]; then
  27. echo -e "\E[31mThis program needs the isutf8 utility."; tput sgr0
  28. exit 1
  29. fi
  30. for BRANCH in $TRUNK $BRANCHES; do
  31. echo -e "Repository: \E[34m$BRANCH"; tput sgr0
  32. DIR="$PREFIX-$BRANCH"
  33. STATSFILE="statistics-$BRANCH.xhtml"
  34. ERRORS="translation-errors-$BRANCH"
  35. nerror=0; nwarn=0; npo=0; nt=0; nu=0; nf=0
  36. rm -f $STATSFILE
  37. echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"" >> $STATSFILE
  38. echo "\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">" >> $STATSFILE
  39. echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">" >> $STATSFILE
  40. echo "<head>" >> $STATSFILE
  41. echo " <meta http-equiv=\"content-type\" content=\"application/xhtml+xml; charset=utf-8\" />" >> $STATSFILE
  42. echo " <link rel=\"shortcut icon\" type=\"image/png\" href=\"http://www.wesnoth.org/mw/skins/glamdrol/ico.png\" />" >> $STATSFILE
  43. echo " <style type=\"text/css\">@import url('http://www.wesnoth.org/mw/skins/glamdrol/main.css');</style>" >> $STATSFILE
  44. echo " <link rel=\"stylesheet\" type=\"text/css\" href=\"http://gettext.wesnoth.org/styles/old.css\" />" >> $STATSFILE
  45. echo " <title>Battle for Wesnoth</title>" >> $STATSFILE
  46. echo "</head>" >> $STATSFILE
  47. echo "<body>" >> $STATSFILE
  48. echo "" >> $STATSFILE
  49. echo "<div id=\"global\">" >> $STATSFILE
  50. echo "<div id=\"header\">" >> $STATSFILE
  51. echo " <div id=\"logo\">" >> $STATSFILE
  52. echo " <a href=\"http://www.wesnoth.org/\"><img alt=\"Wesnoth logo\" src=\"http://www.wesnoth.org/mw/skins/glamdrol/wesnoth-logo.jpg\" /></a>" >> $STATSFILE
  53. echo " </div>" >> $STATSFILE
  54. echo "</div>" >> $STATSFILE
  55. echo "" >> $STATSFILE
  56. echo "<div id=\"nav\">" >> $STATSFILE
  57. echo "<ul>" >> $STATSFILE
  58. echo " <li><a href=\"http://www.wesnoth.org/\">Home</a></li>" >> $STATSFILE
  59. echo " <li><a href=\"http://www.wesnoth.org/wiki/Play\">Play</a></li>" >> $STATSFILE
  60. echo "" >> $STATSFILE
  61. echo " <li><a href=\"http://www.wesnoth.org/wiki/Create\">Create</a></li>" >> $STATSFILE
  62. echo " <li><a href=\"http://www.wesnoth.org/forum/\">Forums</a></li>" >> $STATSFILE
  63. echo " <li><a href=\"http://www.wesnoth.org/wiki/Support\">Support</a></li>" >> $STATSFILE
  64. echo " <li><a href=\"http://www.wesnoth.org/wiki/Project\">Project</a></li>" >> $STATSFILE
  65. echo " <li><a href=\"http://www.wesnoth.org/wiki/Credits\">Credits</a></li>" >> $STATSFILE
  66. echo "</ul>" >> $STATSFILE
  67. echo "</div>" >> $STATSFILE
  68. echo "" >> $STATSFILE
  69. echo "<h2 style=\"display:inline\">Wesnoth translation stats</h2>" >> $STATSFILE
  70. echo "<div> <br/> </div><p style=\"display:inline\">Last update: <strong>" >> $STATSFILE
  71. LC_TIME="en_US.UTF-8" date >> $STATSFILE
  72. echo "</strong></p>" >> $STATSFILE
  73. echo "<div> <br/> </div>" >> $STATSFILE
  74. echo "<table class=\"main\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\"><tr><td>" >> $STATSFILE
  75. echo "<table cellspacing=\"1\" cellpadding=\"2\" border=\"0\" width=\"100%\">" >> $STATSFILE
  76. echo "<tr class=\"header\">" >> $STATSFILE
  77. echo "<td class=\"title\">File</td>" >> $STATSFILE
  78. echo "<td class=\"title\" align=\"center\">Status</td>" >> $STATSFILE
  79. echo "<td class=\"translated\">translated</td>" >> $STATSFILE
  80. echo "<td class=\"translated\"><strong>%</strong></td>" >> $STATSFILE
  81. echo "" >> $STATSFILE
  82. echo "<td class=\"fuzzy\"><strong>fuzzy</strong></td>" >> $STATSFILE
  83. echo "<td class=\"fuzzy\"><strong>%</strong></td>" >> $STATSFILE
  84. echo "<td class=\"untranslated\"><strong>untranslated</strong></td>" >> $STATSFILE
  85. echo "<td class=\"untranslated\"><strong>%</strong></td>" >> $STATSFILE
  86. echo "<td class=\"title\">total</td>" >> $STATSFILE
  87. echo "<td class=\"title\">graph</td>" >> $STATSFILE
  88. echo "</tr>" >> $STATSFILE
  89. for dir in `ls $BRANCH/wesnoth* -d`; do
  90. file=$DIR"/"`basename $dir`"."$LCODE".po";
  91. if [ ! -f $file ]; then
  92. file=$BRANCH"/"`basename $dir`"/"$LCODE".po"
  93. filename=$BRANCH`basename $dir`"/"$dir"."$LCODE
  94. else
  95. filename=`echo $file | sed s/.po//`
  96. fi
  97. npo=$(($npo + 1)); has_errors="no"; has_warnings="no";
  98. # Extract the declared charset, may be we use this in future.
  99. charset=`sed -n '/^"Content-Type:/ s/.*charset=\([a-zA-Z0-9_-]*\).*/\1/p' $file`
  100. echo -n "Checking $file for UTF-8 inconsistencies... "
  101. if $ISUTF8 <$file >/dev/null 2>&1; then
  102. if ! echo $charset | grep -qi "UTF-8"; then
  103. has_errors="yes"
  104. echo -e "\E[31mFAILED; non UTF-8 charset declaration for UTF-8 content"; tput sgr0
  105. echo "$file: non UTF-8 charset declared for UTF-8 content; expect to see 'charset=UTF-8'." >> $ERRORS
  106. else
  107. echo -e "\E[32mOK"; tput sgr0
  108. fi
  109. else
  110. if echo $charset | grep -qi "UTF-8"; then
  111. has_errors="yes"
  112. echo -e "\E[31mFAILED; non UTF-8 content for UTF-8 charset declaration"; tput sgr0
  113. echo "$file: UTF-8 charset declared for non UTF-8 content; expect to see a non UTF-8 charset." >> $ERRORS
  114. else
  115. echo -e "\E[32mOK"; tput sgr0
  116. fi
  117. fi
  118. echo -n "Checking $file for odd characters... "
  119. if grep -nH $ODD_CHARS $file >> $ERRORS; then
  120. has_warnings="yes"
  121. echo -e "\E[31mFAILED; odd characters found"; tput sgr0
  122. echo "$file: one or more odd character from the '$ODD_CHARS' set found." >> $ERRORS
  123. else
  124. echo -e "\E[32mOK"; tput sgr0
  125. fi
  126. # Makes sense only if the file passed the UTF-8 check.
  127. if [ "$has_errors" = "no" ]; then
  128. echo -n "Checking $file with msgfmt... "
  129. if ! LC_MESSAGES=C $MSGFMT --check -o /dev/null $file >> $ERRORS; then
  130. has_errors="yes"
  131. echo -e "\E[31mFAILED"; tput sgr0
  132. else
  133. echo -e "\E[32mOK"; tput sgr0
  134. fi
  135. fi
  136. r=0; rf=0; ru=0; t=0; u=0; f=0; rt=0
  137. if [ "$has_errors" = "yes" ]; then
  138. nerror=$(($nerror + 1))
  139. sum=0
  140. st="Error"
  141. if [ $STATUS = "OK" ]; then
  142. STATUS="Error"
  143. fi
  144. if [ $STATUS = "Warning" ]; then
  145. STATUS="Error"
  146. fi
  147. else
  148. stat=`LC_MESSAGES=C $MSGFMT --statistics -o /dev/null $file 2>&1`
  149. t=`echo "$stat" | sed -ne 's/\([0-9]\+\) translated .*/\1/p'`; t=${t:-0}
  150. u=`echo "$stat" | sed -ne 's/.* \([0-9]\+\) untranslated .*/\1/p'`; u=${u:-0}
  151. f=`echo "$stat" | sed -ne 's/.* \([0-9]\+\) fuzzy translation.*/\1/p'`; f=${f:-0}
  152. sum=$(($t + $f + $u))
  153. if [ ! "$sum" -eq 0 ]; then
  154. rt=`printf '%s%d%s%d\n' "scale=2; 100*" $t "/" $sum | bc`
  155. fi
  156. if [ ! "$sum" -eq 0 ]; then
  157. rf=`printf '%s%d%s%d\n' "scale=2; 100*" $f "/" $sum | bc`
  158. fi
  159. if [ ! "$sum" -eq 0 ]; then
  160. ru=`printf '%s%d%s%d\n' "scale=2; 100*" $u "/" $sum | bc`
  161. fi
  162. nt=$(($nt + $t)); nu=$(($nu + $u)); nf=$(($nf + $f))
  163. st="OK"
  164. if [ "$has_warnings" = "yes" ]; then
  165. nwarn=$(($nwarn + 1))
  166. st='Warning'
  167. if [ "$STATUS" = "OK" ]; then
  168. STATUS='Warning'
  169. fi
  170. fi
  171. fi
  172. echo "<tr class=\"row-$CL\">" >> $STATSFILE
  173. CL=$(( ($CL+1)%2 ))
  174. echo " <td align=\"left\"><strong>`basename $filename`</strong></td>" >> $STATSFILE
  175. echo " <td class=\"percentage-$CL\" align=\"center\">$st</td>" >> $STATSFILE
  176. echo " <td class=\"row-$CL\" align=\"right\">$t</td>" >> $STATSFILE
  177. printf '%s%d%s%.2f%s\n' " <td class=\"percentage-" $CL "\" align=\"right\">" $rt "</td>" >> $STATSFILE
  178. echo " <td class=\"row-$CL\" align=\"right\">$f</td>" >> $STATSFILE
  179. printf '%s%d%s%.2f%s\n' " <td class=\"percentage-" $CL "\" align=\"right\">" $rf "</td>" >> $STATSFILE
  180. echo " <td class=\"row-$CL\" align=\"right\">$u</td>" >> $STATSFILE
  181. printf '%s%d%s%.2f%s\n' " <td class=\"percentage-" $CL "\" align=\"right\">" $ru "</td>" >> $STATSFILE
  182. echo " <td class=\"row-$CL\" align=\"right\">$sum</td>" >> $STATSFILE
  183. n4=`printf '%s%f\n' "scale=2; 3*" $rt | bc`
  184. n6=`printf '%s%f\n' "scale=2; 3*" $rf | bc`
  185. n8=`printf '%s%f\n' "scale=2; 3*" $ru | bc`
  186. printf '%s%.2f%s%.2f%s%.2f%s\n' " <td><img src=\"http://gettext.wesnoth.org/images/green.png\" height=\"15\" width=\"" $n4 "\" alt=\"translated\"/><img src=\"http://gettext.wesnoth.org/images/blue.png\" height=\"15\" width=\"" $n6 "\" alt=\"fuzzy\"/><img src=\"http://gettext.wesnoth.org/images/red.png\" height=\"15\" width=\"" $n8 "\" alt=\"untranslated\"/></td>" >> $STATSFILE
  187. echo "</tr>" >> $STATSFILE
  188. done
  189. if [ "$npo" -eq 0 ]; then
  190. echo -e "\E[31mNo '${PO}.po' file under '$DIR'."; tput sgr0
  191. exit 0
  192. fi
  193. if [ "$nerror" -gt 0 ]; then
  194. echo -n -e "\E[31mFatal errors found. See '"
  195. tput smso; echo -n "$ERRORS"; tput rmso
  196. echo -e "' for details."; tput sgr0
  197. else
  198. echo -e "\E[32mNo fatal errors found."; tput sgr0
  199. if [ -s "$ERRORS" ]; then
  200. echo -n -e "\E[31m See '"
  201. tput smso; echo -n "$ERRORS"; tput rmso
  202. echo -e "' for warnings."; tput sgr0
  203. else
  204. rm -f $ERRORS
  205. fi
  206. fi
  207. if [ "$npo" = "$nerror" ]; then
  208. echo -e "\E[31mAll files have fatal errors."; tput sgr0
  209. else
  210. sum=$((${nt} + ${nf} + ${nu}))
  211. nrt=`printf '%s%d%s%d\n' "scale=2; 100*" $nt "/" $sum | bc`
  212. nrf=`printf '%s%d%s%d\n' "scale=2; 100*" $nf "/" $sum | bc`
  213. nru=`printf '%s%d%s%d\n' "scale=2; 100*" $nu "/" $sum | bc`
  214. echo "<tr class=\"title\">" >> $STATSFILE
  215. echo "<td align=\"left\"><strong>Total</strong></td>" >> $STATSFILE
  216. echo "<td align=\"center\"><strong>$STATUS</strong></td>" >> $STATSFILE
  217. echo "<td align=\"right\"><strong>$nt</strong></td>" >> $STATSFILE
  218. printf '%s%.2f%s\n' "<td align=\"right\"><strong>" $nrt "</strong></td>" >> $STATSFILE
  219. echo "<td align=\"right\"><strong>$nf</strong></td>" >> $STATSFILE
  220. printf '%s%.2f%s\n' "<td align=\"right\"><strong>" $nrf "</strong></td>" >> $STATSFILE
  221. echo "<td align=\"right\"><strong>$nu</strong></td>" >> $STATSFILE
  222. printf '%s%.2f%s\n' "<td align=\"right\"><strong>" $nru "</strong></td>" >> $STATSFILE
  223. echo "<td align=\"right\"><strong>$((${nt} + ${nf} + ${nu}))</strong></td>" >> $STATSFILE
  224. nrt=`printf '%s%f\n' "scale=2; 3*" $nrt | bc`
  225. nrf=`printf '%s%f\n' "scale=2; 3*" $nrf | bc`
  226. nru=`printf '%s%f\n' "scale=2; 3*" $nru | bc`
  227. printf '%s%.2f%s%.2f%s%.2f%s\n' "<td><img src=\"http://gettext.wesnoth.org/images/green.png\" height=\"15\" width=\"" $nrt "\" alt=\"translated\"/><img src=\"http://gettext.wesnoth.org/images/blue.png\" height=\"15\" width=\"" $nrf "\" alt=\"fuzzy\"/><img src=\"http://gettext.wesnoth.org/images/red.png\" height=\"15\" width=\"" $nru "\" alt=\"untranslated\"/></td>" >> $STATSFILE
  228. echo "</tr>" >> $STATSFILE
  229. echo "</table>" >> $STATSFILE
  230. echo "</td>" >> $STATSFILE
  231. echo "</tr>" >> $STATSFILE
  232. echo "</table>" >> $STATSFILE
  233. echo "<div> <br/> </div>" >> $STATSFILE
  234. echo "<div id=\"footer\">" >> $STATSFILE
  235. echo "" >> $STATSFILE
  236. echo "<div id=\"footnote\">" >> $STATSFILE
  237. echo "&copy; 2003-"`date +%Y`" The Battle for Wesnoth<br/>" >> $STATSFILE
  238. echo "<br/>" >> $STATSFILE
  239. echo "<a href=\"http://validator.w3.org/check?uri=referer\"><img src=\"http://www.w3.org/Icons/valid-xhtml11-blue\"" >> $STATSFILE
  240. echo "alt=\"Valid XHTML 1.1!\" height=\"31\" width=\"88\" /></a>" >> $STATSFILE
  241. echo "</div>" >> $STATSFILE
  242. echo "</div>" >> $STATSFILE
  243. echo "</div>" >> $STATSFILE
  244. echo "</body>" >> $STATSFILE
  245. echo "</html>" >> $STATSFILE
  246. fi
  247. done
  248. # Exit with error count just because it may sense.
  249. exit $nerror