aboutsummaryrefslogtreecommitdiffstats
path: root/tools/rebuild_gettext_catalogs.sh
blob: 44ba59d643d5e2dd76af1b97e2144a81c948ae68 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
# Filip Komar, 2014 - 2017
# @license GPL v2
# @author Filip (rebuilding a gettext dictionaries)
# @copyright 2014 - 2017
# 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! Please report that on atelier mailing list."
    exit 2
fi

rpm --quiet -q gettext python3-polib
if [ $? -eq 1 ]; then
    echo "Warning!!! Some gettext components are missing. Please install them as root: urpmi gettext python3-polib"
    exit 1
fi

for resource in "${!resources[@]}"
do
    difference=
    if [ "$resource" = "mognase" ]; then # is it about navigation?
        path_and_filename=./_nav/langs/en
    else
        path_and_filename=./langs/en/$resource
    fi
    if [ -f "$path_and_filename.pot" ]; then # do a temporary copy
        cp $path_and_filename.pot $path_and_filename.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 there are no real differences restore copy
    if [ -f "$path_and_filename.pot" ] &&  [ -f "$path_and_filename.copy" ]; then
        diff --ignore-matching-lines='^"POT-Creation-Date:' --ignore-matching-lines='^# Copyright (C) 2014 - ' --ignore-matching-lines='^#: "/web/' $path_and_filename.pot $path_and_filename.copy > /dev/null
        if [ $? -eq 0 ]; then
            cp $path_and_filename.copy $path_and_filename.pot
        fi
    fi
    if [ -f "$path_and_filename.pot" ]; then
        # msgmerge check of generated pot file
        msgmerge --quiet --no-wrap $path_and_filename.pot $path_and_filename.pot --output-file=$path_and_filename.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 $path_and_filename.tmp
        fi
        if [ -f "$path_and_filename.copy" ] && [ -f "$path_and_filename.tmp" ]; then
            diff --ignore-matching-lines='^"POT-Creation-Date:' --ignore-matching-lines='^# Copyright (C) 2014 - ' --ignore-matching-lines='^#: "/web/' $path_and_filename.tmp $path_and_filename.copy
            if [ $? -eq 1 ]; then # there are differences
                errors[$php_source]=0
                mv $path_and_filename.tmp $path_and_filename.pot
                rm $path_and_filename.copy
                echo Changes in $path_and_filename.pot are listed above
            else # Cleanup as there are no changes in $resource
                errors[$php_source]=5
                rm $path_and_filename.tmp
                mv $path_and_filename.copy $path_and_filename.pot
                difference=none
            fi
        else # diff not possible
            if [ -f "$path_and_filename.tmp" ]; then
                mv $path_and_filename.tmp $path_and_filename.pot
                if [ -f "$path_and_filename.copy" ]; then
                    rm $path_and_filename.copy
                fi
            else
                rm $path_and_filename.pot
                if [ -f "$path_and_filename.copy" ]; then
                    mv $path_and_filename.copy $path_and_filename.pot
                    difference=none
                fi
            fi
        fi
        if [ "$resource" = "mognase" ]; then # is it about navigation?
            # create po files if needed from lang files if available
            if [ -f ./_nav/langs/en.lang ]; then
                python tools/translated_converter.py --filename ./_nav/langs/
                python_error_level=$? # catch python error level
                if [ $python_error_level -gt 0 ]; then
                    errors[$resource]=50
                fi
                if [ -f ./_nav/langs/en.po ]; then
                    rm ./_nav/langs/en.po
                fi
            fi

            for tr_file in ./_nav/langs/*.po
            do
                if [ -z $difference ]; then # update po file if needed
#                    msgfmt --statistics --verbose -c $tr_file -o /dev/null # usefull for debuging (msgctxt "/web/en/ or msgctxt "en/)
                    echo merging $tr_file
                    msgmerge --quiet --no-wrap $tr_file ./_nav/langs/en.pot --output-file=$tr_file.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
                        if [ -f "$tr_file.tmp" ]; then
                            rm $tr_file.tmp
                        fi
                    else
                        mv $tr_file.tmp $tr_file
#                        msgfmt --statistics --verbose -c $tr_file -o /dev/null # usefull for debuging (msgctxt "/web/en/ or msgctxt "en/)
                    fi
                fi
            done
        else
            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/)
                        # merge the translations once if it's about mga version_soon_to_be_released but adjust the numbers and uncomment the code ;)
#                         if [ "$resource" = "5" ]; then
#                             msgcat --use-first --no-wrap $directory/5.po $directory/cauldron.po $directory/4.po > $directory/5_.po
#                             sed -i 's/Mageia 4/Mageia 5/g' $directory/5_.po
#                             mv $directory/5_.po $directory/5.po
#                         fi
                        echo merging $directory/$resource.po
                        msgmerge --quiet --no-wrap $directory/$resource.po ./langs/en/$resource.pot --output-file=$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
                            if [ -f "$tr_file.tmp" ]; then
                                rm $directory/$resource.tmp
                            fi
                        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
    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