summaryrefslogtreecommitdiff
path: root/tools/localization/updateLocalizationModule.sh
blob: 37af7bc8aa869ba5474f72a92d84bb71c840d43d (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/bin/bash
# Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
# Copyright (C) INRIA - 2007-2008 - Sylvestre Ledru
# Copyright (C) DIGITEO - 2009-2011 - Sylvestre Ledru
# Copyright (C) DIGITEO - 2011-2011 - Bruno JOFRET
# This file must be used under the terms of the CeCILL.
# This source file is licensed as described in the file COPYING, which
# you should have received as part of this distribution.  The terms
# are also available at
# http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
#
# This script goes into a module and updates the localization file by checking
# the _( and gettext( calls in the code

# TODO :
# * Write Small documentation
#

if test $# -ne 1; then
    echo "This script goes into a module and updates the localization file "
    echo "by checking the _(xxx) and gettext(xxx) calls in the code"
    echo "It creates the locales directory for C, C++ and Java and"
    echo "locales_macros for Scilab code"
    echo
    echo "Syntax : $0 <module>"
    echo "If <module> is equal to 'process_all', it will parse all Scilab module"
    echo "per module"
    exit 42
fi

if test -z "$SCI"; then
    echo "Please define the variable SCI"
    exit 42
fi

MODULES=$1

if test "$MODULES" = "process_all"; then
    echo ".. Process all the modules one by one"
    PROCESS_ALL=1
else
    PROCESS_ALL=0
fi


XGETTEXT=/usr/bin/xgettext
FROM_CODE=ISO-8859-1
EXTENSIONS=( 'c' 'h' 'cpp' 'hxx' 'java' )
EXTENSIONS_MACROS=( sci sce start quit )
TARGETDIR=locales/
TARGETDIR_MACROS=locales_macros/
HEADER_TEMPLATE=$SCI/modules/localization/data/header.pot
GUI_FILES="etc/*.x*l"
PREFERENCE_FILES="src/xslt/*.xsl"
FAKE_C_FILE=scilab_fake_localization_file.c
TIMEZONE="+0100"
# Gettext arg
XGETTEXT_OPTIONS="--add-location --strict --keyword=_ --from-code $FROM_CODE --omit-header --no-wrap --sort-by-file"

process_XML_files() {
# First expression => remove line which does NOT contain label
# Second expression =>  extract the content of the label and switch it to a gettext fake instruction
# Third expression => remove empty lines
# Please note that it will only extract string from the label tag
    if test -n "$(ls $GUI_FILES 2>/dev/null)"; then
        COMMON_SED='s/&amp;/\&/g'
	sed  -e '/label/!s/.*//'  -e 's/.*label="\([^"]*\)".*/gettext("\1")/' -e '/^$/d' -e $COMMON_SED $GUI_FILES > $FAKE_C_FILE
	sed  -e '/tooltiptext/!s/.*//'  -e 's/.*tooltiptext="\([^"]*\)".*/gettext("\1")/' -e '/^$/d' -e $COMMON_SED $GUI_FILES >> $FAKE_C_FILE
	sed -e 's/.*_(\([^"]*\)).*/gettext("\1")/' $GUI_FILES >> $FAKE_C_FILE
    fi
    if test -n "$(ls $PREFERENCE_FILES 2>/dev/null)"; then
	sed -e 's/.*_(\([^"]*\)).*/gettext("\1")/' $PREFERENCE_FILES  >> $FAKE_C_FILE
    fi
}


function generate_find_command {
    # Setting the shell's Internal Field Separator to null
    OLD_IFS=$IFS
    IFS=''
    # Create a string containing "colors[*]"
    local array_string="$1[*]"
    # assign loc_array value to ${colors[*]} using indirect variable reference
    local EXT=(${!array_string})
#
# Retrieve all the sources files
    FILESCMD='find . -type f '
####### GENERATES THE FIND COMMAND
    i=0
    NB_ELEMENT=${#EXT[@]}

    while [ "$i" -lt "$NB_ELEMENT" ]; do
        ext=${EXT[$i]}
        FILESCMD="$FILESCMD -name '*.$ext'"
        if test "$NB_ELEMENT" -ne `expr $i + 1`; then # because we don't want a trailing -o
            FILESCMD="$FILESCMD -o "
        fi
        i=$((i + 1))
    done
    # Resetting IFS to default
    IFS=$OLD_IFS
}


# Process all the modules ... then, build the list
if test $PROCESS_ALL -eq 1; then
    cd $SCI/modules/
    MODULES=`find . -maxdepth 1 -type d  ! -name ".*" ! -name 'javasci'`
fi


function process_module {

    IS_MACROS=0
    if test "$1" == "macros"; then
        IS_MACROS=1
    fi

# Extract label from xml files
    process_XML_files
    if test $IS_MACROS -eq 1; then
        generate_find_command EXTENSIONS_MACROS
        local TARGETDIR=$TARGETDIR_MACROS
    else
        generate_find_command EXTENSIONS
    fi
    if test ! -d $TARGETDIR; then mkdir $TARGETDIR; fi

    FILES=`eval $FILESCMD|tr "\n" " "`

    if test "$MODULE" = "core" -o "$MODULE" = "./core"; then
        # We want some strings from the ROOTDIR when it is the core module
        FILES="$FILES `ls $SCI/etc/scilab.*`"
    fi

    # Also extract string straight from the XML because we have some gettext calls in it
    if test -n "$(ls $GUI_FILES 2>/dev/null)" -a $IS_MACROS -ne 1; then
        FILES="$FILES `ls $GUI_FILES`"
    fi

    FILES=$(echo $FILES|sort)

    MODULE_NAME=`echo $MODULE|sed -e 's|./||'` # avoid to have ./module_name

    if test $IS_MACROS -eq 1; then
        echo "..... Parsing all Scilab macros in $PATHTOPROCESS"
    else
        echo "..... Parsing all sources in $PATHTOPROCESS"
    fi
# Parse all the sources and get the string which should be localized


    if test $IS_MACROS -eq 1; then
        MODULE_NAME=$MODULE_NAME-macros
    fi
    LOCALIZATION_FILE_US=$TARGETDIR/$MODULE_NAME.pot

    if test -f $LOCALIZATION_FILE_US; then
        # Localization file already existing. Retrieve POT-Creation-Date
        CreationDate=`grep POT-Creation-Date: $LOCALIZATION_FILE_US|sed -e 's|\"POT-Creation-Date: \(.*\)\\\n\"|\1|'`
    fi

    echo "........ Generate the English localization file by parsing the code"
    if test $IS_MACROS -eq 1; then
        # It is Scilab code... xgettext does not how to process it
        XGETTEXT_OPTIONS="$XGETTEXT_OPTIONS --language=C"
    fi

    $XGETTEXT $XGETTEXT_OPTIONS -p $TARGETDIR/ -o $MODULE_NAME.pot.tmp $FILES > /dev/null
    if test ! -f $MODULE_NAME.pot.tmp -a $IS_MACROS -eq 1; then
        # Empty file => no string found
        # We are modifing on the fly Scilab localization files
        #
        # We need C strings format to be used as gettext key
        # "" -> \"
        # '' -> '
        # '" -> \"
        # "' -> ' -e "s/\"'/'/g" 
        sed -i -e "s/\"\"/\\\"/g" -e "s/''/'/g" -e "s/'\"/\\\"/g" $TARGETDIR/$MODULE_NAME.pot.tmp
        # We introduced invalid tag [msgstr "] and [msgid "]
        # restore them [msgstr ""] and [msgid ""]
        sed -i -e "s/msgstr \"$/msgstr \"\"/" -e "s/msgid \"$/msgid \"\"/" $TARGETDIR/$MODULE_NAME.pot.tmp
    fi

    if test  -z "$CreationDate"; then
        # File not existing before ... Set the current date a POT-Creation-Date
        sed -e "s/MODULE/$MODULE_NAME/" -e "s/CREATION-DATE/`date +'%Y-%m-%d %H:%M'`$TIMEZONE/" -e "s/REVISION-DATE/`date +'%Y-%m-%d %H:%M'`$TIMEZONE/" $HEADER_TEMPLATE > $LOCALIZATION_FILE_US
    else
        sed -e "s/MODULE/$MODULE_NAME/" -e "s/CREATION-DATE/$CreationDate/" -e "s/REVISION-DATE/`date +'%Y-%m-%d %H:%M'`$TIMEZONE/" $HEADER_TEMPLATE > $LOCALIZATION_FILE_US
    fi

    msguniq -u $LOCALIZATION_FILE_US.tmp >> $LOCALIZATION_FILE_US 2> /dev/null

    rm $LOCALIZATION_FILE_US.tmp 2> /dev/null

    MSGOUTPUT=$(msgcat $LOCALIZATION_FILE_US)
    if test $? -ne 0; then
        echo "Badly formated localization files"
        exit 32
    fi
    if test -z "$(msgcat $LOCALIZATION_FILE_US)"; then
        # empty template. Kill it!
        rm $LOCALIZATION_FILE_US
    fi

    if test $IS_MACROS -eq 1; then
        LOCALIZATION_FILE_NATIVE=$(echo $LOCALIZATION_FILE_US|sed -e "s|-macros||g" -e "s|_macros||g")
        if test ! -f $LOCALIZATION_FILE_NATIVE; then
            # no native code. Copy the macro one
            cp $LOCALIZATION_FILE_US $LOCALIZATION_FILE_NATIVE
        else
        # merge locale macros => native code
            msgcat --use-first  -o $LOCALIZATION_FILE_NATIVE.tmp  $LOCALIZATION_FILE_NATIVE $LOCALIZATION_FILE_US
            mv $LOCALIZATION_FILE_NATIVE.tmp $LOCALIZATION_FILE_NATIVE
        fi
        rm -rf $TARGETDIR_MACROS
    fi

    # Remove fake file used to extract string from XML
    rm $FAKE_C_FILE 2> /dev/null


}

for MODULE in $MODULES; do

    PATHTOPROCESS=$SCI/modules/$MODULE/
    if test ! -d $PATHTOPROCESS; then
        echo "... Cannot find module $PATHTOPROCESS"
        exit
    fi
    echo "... Processing module $MODULE"

    cd $PATHTOPROCESS
    process_module "src"
    process_module "macros"

    cd $SCI/
done # Browse modules