aboutsummaryrefslogtreecommitdiffstats
path: root/tools/rebuild_gettext_catalogs.sh
blob: d32dfdde3c276076c4b84db0bb99966209ffd275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
# Filip Komar, 2014
# @license GPL v2
# @author Filip (rebuilding a gettext dictionaries)
# @copyright 2014/03
# inspired by check_for_translation_work.sh

error=0
declare -A resources
declare -A errors
if [ -f ./tools/web_projects.dat ]; then
    source ./tools/web_projects.dat
else
    echo Critical error!!! It was not possible to load web projects database!
fi

for resource in "${!resources[@]}"
do
    difference=
    if [ -f ./langs/en/$resource.pot ]; then # do a temporary copy
        cp ./langs/en/$resource.pot ./langs/en/$resource.copy
    fi
    php_source=${resources[$resource]}
    php tools/extract2gettext.php "$php_source" $resource true
    php_error_level=$? # catch php error level
    errors[$php_source]=$php_error_level
    if [ -f ./langs/en/$resource.pot ]; then # msgmerge check of generated pot file
        msgmerge --quiet --no-wrap ./langs/en/$resource.pot ./langs/en/$resource.pot > ./langs/en/$resource.tmp
        msgmerge_error_level=$? # catch msgmerge error level
        if [ $msgmerge_error_level -gt 0 ]; then # on failure stop the process for this resource
            errors[$php_source]=$msgmerge_error_level
            rm ./langs/en/$resource.tmp
        fi
        if [ -f ./langs/en/$resource.copy ] && [ -f ./langs/en/$resource.tmp ]; then
            diff --ignore-matching-lines='^"POT-Creation-Date:' ./langs/en/$resource.tmp ./langs/en/$resource.copy
            if [ $? -eq 1 ]; then # there are differences
                errors[$php_source]=0
                mv ./langs/en/$resource.tmp ./langs/en/$resource.pot
                rm ./langs/en/$resource.copy
                echo Changes in ./langs/en/$resource.pot
            else # Cleanup as there are no changes in $resource
                errors[$php_source]=5
                rm ./langs/en/$resource.tmp
                mv ./langs/en/$resource.copy ./langs/en/$resource.pot
                difference=none
            fi
        else # diff not possible
            if [ -f ./langs/en/$resource.tmp ]; then
                mv ./langs/en/$resource.tmp ./langs/en/$resource.pot
                rm ./langs/en/$resource.copy
            else
                rm ./langs/en/$resource.pot
                if [ -f ./langs/en/$resource.copy ]; then
                    mv ./langs/en/$resource.copy ./langs/en/$resource.pot
                    difference=none
                fi
            fi
        fi
        for directory in ./langs/*
        do
            # in each language directory except source
            if [ -d $directory/ ] && [ $directory != ./langs/en ]; then
                # create po file if needed from lang file if available
                if [ ! -f $directory/$resource.po ] && [ -f $directory/$resource.*.lang ]; then
#                    echo $directory # usefull for debuging of non UTF-8 files or other python errors
                    python tools/translated_converter.py --filename $directory/$resource
                    python_error_level=$? # catch python error level
                    if [ $python_error_level -gt 0 ]; then
                        errors[$directory/$resource]=50
                    fi
                fi
                if [ -f $directory/$resource.po ] && [ -z $difference ]; then # update po file if it exists
#                    msgfmt --statistics --verbose -c $directory/$resource.po -o /dev/null # usefull for debuging (msgctxt "/web/en/ or msgctxt "en/)
                    echo merging $directory/$resource.po
                    msgmerge --quiet --no-wrap $directory/$resource.po ./langs/en/$resource.pot > $directory/$resource.tmp
                    msgmerge_error_level=$? # catch msgmerge error level
                    if [ $msgmerge_error_level -gt 0 ]; then
                        errors[$directory/$resource]=60
                        echo "$directory"/$resource.po could not be created succesfully
                    else
                        mv $directory/$resource.tmp $directory/$resource.po
#                        msgfmt --statistics --verbose -c $directory/$resource.po -o /dev/null # usefull for debuging (msgctxt "/web/en/ or msgctxt "en/)
                    fi
                fi
            fi
        done
    fi
done

echo ''
#printf "%s\n" "${errors[@]}"
for error_in_source in "${!errors[@]}"
do
    case ${errors[$error_in_source]} in
         0)  echo "Done lookup for _g() and _r() in $error_in_source. Gettext catalogs (pot files) rebuilded. Existing po files updated." ;;
         1)  echo "Unkown error!!" ;;
         5)  echo "There are no changes in $error_in_source." ;;
        10)  echo "No strings to save from $error_in_source!" ;;
        20)  echo "!!!! Parse error, please fix $error_in_source first !!!! See details above." ;;
        30)  echo "!!!! Failed to write $error_in_source !!!! See details above." ;;
        50)  echo "!!!! Script translated_converter.py encounter an error in $error_in_source !!!!" ;;
        60)  echo "$error_in_source.po could not be created succesfully!! Duplicates?" ;;
    esac
done