MOBILE-4362 scripts: Lint language sh

main
Pau Ferrer Ocaña 2023-06-27 14:51:23 +02:00
parent 8e413bad8e
commit 71b9a55b2c
7 changed files with 138 additions and 134 deletions

View File

@ -1,13 +1,13 @@
#!/bin/bash #!/bin/bash
source "scripts/functions.sh" source "scripts/functions.sh"
if [ -z $GIT_TOKEN ]; then if [ -z "$GIT_TOKEN" ]; then
print_error "Env vars not correctly defined" print_error "Env vars not correctly defined"
exit 1 exit 1
fi fi
print_title "Run scripts" print_title "Run scripts"
git clone --depth 1 --single-branch --branch ionic5 https://$GIT_TOKEN@github.com/moodlemobile/apps-scripts.git ../scripts git clone --depth 1 --single-branch --branch ionic5 https://"$GIT_TOKEN"@github.com/moodlemobile/apps-scripts.git ../scripts
cp ../scripts/*.sh scripts/ cp ../scripts/*.sh scripts/
if [ ! -f scripts/platform.sh ]; then if [ ! -f scripts/platform.sh ]; then

View File

@ -9,7 +9,7 @@
DIR="${BASH_SOURCE%/*}" DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
cd $DIR cd "$DIR"
source "functions.sh" source "functions.sh"
source "lang_functions.sh" source "lang_functions.sh"
@ -27,7 +27,7 @@ if [ ! -f 'langindex.json' ]; then
fi fi
findbetter=$1 findbetter=$1
parse_file $findbetter parse_file "$findbetter"
echo echo

View File

@ -8,15 +8,15 @@ SERVER_URL='https://download.moodle.org/'
# Downloads a file and if it's a zip file, unzip it. # Downloads a file and if it's a zip file, unzip it.
function download_file { function download_file {
local url=$1 local url=$1
local filename=$(basename ${url}) local filename=$(basename "${url}")
pushd $LANGPACKS_PATH > /dev/null pushd "$LANGPACKS_PATH" > /dev/null
curl -s $url --output $filename > /dev/null curl -s "$url" --output "$filename" > /dev/null
size=$(du -k "$filename" | cut -f 1) size=$(du -k "$filename" | cut -f 1)
if [ ! -n $filename ] || [ $size -le 1 ]; then if [ ! -n "$filename" ] || [ "$size" -le 1 ]; then
echo "Wrong or corrupt file $filename" echo "Wrong or corrupt file $filename"
rm $filename rm "$filename"
popd > /dev/null popd > /dev/null
return return
@ -25,21 +25,21 @@ function download_file {
if [[ $filename == *.zip ]]; then if [[ $filename == *.zip ]]; then
local lang="${filename%.*}" local lang="${filename%.*}"
# Delete previous downloaded folder # Delete previous downloaded folder
rm -R $lang > /dev/null 2>&1> /dev/null rm -R "$lang" > /dev/null 2>&1> /dev/null
# Unzip # Unzip
unzip -o -u $lang.zip > /dev/null unzip -o -u "$lang".zip > /dev/null
# Delete the zip # Delete the zip
rm $filename rm "$filename"
fi fi
popd > /dev/null popd > /dev/null
} }
function get_english { function get_english {
if [ ! -d $LANGPACKS_PATH ]; then if [ ! -d "$LANGPACKS_PATH" ]; then
mkdir $LANGPACKS_PATH mkdir "$LANGPACKS_PATH"
fi fi
get_app_version get_app_version
@ -63,7 +63,7 @@ function save_key {
function remove_key { function remove_key {
local key=$1 local key=$1
cat langindex.json | jq 'del(."'$key'")' > langindex_new.json cat langindex.json | jq 'del(."'"$key"'")' > langindex_new.json
mv langindex_new.json langindex.json mv langindex_new.json langindex.json
print_ok "Deleted unused key $key" print_ok "Deleted unused key $key"
} }
@ -73,14 +73,14 @@ function exists_in_file {
local file=$1 local file=$1
local id=$2 local id=$2
file=`echo $file | sed s/^mod_workshop_assessment/workshopform/1` file=$(echo "$file" | sed s/^mod_workshop_assessment/workshopform/1)
file=`echo $file | sed s/^mod_assign_/assign/1` file=$(echo "$file" | sed s/^mod_assign_/assign/1)
file=`echo $file | sed s/^mod_//1` file=$(echo "$file" | sed s/^mod_//1)
completeFile="$LANGPACKS_PATH/en/$file.php" completeFile="$LANGPACKS_PATH/en/$file.php"
if [ -f "$completeFile" ]; then if [ -f "$completeFile" ]; then
foundInFile=`grep "string\[\'$id\'\]" $completeFile` foundInFile=$(grep "string\['${id}'\]" "${completeFile}")
if [ ! -z "$foundInFile" ]; then if [ -n "$foundInFile" ]; then
coincidence=1 coincidence=1
found=$file found=$file
return return
@ -93,7 +93,7 @@ function exists_in_file {
#Checks if a key exists on the original local_moodlemobileapp.php #Checks if a key exists on the original local_moodlemobileapp.php
function exists_in_mobile { function exists_in_mobile {
local file='local_moodlemobileapp' local file='local_moodlemobileapp'
exists_in_file $file $key exists_in_file $file "$key"
} }
function do_match { function do_match {
@ -101,14 +101,14 @@ function do_match {
match=${match/\}\}/\}} match=${match/\}\}/\}}
filematch="" filematch=""
coincidence=`grep "$match" $LANGPACKS_PATH/en/*.php | wc -l` coincidence=$(grep "$match" "$LANGPACKS_PATH"/en/*.php | wc -l)
if [ $coincidence -eq 1 ]; then if [ "$coincidence" -eq 1 ]; then
filematch=`grep "$match" $LANGPACKS_PATH/en/*.php | cut -d'/' -f5 | cut -d'.' -f1` filematch=$(grep "$match" "$LANGPACKS_PATH"/en/*.php | cut -d'/' -f5 | cut -d'.' -f1)
exists_in_file $filematch $plainid exists_in_file "$filematch" "$plainid"
elif [ $coincidence -gt 0 ] && [ "$#" -gt 1 ]; then elif [ "$coincidence" -gt 0 ] && [ "$#" -gt 1 ]; then
print_message "$2" print_message "$2"
tput setaf 6 tput setaf 6
grep "$match" $LANGPACKS_PATH/en/*.php grep "$match" "$LANGPACKS_PATH"/en/*.php
else else
coincidence=0 coincidence=0
fi fi
@ -117,48 +117,48 @@ function do_match {
#Find if the id or the value can be found on files to help providing a solution. #Find if the id or the value can be found on files to help providing a solution.
function find_matches { function find_matches {
do_match "string\[\'$plainid\'\] = \'$value\'" "Found EXACT match for $key in the following paths" do_match "string\[\'$plainid\'\] = \'$value\'" "Found EXACT match for $key in the following paths"
if [ $coincidence -gt 0 ]; then if [ "$coincidence" -gt 0 ]; then
case=1 case=1
save_key $key "TBD" save_key "$key" "TBD"
return return
fi fi
do_match " = \'$value\'" "Found some string VALUES for $key in the following paths" do_match " = \'$value\'" "Found some string VALUES for $key in the following paths"
if [ $coincidence -gt 0 ]; then if [ "$coincidence" -gt 0 ]; then
case=2 case=2
save_key $key "TBD" save_key "$key" "TBD"
return return
fi fi
do_match "string\[\'$plainid\'\]" "Found some string KEYS for $key in the following paths, value $value" do_match "string\[\'$plainid\'\]" "Found some string KEYS for $key in the following paths, value $value"
if [ $coincidence -gt 0 ]; then if [ "$coincidence" -gt 0 ]; then
case=3 case=3
save_key $key "TBD" save_key "$key" "TBD"
return return
fi fi
print_message "No match found for $key add it to local_moodlemobileapp" print_message "No match found for $key add it to local_moodlemobileapp"
save_key $key "local_moodlemobileapp" save_key "$key" "local_moodlemobileapp"
} }
function find_single_matches { function find_single_matches {
do_match "string\[\'$plainid\'\] = \'$value\'" do_match "string\[\'$plainid\'\] = \'$value\'"
if [ ! -z $filematch ] && [ $found != 0 ]; then if [ -n "$filematch" ] && [ "$found" != 0 ]; then
case=1 case=1
return return
fi fi
do_match " = \'$value\'" do_match " = \'$value\'"
if [ ! -z $filematch ] && [ $filematch != 'local_moodlemobileapp' ]; then if [ -n "$filematch" ] && [ "$filematch" != 'local_moodlemobileapp' ]; then
case=2 case=2
print_message "Found some string VALUES for $key in the following paths $filematch" print_message "Found some string VALUES for $key in the following paths $filematch"
tput setaf 6 tput setaf 6
grep "$match" $LANGPACKS_PATH/en/*.php grep "$match" "$LANGPACKS_PATH"/en/*.php
return return
fi fi
do_match "string\[\'$plainid\'\]" do_match "string\[\'$plainid\'\]"
if [ ! -z $filematch ] && [ $found != 0 ]; then if [ -n "$filematch" ] && [ "$found" != 0 ]; then
case=3 case=3
return return
fi fi
@ -170,47 +170,47 @@ function guess_file {
local key=$1 local key=$1
local value=$2 local value=$2
local type=`echo $key | cut -d'.' -f1` local type=$(echo "$key" | cut -d'.' -f1)
local component=`echo $key | cut -d'.' -f2` local component=$(echo "$key" | cut -d'.' -f2)
local plainid=`echo $key | cut -d'.' -f3-` local plainid=$(echo "$key" | cut -d'.' -f3-)
if [ -z "$plainid" ]; then if [ -z "$plainid" ]; then
plainid=$component plainid=$component
component='moodle' component='moodle'
fi fi
exists_in_file $component $plainid exists_in_file "$component" "$plainid"
if [ $found == 0 ]; then if [ "$found" == 0 ]; then
tempid=`echo $plainid | sed s/^mod_//1` tempid=$(echo "$plainid" | sed s/^mod_//1)
if [ $component == 'moodle' ] && [ "$tempid" != "$plainid" ]; then if [ "$component" == 'moodle' ] && [ "$tempid" != "$plainid" ]; then
exists_in_file $plainid pluginname exists_in_file "$plainid" pluginname
if [ $found != 0 ]; then if [ "$found" != 0 ]; then
found=$found/pluginname found=$found/pluginname
fi fi
fi fi
fi fi
# Not found in file, try in local_moodlemobileapp # Not found in file, try in local_moodlemobileapp
if [ $found == 0 ]; then if [ "$found" == 0 ]; then
exists_in_mobile exists_in_mobile
fi fi
# Still not found, if only found in one file, use it. # Still not found, if only found in one file, use it.
if [ $found == 0 ]; then if [ "$found" == 0 ]; then
find_single_matches find_single_matches
fi fi
# Last fallback. # Last fallback.
if [ $found == 0 ]; then if [ "$found" == 0 ]; then
exists_in_file 'moodle' $plainid exists_in_file 'moodle' "$plainid"
fi fi
if [ $found == 0 ]; then if [ "$found" == 0 ]; then
find_matches find_matches
else else
save_key $key $found save_key "$key" "$found"
fi fi
} }
@ -219,27 +219,29 @@ function current_translation_exists {
local current=$2 local current=$2
local file=$3 local file=$3
plainid=`echo $key | cut -d'.' -f3-` plainid=$(echo "$key" | cut -d'.' -f3-)
if [ -z "$plainid" ]; then if [ -z "$plainid" ]; then
plainid=`echo $key | cut -d'.' -f2` plainid=$(echo "$key" | cut -d'.' -f2)
fi fi
local currentFile=`echo $current | cut -d'/' -f1` local currentFile=$(echo "$current" | cut -d'/' -f1)
local currentStr=`echo $current | cut -d'/' -f2-` local currentStr=$(echo "$current" | cut -d'/' -f2-)
if [ $currentFile == $current ]; then if [ "$currentFile" == "$current" ]; then
currentStr=$plainid currentStr=$plainid
fi fi
exists_in_file $currentFile $currentStr exists_in_file "$currentFile" "$currentStr"
if [ $found == 0 ]; then if [ "$found" == 0 ]; then
# Translation not found. # Translation not found.
exec="jq -r .\"$key\" $file" exec="jq -r .\"$key\" $file"
value=`$exec`
value=$($exec)
found=$($exec)
print_error "Translation of '$currentStr' not found in '$currentFile'" print_error "Translation of '$currentStr' not found in '$currentFile'"
guess_file $key "$value" guess_file "$key" "$value"
fi fi
} }
@ -249,24 +251,24 @@ function find_better_file {
local value=$2 local value=$2
local current=$3 local current=$3
local type=`echo $key | cut -d'.' -f1` local type=$(echo "$key" | cut -d'.' -f1)
local component=`echo $key | cut -d'.' -f2` local component=$(echo "$key" | cut -d'.' -f2)
local plainid=`echo $key | cut -d'.' -f3-` local plainid=$(echo "$key" | cut -d'.' -f3-)
if [ -z "$plainid" ]; then if [ -z "$plainid" ]; then
plainid=$component plainid=$component
component='moodle' component='moodle'
fi fi
local currentFile=`echo $current | cut -d'/' -f1` local currentFile=$(echo "$current" | cut -d'/' -f1)
local currentStr=`echo $current | cut -d'/' -f2-` local currentStr=$(echo "$current" | cut -d'/' -f2-)
if [ $currentFile == $current ]; then if [ "$currentFile" == "$current" ]; then
currentStr=$plainid currentStr=$plainid
fi fi
exists_in_file $component $plainid exists_in_file "$component" "$plainid"
if [ $found != 0 ] && [ $currentStr == $plainid ]; then if [ "$found" != 0 ] && [ "$currentStr" == "$plainid" ]; then
if [ $found != $currentFile ]; then if [ "$found" != "$currentFile" ]; then
print_ok "Key '$key' found in component, no need to replace old '$current'" print_ok "Key '$key' found in component, no need to replace old '$current'"
fi fi
@ -274,27 +276,27 @@ function find_better_file {
fi fi
# Still not found, if only found in one file, use it. # Still not found, if only found in one file, use it.
if [ $found == 0 ]; then if [ "$found" == 0 ]; then
find_single_matches find_single_matches
fi fi
if [ $found != 0 ] && [ $found != $currentFile ] && [ $case -lt 3 ]; then if [ "$found" != 0 ] && [ "$found" != "$currentFile" ] && [ "$case" -lt 3 ]; then
print_message "Indexed string '$key' found in '$found' better than '$current'" print_message "Indexed string '$key' found in '$found' better than '$current'"
return return
fi fi
if [ $currentFile == 'local_moodlemobileapp' ]; then if [ "$currentFile" == 'local_moodlemobileapp' ]; then
exists_in_mobile exists_in_mobile
else else
exists_in_file $currentFile $currentStr exists_in_file "$currentFile" "$currentStr"
fi fi
if [ $found == 0 ]; then if [ "$found" == 0 ]; then
print_error "Indexed string '$key' not found on current place '$current'" print_error "Indexed string '$key' not found on current place '$current'"
if [ $currentFile != 'local_moodlemobileapp' ]; then if [ "$currentFile" != 'local_moodlemobileapp' ]; then
print_error "Execute this on AMOS print_error "Execute this on AMOS
CPY [$currentStr,$currentFile],[$key,local_moodlemobileapp]" CPY [$currentStr,$currentFile],[$key,local_moodlemobileapp]"
save_key $key "local_moodlemobileapp" save_key "$key" "local_moodlemobileapp"
fi fi
fi fi
} }
@ -304,23 +306,25 @@ function parse_file {
file="$LANG_PATH/en.json" file="$LANG_PATH/en.json"
findbetter=$1 findbetter=$1
keys=`jq -r 'keys[]' $file` keys=$(jq -r 'keys[]' "$file")
for key in $keys; do for key in $keys; do
echo -n '.'
# Check if already parsed. # Check if already parsed.
exec="jq -r .\"$key\" langindex.json" exec="jq -r .\"$key\" langindex.json"
found=`$exec` found=$($exec)
if [ -z "$found" ] || [ "$found" == 'null' ]; then if [ -z "$found" ] || [ "$found" == 'null' ]; then
exec="jq -r .\"$key\" $1" exec="jq -r .\"$key\" $1"
value=`$exec` value=$($exec)
guess_file $key "$value" guess_file "$key" "$value"
else else
if [ "$found" == 'donottranslate' ]; then if [ "$found" == 'donottranslate' ]; then
# Do nothing since is not translatable. # Do nothing since is not translatable.
continue continue
elif [ ! -z "$findbetter" ]; then elif [ -n "$findbetter" ]; then
exec="jq -r .\"$key\" $1" exec="jq -r .\"$key\" $1"
value=`$exec` value=$($exec)
find_better_file "$key" "$value" "$found" find_better_file "$key" "$value" "$found"
elif [ "$found" != 'local_moodlemobileapp' ]; then elif [ "$found" != 'local_moodlemobileapp' ]; then
current_translation_exists "$key" "$found" "$1" current_translation_exists "$key" "$found" "$1"
@ -329,14 +333,14 @@ function parse_file {
done done
# Do some cleanup # Do some cleanup
langkeys=`jq -r 'keys[]' langindex.json` langkeys=$(jq -r 'keys[]' langindex.json)
findkeys="${keys[@]}" findkeys="${keys[@]}"
for key in $langkeys; do for key in $langkeys; do
# Check if already used. # Check if already used.
array_contains "$key" "$findkeys" array_contains "$key" "$findkeys"
if [ -z "$found" ] || [ "$found" == 'null' ]; then if [ -z "$found" ] || [ "$found" == 'null' ]; then
remove_key $key remove_key "$key"
fi fi
done done
} }

View File

@ -58,13 +58,13 @@ function print_title {
} }
function telegram_notify { function telegram_notify {
if [ ! -z $TELEGRAM_APIKEY ] && [ ! -z $TELEGRAM_CHATID ] ; then if [ ! -z "$TELEGRAM_APIKEY" ] && [ ! -z "$TELEGRAM_CHATID" ] ; then
set_branch_and_repo set_branch_and_repo
MESSAGE="$1%0ABranch: *$REFNAME* on $REPO%0ACommit: $COMMIT%0AJob: $JOB ($RUN_NUMBER) [Complete log here]($JOB_URL)" MESSAGE="$1%0ABranch: *$REFNAME* on $REPO%0ACommit: $COMMIT%0AJob: $JOB ($RUN_NUMBER) [Complete log here]($JOB_URL)"
URL="https://api.telegram.org/bot$TELEGRAM_APIKEY/sendMessage" URL="https://api.telegram.org/bot$TELEGRAM_APIKEY/sendMessage"
curl -s -X POST $URL -d chat_id=$TELEGRAM_CHATID -d text="$MESSAGE" curl -s -X POST "$URL" -d chat_id="$TELEGRAM_CHATID" -d text="$MESSAGE"
fi fi
} }
@ -78,12 +78,12 @@ function notify_on_error_exit {
function set_branch_and_repo { function set_branch_and_repo {
if [ ! -z $REPO ]; then if [ ! -z "$REPO" ]; then
# Already filled. # Already filled.
return; return;
fi fi
if [ -z $TRAVIS_OS_NAME ]; then if [ -z "$TRAVIS_OS_NAME" ]; then
#Run on github #Run on github
if [ "$RUNNER_OS" == 'macOS' ]; then if [ "$RUNNER_OS" == 'macOS' ]; then
export OS_NAME='osx' export OS_NAME='osx'
@ -118,12 +118,12 @@ function set_branch_and_repo {
export CI_TYPE='travis' export CI_TYPE='travis'
fi fi
if [ -z $REFNAME ]; then if [ -z "$REFNAME" ]; then
print_error "Empty branch/tag, cancelling..." print_error "Empty branch/tag, cancelling..."
exit 0 exit 0
fi fi
if [ -z $REPO ]; then if [ -z "$REPO" ]; then
print_error "Empty repo, cancelling..." print_error "Empty repo, cancelling..."
exit 0 exit 0
fi fi

View File

@ -14,8 +14,8 @@ function get_app_version {
return return
fi fi
APP_VERSION=`jq -r '.versionname' ../moodle.config.json| cut -d. -f1-2` APP_VERSION=$(jq -r '.versionname' ../moodle.config.json| cut -d. -f1-2)
if [ ! -z $APP_VERSION ]; then if [ ! -z "$APP_VERSION" ]; then
export LANGVERSION=$APP_VERSION export LANGVERSION=$APP_VERSION
echo "Using app version $LANGVERSION" echo "Using app version $LANGVERSION"
return return

View File

@ -21,37 +21,37 @@ function progressbar {
function copy_lang { function copy_lang {
lang=$1 lang=$1
index_keys=`jq -r 'to_entries[] | "\"\(.key)\","' langindex.json` index_keys=$(jq -r 'to_entries[] | "\"\(.key)\","' langindex.json)
index_keys=`echo $index_keys | sed 's/,*$//'` index_keys=$(echo "$index_keys" | sed 's/,*$//')
hyphenlang=${lang/_/-} hyphenlang=${lang/_/-}
langfilepath=$LANG_PATH/$hyphenlang.json langfilepath=$LANG_PATH/$hyphenlang.json
cp $LANGPACKS_PATH/$lang.json $langfilepath cp "$LANGPACKS_PATH"/"$lang".json "$langfilepath"
# Merge SUFFIX file if exists. # Merge SUFFIX file if exists.
if [ ! -z "$SUFFIX" ] && [ -f "$LANGPACKS_PATH/${lang}_${SUFFIX}.json" ]; then if [ -n "$SUFFIX" ] && [ -f "$LANGPACKS_PATH/${lang}_${SUFFIX}.json" ]; then
suffixfilepath="$LANGPACKS_PATH/${lang}_${SUFFIX}.json" suffixfilepath="$LANGPACKS_PATH/${lang}_${SUFFIX}.json"
jq --indent 4 -s --sort-keys ".[0] + .[1]" $langfilepath $suffixfilepath > /tmp/moodle-langtmp.json jq --indent 4 -s --sort-keys ".[0] + .[1]" "$langfilepath" "$suffixfilepath" > /tmp/moodle-langtmp.json
mv /tmp/moodle-langtmp.json $langfilepath mv /tmp/moodle-langtmp.json "$langfilepath"
fi fi
# Remove strings non exiting on langindex. # Remove strings non exiting on langindex.
query="with_entries(select([.key] | inside([$index_keys])))" query="with_entries(select([.key] | inside([$index_keys])))"
jq --indent 2 -r "$query" $langfilepath > /tmp/moodle-langtmp.json jq --indent 2 -r "$query" "$langfilepath" > /tmp/moodle-langtmp.json
mv /tmp/moodle-langtmp.json $langfilepath mv /tmp/moodle-langtmp.json "$langfilepath"
name=`jq -r .\"$lang\".name $LANGPACKS_PATH/languages.json` name=$(jq -r .\""$lang"\".name "$LANGPACKS_PATH"/languages.json)
local=`jq -r .\"$lang\".local $LANGPACKS_PATH/languages.json` local=$(jq -r .\""$lang"\".local "$LANGPACKS_PATH"/languages.json)
translated=`jq -r '. | length' $langfilepath` translated=$(jq -r '. | length' "$langfilepath")
percentage=`echo "($translated * 100) /$LANGINDEX_STRINGS" | bc` percentage=$(echo "($translated * 100) /$LANGINDEX_STRINGS" | bc)
progressbar $percentage progressbar "$percentage"
echo -e "Generated $hyphenlang\t $translated of $LANGINDEX_STRINGS\t $bar ($local local)" echo -e "Generated $hyphenlang\t $translated of $LANGINDEX_STRINGS\t $bar ($local local)"
# Add or update language name to config. # Add or update language name to config.
newlang="{\"$hyphenlang\": \"$name\"}" newlang="{\"$hyphenlang\": \"$name\"}"
languages=`jq -s --sort-keys ".[0].languages + $newlang" ../moodle.config.json` languages=$(jq -s --sort-keys ".[0].languages + $newlang" ../moodle.config.json)
jq --indent 4 -s ".[0].languages = $languages | .[0]" ../moodle.config.json > /tmp/moodle-langtmp.json jq --indent 4 -s ".[0].languages = $languages | .[0]" ../moodle.config.json > /tmp/moodle-langtmp.json
mv /tmp/moodle-langtmp.json ../moodle.config.json mv /tmp/moodle-langtmp.json ../moodle.config.json
} }
@ -59,28 +59,28 @@ function copy_lang {
function detect_lang { function detect_lang {
lang=$1 lang=$1
name=`jq -r .\"$lang\".name $LANGPACKS_PATH/languages.json` name=$(jq -r .\""$lang"\".name "$LANGPACKS_PATH"/languages.json)
if [ -z "$name" ] || [ "$name" == 'null' ]; then if [ -z "$name" ] || [ "$name" == 'null' ]; then
continue return
fi fi
hyphenlang=${lang/_/-} hyphenlang=${lang/_/-}
if [ -f $LANG_PATH/$hyphenlang.json ]; then if [ -f "$LANG_PATH"/"$hyphenlang".json ]; then
# Already exists # Already exists
continue return
fi fi
local=`jq -r .\"$lang\".local $LANGPACKS_PATH/languages.json` local=$(jq -r .\""$lang"\".local "$LANGPACKS_PATH"/languages.json)
translated=`jq -r .\"$lang\".translated $LANGPACKS_PATH/languages.json` translated=$(jq -r .\""$lang"\".translated "$LANGPACKS_PATH"/languages.json)
percentage=`echo "($translated * 100) /$TOTAL_STRINGS" | bc` percentage=$(echo "($translated * 100) /$TOTAL_STRINGS" | bc)
progressbar $percentage progressbar "$percentage"
echo -e "Checking $lang\t $translated of $TOTAL_STRINGS \t $bar ($local local)"; echo -e "Checking $lang\t $translated of $TOTAL_STRINGS \t $bar ($local local)";
if [[ ( $percentage -gt 75 && $local -gt 50 ) || ( $percentage -gt 50 && $local -gt 75 ) ]] ; then if [[ ( $percentage -gt 75 && $local -gt 50 ) || ( $percentage -gt 50 && $local -gt 75 ) ]] ; then
name=`jq -r .\"$lang\".name $LANGPACKS_PATH/languages.json` name=$(jq -r .\""$lang"\".name "$LANGPACKS_PATH"/languages.json)
echo "*** NEW LANGUAGE DETECTED $lang - $name ***" echo "*** NEW LANGUAGE DETECTED $lang - $name ***"
copy_lang $lang copy_lang "$lang"
fi fi
} }
@ -88,8 +88,8 @@ function load_langpacks {
get_app_version get_app_version
print_title 'Getting local mobile langs' print_title 'Getting local mobile langs'
if [ -d $LANGPACKS_PATH ]; then if [ -d "$LANGPACKS_PATH" ]; then
pushd $LANGPACKS_PATH pushd "$LANGPACKS_PATH"
git checkout "langpack_$LANGVERSION" git checkout "langpack_$LANGVERSION"
git pull git pull
@ -101,16 +101,16 @@ function load_langpacks {
popd popd
else else
git clone --depth 1 --single-branch --branch "langpack_$LANGVERSION" https://github.com/moodlehq/moodle-local_moodlemobileapp.git $LANGPACKS_PATH git clone --depth 1 --single-branch --branch "langpack_$LANGVERSION" https://github.com/moodlehq/moodle-local_moodlemobileapp.git "$LANGPACKS_PATH"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Cannot clone language repository" echo "Cannot clone language repository"
exit 1 exit 1
fi fi
fi fi
local_strings=`jq -r '.languages.local' $LANGPACKS_PATH/languages.json` local_strings=$(jq -r '.languages.local' "$LANGPACKS_PATH"/languages.json)
TOTAL_STRINGS=`jq -r '.languages.total' $LANGPACKS_PATH/languages.json` TOTAL_STRINGS=$(jq -r '.languages.total' "$LANGPACKS_PATH"/languages.json)
LANGINDEX_STRINGS=`jq -r '. | length' langindex.json` LANGINDEX_STRINGS=$(jq -r '. | length' langindex.json)
print_message "Total strings to translate $TOTAL_STRINGS ($local_strings local)"; print_message "Total strings to translate $TOTAL_STRINGS ($local_strings local)";
} }
@ -119,29 +119,29 @@ function load_langpacks {
function get_languages { function get_languages {
print_title 'Copying existing languages' print_title 'Copying existing languages'
# Existing languages, copy and clean the files. # Existing languages, copy and clean the files.
langs=`jq -r '.languages | keys[]' ../moodle.config.json` langs=$(jq -r '.languages | keys[]' ../moodle.config.json)
for lang in $langs; do for lang in $langs; do
lang=${lang//-/_} lang=${lang//-/_}
copy_lang $lang copy_lang "$lang"
done done
} }
# Entry function to detect new languages. # Entry function to detect new languages.
function detect_languages { function detect_languages {
# Do not detect new langs when suffix is set. # Do not detect new langs when suffix is set.
if [ ! -z $SUFFIX ]; then if [ -n "$SUFFIX" ]; then
return return
fi fi
print_title "Detect new languages" print_title "Detect new languages"
langs=`jq -r 'keys[]' $LANGPACKS_PATH/languages.json` langs=$(jq -r 'keys[]' "$LANGPACKS_PATH"/languages.json)
for lang in $langs; do for lang in $langs; do
if [[ $lang = *_wp ]]; then if [[ $lang = *_wp ]]; then
# Skip Workplace. # Skip Workplace.
continue continue
fi fi
detect_lang $lang detect_lang "$lang"
done done
} }
@ -156,10 +156,10 @@ function generate_local_module_file {
module_translations='' module_translations=''
keys=`jq -r 'map_values(select(contains("local_moodlemobileapp"))) | keys[]' langindex.json` keys=$(jq -r 'map_values(select(contains("local_moodlemobileapp"))) | keys[]' langindex.json)
for key in $keys; do for key in $keys; do
# Check if already parsed. # Check if already parsed.
translation=`jq -r .\"$key\" $LANG_PATH/en.json` translation=$(jq -r .\""$key"\" "$LANG_PATH"/en.json)
if [ -z "$translation" ]; then if [ -z "$translation" ]; then
echo "Key $key not translated!" echo "Key $key not translated!"
continue continue
@ -173,13 +173,13 @@ function generate_local_module_file {
return return
fi fi
echo -e $module_translations > /tmp/translations.php echo -e "$module_translations" > /tmp/translations.php
filepath="../../moodle-$APPMODULENAME/lang/en/$APPMODULENAME.php"; filepath="../../moodle-$APPMODULENAME/lang/en/$APPMODULENAME.php";
BEGIN_GEN=$(cat $filepath | grep -n '\/\* AUTO START \*\/' | sed 's/\(.*\):.*/\1/g') BEGIN_GEN=$(cat $filepath | grep -n '\/\* AUTO START \*\/' | sed 's/\(.*\):.*/\1/g')
END_GEN=$(cat $filepath | grep -n '\/\* AUTO END \*\/' | sed 's/\(.*\):.*/\1/g') END_GEN=$(cat $filepath | grep -n '\/\* AUTO END \*\/' | sed 's/\(.*\):.*/\1/g')
cat <(head -n $BEGIN_GEN $filepath) /tmp/translations.php <(tail -n +$END_GEN $filepath) > /tmp/translations_temp.php cat <(head -n "$BEGIN_GEN" $filepath) /tmp/translations.php <(tail -n +"$END_GEN" $filepath) > /tmp/translations_temp.php
mv /tmp/translations_temp.php $filepath mv /tmp/translations_temp.php $filepath
cp langindex.json ../../moodle-$APPMODULENAME cp langindex.json ../../moodle-$APPMODULENAME

View File

@ -10,7 +10,7 @@
DIR="${BASH_SOURCE%/*}" DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
cd $DIR cd "$DIR"
source "functions.sh" source "functions.sh"
source "lang_functions.sh" source "lang_functions.sh"