Vmeda.Online/scripts/create_langindex_functions.sh

360 lines
9.3 KiB
Bash
Raw Normal View History

#!/bin/bash
#
# Functions used to create langidex.
#
SERVER_URL='https://download.moodle.org/'
# Downloads a file and if it's a zip file, unzip it.
function download_file {
local url=$1
2023-06-27 12:51:23 +00:00
local filename=$(basename "${url}")
2023-06-27 12:51:23 +00:00
pushd "$LANGPACKS_PATH" > /dev/null
2023-06-27 12:51:23 +00:00
curl -s "$url" --output "$filename" > /dev/null
size=$(du -k "$filename" | cut -f 1)
2023-06-27 12:51:23 +00:00
if [ ! -n "$filename" ] || [ "$size" -le 1 ]; then
echo "Wrong or corrupt file $filename"
2023-06-27 12:51:23 +00:00
rm "$filename"
popd > /dev/null
return
fi
if [[ $filename == *.zip ]]; then
local lang="${filename%.*}"
# Delete previous downloaded folder
2023-06-27 12:51:23 +00:00
rm -R "$lang" > /dev/null 2>&1> /dev/null
# Unzip
2023-06-27 12:51:23 +00:00
unzip -o -u "$lang".zip > /dev/null
# Delete the zip
2023-06-27 12:51:23 +00:00
rm "$filename"
fi
popd > /dev/null
}
function get_english {
2023-06-27 12:51:23 +00:00
if [ ! -d "$LANGPACKS_PATH" ]; then
mkdir "$LANGPACKS_PATH"
fi
get_app_version
echo "Getting English language..."
download_file "$SERVER_URL/download.php/direct/langpack/$LANGVERSION/en.zip"
}
#Saves or updates a key on langindex_old.json
function save_key {
local key=$1
local found=$2
print_ok "$key=$found"
echo "{\"$key\": \"$found\"}" > langindex_old.json
jq -s '.[0] + .[1]' langindex.json langindex_old.json > langindex_new.json
mv langindex_new.json langindex.json
}
#Removes a key on langindex_old.json
function remove_key {
local key=$1
2023-06-27 12:51:23 +00:00
cat langindex.json | jq 'del(."'"$key"'")' > langindex_new.json
mv langindex_new.json langindex.json
print_ok "Deleted unused key $key"
}
#Check if a lang id exists in php file
function exists_in_file {
local file=$1
local id=$2
2023-06-27 12:51:23 +00:00
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_//1)
completeFile="$LANGPACKS_PATH/en/$file.php"
if [ -f "$completeFile" ]; then
2023-06-27 12:51:23 +00:00
foundInFile=$(grep "string\['${id}'\]" "${completeFile}")
if [ -n "$foundInFile" ]; then
coincidence=1
found=$file
return
fi
fi
coincidence=0
found=0
}
#Checks if a key exists on the original local_moodlemobileapp.php
function exists_in_mobile {
local file='local_moodlemobileapp'
2023-06-27 12:51:23 +00:00
exists_in_file $file "$key"
}
function do_match {
match=${1/\{\{/\{}
match=${match/\}\}/\}}
filematch=""
2023-06-27 12:51:23 +00:00
coincidence=$(grep "$match" "$LANGPACKS_PATH"/en/*.php | wc -l)
if [ "$coincidence" -eq 1 ]; then
filematch=$(grep "$match" "$LANGPACKS_PATH"/en/*.php | cut -d'/' -f5 | cut -d'.' -f1)
exists_in_file "$filematch" "$plainid"
elif [ "$coincidence" -gt 0 ] && [ "$#" -gt 1 ]; then
print_message "$2"
tput setaf 6
2023-06-27 12:51:23 +00:00
grep "$match" "$LANGPACKS_PATH"/en/*.php
else
coincidence=0
fi
}
#Find if the id or the value can be found on files to help providing a solution.
function find_matches {
do_match "string\[\'$plainid\'\] = \'$value\'" "Found EXACT match for $key in the following paths"
2023-06-27 12:51:23 +00:00
if [ "$coincidence" -gt 0 ]; then
case=1
2023-06-27 12:51:23 +00:00
save_key "$key" "TBD"
return
fi
do_match " = \'$value\'" "Found some string VALUES for $key in the following paths"
2023-06-27 12:51:23 +00:00
if [ "$coincidence" -gt 0 ]; then
case=2
2023-06-27 12:51:23 +00:00
save_key "$key" "TBD"
return
fi
do_match "string\[\'$plainid\'\]" "Found some string KEYS for $key in the following paths, value $value"
2023-06-27 12:51:23 +00:00
if [ "$coincidence" -gt 0 ]; then
case=3
2023-06-27 12:51:23 +00:00
save_key "$key" "TBD"
return
fi
print_message "No match found for $key add it to local_moodlemobileapp"
2023-06-27 12:51:23 +00:00
save_key "$key" "local_moodlemobileapp"
}
function find_single_matches {
do_match "string\[\'$plainid\'\] = \'$value\'"
2023-06-27 12:51:23 +00:00
if [ -n "$filematch" ] && [ "$found" != 0 ]; then
case=1
return
fi
do_match " = \'$value\'"
2023-06-27 12:51:23 +00:00
if [ -n "$filematch" ] && [ "$filematch" != 'local_moodlemobileapp' ]; then
case=2
print_message "Found some string VALUES for $key in the following paths $filematch"
tput setaf 6
2023-06-27 12:51:23 +00:00
grep "$match" "$LANGPACKS_PATH"/en/*.php
return
fi
do_match "string\[\'$plainid\'\]"
2023-06-27 12:51:23 +00:00
if [ -n "$filematch" ] && [ "$found" != 0 ]; then
case=3
return
fi
}
#Tries to gues the file where the id will be found.
function guess_file {
local key=$1
local value=$2
2023-06-27 12:51:23 +00:00
local type=$(echo "$key" | cut -d'.' -f1)
local component=$(echo "$key" | cut -d'.' -f2)
local plainid=$(echo "$key" | cut -d'.' -f3-)
if [ -z "$plainid" ]; then
plainid=$component
component='moodle'
fi
2023-06-27 12:51:23 +00:00
exists_in_file "$component" "$plainid"
2023-06-27 12:51:23 +00:00
if [ "$found" == 0 ]; then
tempid=$(echo "$plainid" | sed s/^mod_//1)
if [ "$component" == 'moodle' ] && [ "$tempid" != "$plainid" ]; then
exists_in_file "$plainid" pluginname
2023-06-27 12:51:23 +00:00
if [ "$found" != 0 ]; then
found=$found/pluginname
fi
fi
fi
# Not found in file, try in local_moodlemobileapp
2023-06-27 12:51:23 +00:00
if [ "$found" == 0 ]; then
exists_in_mobile
fi
# Still not found, if only found in one file, use it.
2023-06-27 12:51:23 +00:00
if [ "$found" == 0 ]; then
find_single_matches
fi
# Last fallback.
2023-06-27 12:51:23 +00:00
if [ "$found" == 0 ]; then
exists_in_file 'moodle' "$plainid"
fi
2023-06-27 12:51:23 +00:00
if [ "$found" == 0 ]; then
find_matches
else
2023-06-27 12:51:23 +00:00
save_key "$key" "$found"
fi
}
function current_translation_exists {
local key=$1
local current=$2
local file=$3
2023-06-27 12:51:23 +00:00
plainid=$(echo "$key" | cut -d'.' -f3-)
if [ -z "$plainid" ]; then
2023-06-27 12:51:23 +00:00
plainid=$(echo "$key" | cut -d'.' -f2)
fi
2023-06-27 12:51:23 +00:00
local currentFile=$(echo "$current" | cut -d'/' -f1)
local currentStr=$(echo "$current" | cut -d'/' -f2-)
if [ "$currentFile" == "$current" ]; then
currentStr=$plainid
fi
2023-06-27 12:51:23 +00:00
exists_in_file "$currentFile" "$currentStr"
if [ "$found" == 0 ]; then
# Translation not found.
exec="jq -r .\"$key\" $file"
2023-06-27 12:51:23 +00:00
value=$($exec)
found=$($exec)
print_error "Translation of '$currentStr' not found in '$currentFile'"
2023-06-27 12:51:23 +00:00
guess_file "$key" "$value"
fi
}
#Finds if there's a better file where to get the id from.
function find_better_file {
local key=$1
local value=$2
local current=$3
2023-06-27 12:51:23 +00:00
local type=$(echo "$key" | cut -d'.' -f1)
local component=$(echo "$key" | cut -d'.' -f2)
local plainid=$(echo "$key" | cut -d'.' -f3-)
if [ -z "$plainid" ]; then
plainid=$component
component='moodle'
fi
2023-06-27 12:51:23 +00:00
local currentFile=$(echo "$current" | cut -d'/' -f1)
local currentStr=$(echo "$current" | cut -d'/' -f2-)
if [ "$currentFile" == "$current" ]; then
currentStr=$plainid
fi
2023-06-27 12:51:23 +00:00
exists_in_file "$component" "$plainid"
if [ "$found" != 0 ] && [ "$currentStr" == "$plainid" ]; then
if [ "$found" != "$currentFile" ]; then
print_ok "Key '$key' found in component, no need to replace old '$current'"
fi
return
fi
# Still not found, if only found in one file, use it.
2023-06-27 12:51:23 +00:00
if [ "$found" == 0 ]; then
find_single_matches
fi
2023-06-27 12:51:23 +00:00
if [ "$found" != 0 ] && [ "$found" != "$currentFile" ] && [ "$case" -lt 3 ]; then
print_message "Indexed string '$key' found in '$found' better than '$current'"
return
fi
2023-06-27 12:51:23 +00:00
if [ "$currentFile" == 'local_moodlemobileapp' ]; then
exists_in_mobile
else
2023-06-27 12:51:23 +00:00
exists_in_file "$currentFile" "$currentStr"
fi
2023-06-27 12:51:23 +00:00
if [ "$found" == 0 ]; then
print_error "Indexed string '$key' not found on current place '$current'"
2023-06-27 12:51:23 +00:00
if [ "$currentFile" != 'local_moodlemobileapp' ]; then
print_error "Execute this on AMOS
CPY [$currentStr,$currentFile],[$key,local_moodlemobileapp]"
2023-06-27 12:51:23 +00:00
save_key "$key" "local_moodlemobileapp"
fi
fi
}
# Parses the file.
function parse_file {
file="$LANG_PATH/en.json"
findbetter=$1
2023-06-27 12:51:23 +00:00
keys=$(jq -r 'keys[]' "$file")
for key in $keys; do
2023-06-27 12:51:23 +00:00
echo -n '.'
# Check if already parsed.
exec="jq -r .\"$key\" langindex.json"
2023-06-27 12:51:23 +00:00
found=$($exec)
if [ -z "$found" ] || [ "$found" == 'null' ]; then
2023-06-27 12:51:23 +00:00
exec="jq -r .\"$key\" $file"
2023-06-27 12:51:23 +00:00
value=$($exec)
guess_file "$key" "$value"
else
if [ "$found" == 'donottranslate' ]; then
# Do nothing since is not translatable.
continue
2023-06-27 12:51:23 +00:00
elif [ -n "$findbetter" ]; then
exec="jq -r .\"$key\" $file"
2023-06-27 12:51:23 +00:00
value=$($exec)
find_better_file "$key" "$value" "$found"
elif [ "$found" != 'local_moodlemobileapp' ]; then
current_translation_exists "$key" "$found" "$file"
fi
fi
done
# Do some cleanup
2023-06-27 12:51:23 +00:00
langkeys=$(jq -r 'keys[]' langindex.json)
findkeys="${keys[@]}"
for key in $langkeys; do
# Check if already used.
array_contains "$key" "$findkeys"
if [ -z "$found" ] || [ "$found" == 'null' ]; then
2023-06-27 12:51:23 +00:00
remove_key "$key"
fi
done
}
# Checks if an array contains an string.
function array_contains {
local hayjack=$2
local needle=$1
found=''
for i in $hayjack; do
if [ "$i" == "$needle" ] ; then
found=$i
return
fi
done
}