2 # Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 # Copyright (C) INRIA - 2007-2008 - Sylvestre Ledru
4 # Copyright (C) DIGITEO - 2009-2011 - Sylvestre Ledru
5 # Copyright (C) DIGITEO - 2011-2011 - Bruno JOFRET
6 # This file must be used under the terms of the CeCILL.
7 # This source file is licensed as described in the file COPYING, which
8 # you should have received as part of this distribution. The terms
9 # are also available at
10 # http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
12 # This script goes into a module and updates the localization file by checking
13 # the _( and gettext( calls in the code
16 # * Write Small documentation
19 if test $# -ne 1; then
20 echo "This script goes into a module and updates the localization file "
21 echo "by checking the _(xxx) and gettext(xxx) calls in the code"
22 echo "It creates the locales directory for C, C++ and Java and"
23 echo "locales_macros for Scilab code"
25 echo "Syntax : $0 <module>"
26 echo "If <module> is equal to 'process_all', it will parse all Scilab module"
31 if test -z "$SCI"; then
32 echo "Please define the variable SCI"
38 if test "$MODULES" = "process_all"; then
39 echo ".. Process all the modules one by one"
46 XGETTEXT=/usr/bin/xgettext
48 EXTENSIONS=( 'c' 'h' 'cpp' 'hxx' 'java' )
49 EXTENSIONS_MACROS=( sci sce start quit )
51 TARGETDIR_MACROS=locales_macros/
52 HEADER_TEMPLATE=$SCI/modules/localization/data/header.pot
54 PREFERENCE_FILES="src/xslt/*.xsl"
55 FAKE_C_FILE=scilab_fake_localization_file.c
58 XGETTEXT_OPTIONS="--add-location --strict --keyword=_ --from-code $FROM_CODE --omit-header --no-wrap --sort-by-file"
61 # First expression => remove line which does NOT contain label
62 # Second expression => extract the content of the label and switch it to a gettext fake instruction
63 # Third expression => remove empty lines
64 # Please note that it will only extract string from the label tag
65 if test -n "$(ls $GUI_FILES 2>/dev/null)"; then
66 COMMON_SED='s/&/\&/g'
67 sed -e '/label/!s/.*//' -e 's/.*label="\([^"]*\)".*/gettext("\1")/' -e '/^$/d' -e $COMMON_SED $GUI_FILES > $FAKE_C_FILE
68 sed -e '/tooltiptext/!s/.*//' -e 's/.*tooltiptext="\([^"]*\)".*/gettext("\1")/' -e '/^$/d' -e $COMMON_SED $GUI_FILES >> $FAKE_C_FILE
69 sed -e 's/.*_(\([^"]*\)).*/gettext("\1")/' $GUI_FILES >> $FAKE_C_FILE
71 if test -n "$(ls $PREFERENCE_FILES 2>/dev/null)"; then
72 sed -e 's/.*_(\([^"]*\)).*/gettext("\1")/' $PREFERENCE_FILES >> $FAKE_C_FILE
77 function generate_find_command {
78 # Setting the shell's Internal Field Separator to null
81 # Create a string containing "colors[*]"
82 local array_string="$1[*]"
83 # assign loc_array value to ${colors[*]} using indirect variable reference
84 local EXT=(${!array_string})
86 # Retrieve all the sources files
87 FILESCMD='find . -type f '
88 ####### GENERATES THE FIND COMMAND
92 while [ "$i" -lt "$NB_ELEMENT" ]; do
94 FILESCMD="$FILESCMD -name '*.$ext'"
95 if test "$NB_ELEMENT" -ne `expr $i + 1`; then # because we don't want a trailing -o
96 FILESCMD="$FILESCMD -o "
100 # Resetting IFS to default
105 # Process all the modules ... then, build the list
106 if test $PROCESS_ALL -eq 1; then
108 MODULES=`find . -maxdepth 1 -type d ! -name ".*" ! -name 'javasci'`
112 function process_module {
115 if test "$1" == "macros"; then
119 # Extract label from xml files
121 if test $IS_MACROS -eq 1; then
122 generate_find_command EXTENSIONS_MACROS
123 local TARGETDIR=$TARGETDIR_MACROS
125 generate_find_command EXTENSIONS
127 if test ! -d $TARGETDIR; then mkdir $TARGETDIR; fi
129 FILES=`eval $FILESCMD|tr "\n" " "`
131 if test "$MODULE" = "core" -o "$MODULE" = "./core"; then
132 # We want some strings from the ROOTDIR when it is the core module
133 FILES="$FILES `ls $SCI/etc/scilab.*`"
136 # Also extract string straight from the XML because we have some gettext calls in it
137 if test -n "$(ls $GUI_FILES 2>/dev/null)" -a $IS_MACROS -ne 1; then
138 FILES="$FILES `ls $GUI_FILES`"
141 FILES=$(echo $FILES|sort)
143 MODULE_NAME=`echo $MODULE|sed -e 's|./||'` # avoid to have ./module_name
145 if test $IS_MACROS -eq 1; then
146 echo "..... Parsing all Scilab macros in $PATHTOPROCESS"
148 echo "..... Parsing all sources in $PATHTOPROCESS"
150 # Parse all the sources and get the string which should be localized
153 if test $IS_MACROS -eq 1; then
154 MODULE_NAME=$MODULE_NAME-macros
156 LOCALIZATION_FILE_US=$TARGETDIR/$MODULE_NAME.pot
158 if test -f $LOCALIZATION_FILE_US; then
159 # Localization file already existing. Retrieve POT-Creation-Date
160 CreationDate=`grep POT-Creation-Date: $LOCALIZATION_FILE_US|sed -e 's|\"POT-Creation-Date: \(.*\)\\\n\"|\1|'`
163 echo "........ Generate the English localization file by parsing the code"
164 if test $IS_MACROS -eq 1; then
165 # It is Scilab code... xgettext does not how to process it
166 XGETTEXT_OPTIONS="$XGETTEXT_OPTIONS --language=C"
169 $XGETTEXT $XGETTEXT_OPTIONS -p $TARGETDIR/ -o $MODULE_NAME.pot.tmp $FILES > /dev/null
170 if test ! -f $MODULE_NAME.pot.tmp -a $IS_MACROS -eq 1; then
171 # Empty file => no string found
172 # We are modifing on the fly Scilab localization files
174 # We need C strings format to be used as gettext key
178 # "' -> ' -e "s/\"'/'/g"
179 sed -i -e "s/\"\"/\\\"/g" -e "s/''/'/g" -e "s/'\"/\\\"/g" $TARGETDIR/$MODULE_NAME.pot.tmp
180 # We introduced invalid tag [msgstr "] and [msgid "]
181 # restore them [msgstr ""] and [msgid ""]
182 sed -i -e "s/msgstr \"$/msgstr \"\"/" -e "s/msgid \"$/msgid \"\"/" $TARGETDIR/$MODULE_NAME.pot.tmp
185 if test -z "$CreationDate"; then
186 # File not existing before ... Set the current date a POT-Creation-Date
187 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
189 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
192 msguniq -u $LOCALIZATION_FILE_US.tmp >> $LOCALIZATION_FILE_US 2> /dev/null
194 rm $LOCALIZATION_FILE_US.tmp 2> /dev/null
196 MSGOUTPUT=$(msgcat $LOCALIZATION_FILE_US)
197 if test $? -ne 0; then
198 echo "Badly formated localization files"
201 if test -z "$(msgcat $LOCALIZATION_FILE_US)"; then
202 # empty template. Kill it!
203 rm $LOCALIZATION_FILE_US
206 if test $IS_MACROS -eq 1; then
207 LOCALIZATION_FILE_NATIVE=$(echo $LOCALIZATION_FILE_US|sed -e "s|-macros||g" -e "s|_macros||g")
208 if test ! -f $LOCALIZATION_FILE_NATIVE; then
209 # no native code. Copy the macro one
210 cp $LOCALIZATION_FILE_US $LOCALIZATION_FILE_NATIVE
212 # merge locale macros => native code
213 msgcat --use-first -o $LOCALIZATION_FILE_NATIVE.tmp $LOCALIZATION_FILE_NATIVE $LOCALIZATION_FILE_US
214 mv $LOCALIZATION_FILE_NATIVE.tmp $LOCALIZATION_FILE_NATIVE
216 rm -rf $TARGETDIR_MACROS
219 # Remove fake file used to extract string from XML
220 rm $FAKE_C_FILE 2> /dev/null
225 for MODULE in $MODULES; do
227 PATHTOPROCESS=$SCI/modules/$MODULE/
228 if test ! -d $PATHTOPROCESS; then
229 echo "... Cannot find module $PATHTOPROCESS"
232 echo "... Processing module $MODULE"
236 process_module "macros"
239 done # Browse modules