1#!/usr/bin/env bash
2
3#
4# usage() : short help
5#
6usage() {
7    cat <<EOF
8Usage :
9    $0 [--help] [-e|--export-html] languages
10EOF
11}
12
13#
14# count_msgids() : count the original strings
15#
16count_msgids() {
17    cat | grep -E '^msgid\s+' | wc -l
18}
19
20#
21# count_original_words() : count the words in the original strings
22#
23count_original_words() {
24    cat | grep ^msgid | sed 's/^msgid "//g;s/"$//g' | wc -w
25}
26
27#
28# statistics() : process statistics on translations
29#
30statistics() {
31    PO_FILES="$(mktemp -t XXXXXXXXXX.pofiles)"
32    find . -iname "$lang.po" > $PO_FILES
33    PO_MESSAGES="$(mktemp -t XXXXXXXXXX.po)"
34    msgcat --files-from=$PO_FILES --output=$PO_MESSAGES
35    REV_DATE=$(grep "PO-Revision-Date: [1-9]" $PO_MESSAGES |cut -d " " -f2)
36    TOTAL=$(msgattrib --force-po --no-obsolete $PO_MESSAGES | count_msgids)
37    TOTAL_WC=$(msgattrib --force-po --no-obsolete --no-wrap $PO_MESSAGES | count_original_words)
38    FUZZY=$(msgattrib --force-po --only-fuzzy --no-obsolete $PO_MESSAGES | count_msgids)
39    # Fully translated files always return one remaining fuzzy entry...
40    if [ $FUZZY = 1 ]
41        then FUZZY=0
42    fi
43    TRANSLATED=$(msgattrib --force-po --translated --no-fuzzy --no-obsolete $PO_MESSAGES | count_msgids)
44    TRANSLATED_WC=$(msgattrib --force-po --translated --no-fuzzy --no-obsolete --no-wrap $PO_MESSAGES | count_original_words)
45    rm -f $PO_FILES $PO_MESSAGES
46}
47
48#
49# show_text() : show the statistics in a readable text format
50#
51show_text() {
52    for lang in $LANGUAGES ; do
53        statistics
54        echo "   $lang: $(($TRANSLATED*100/$TOTAL))% ($TRANSLATED/$TOTAL) translated, $(($FUZZY*100/$TOTAL))% ($FUZZY) fuzzy, $(($TRANSLATED_WC*100/$TOTAL_WC))% ($TRANSLATED_WC/$TOTAL_WC) words translated (rev. date: $REV_DATE)"
55    done
56}
57
58#
59# show_html() : show the statistics in HTML format
60#
61show_html() {
62    echo "<html>
63  <body>
64    <table>
65      <caption>Translation status of the Inkscape user interface</caption>
66      <thead>
67        <tr>
68          <td>Language</td><td>Status</td><td>Untranslated</td><td>Fuzzy</td><td>Total</td><td>Last changed</td>
69        </tr>
70      </thead>
71      <tbody>"
72    for lang in $LANGUAGES ; do
73        statistics
74        echo "        <tr><td>$lang</td><td><progress max='100' value='$(($TRANSLATED*100/$TOTAL))' title='$(($TRANSLATED*100/$TOTAL))%'>$(($TRANSLATED*100/$TOTAL))%</progress></td><td>$(($TOTAL-$TRANSLATED-$FUZZY)) ($((($TOTAL-$TRANSLATED-$FUZZY)*100/$TOTAL))%)</td><td>$FUZZY ($(($FUZZY*100/$TOTAL))%)</td><td>$TOTAL</td><td>$REV_DATE</td></tr>"
75    done
76    echo "      </tbody>
77    </table>
78  </body>
79</html>"
80}
81
82user_lang=
83export_html=0
84
85# Command line options
86while test $# -gt 0
87do
88    case $1 in
89    -h | --help)
90        usage
91        exit 0
92        ;;
93    -e | --export-html)
94        export_html=1
95        ;;
96    -*)  echo "$0 : invalid option $1" >&2
97        usage
98        exit 1
99        ;;
100    *)
101        user_lang=$@
102        break
103        ;;
104    esac
105    shift
106done
107
108
109set -eu
110#set -o pipefail
111
112if [ "$user_lang" ]
113    then LANGUAGES="$user_lang"
114    else LANGUAGES="$(grep -v "^\#" ./LINGUAS)"
115fi
116
117if [ $export_html = 1 ]
118    then show_html
119    else show_text
120fi
121
122
123# -*- mode: sh; sh-basic-offset: 4; indent-tabs-mode: nil; -*-
124# vim: set filetype=sh sw=4 sts=4 expandtab autoindent:
125