2023-05-18 11:59:02 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# Functions to update langpacks.
|
|
|
|
#
|
|
|
|
|
|
|
|
APPMODULENAME='local_moodlemobileapp'
|
|
|
|
|
|
|
|
TOTAL_STRINGS=0
|
|
|
|
LANGINDEX_STRINGS=0
|
|
|
|
|
2023-10-31 15:08:09 +00:00
|
|
|
LANGPACKS_PATH='/tmp/moodleapp-lang'
|
|
|
|
|
2023-05-18 11:59:02 +00:00
|
|
|
function progressbar {
|
|
|
|
let _progress=(${1}*100/100*100)/100
|
|
|
|
let _done=(${_progress}*4)/10
|
|
|
|
let _left=40-$_done
|
|
|
|
_fill=$(printf "%${_done}s")
|
|
|
|
_empty=$(printf "%${_left}s")
|
|
|
|
bar=`printf "[${_fill// /#}${_empty// /-}] ${_progress}%%"`
|
|
|
|
}
|
|
|
|
|
|
|
|
# Copy language file
|
|
|
|
function copy_lang {
|
|
|
|
lang=$1
|
|
|
|
|
2023-06-27 12:51:23 +00:00
|
|
|
index_keys=$(jq -r 'to_entries[] | "\"\(.key)\","' langindex.json)
|
2023-07-19 10:55:47 +00:00
|
|
|
index_keys=${index_keys:0:${#index_keys}-1}
|
2023-05-18 11:59:02 +00:00
|
|
|
|
|
|
|
hyphenlang=${lang/_/-}
|
|
|
|
langfilepath=$LANG_PATH/$hyphenlang.json
|
2023-06-27 12:51:23 +00:00
|
|
|
cp "$LANGPACKS_PATH"/"$lang".json "$langfilepath"
|
2023-05-18 11:59:02 +00:00
|
|
|
|
|
|
|
# Merge SUFFIX file if exists.
|
2023-06-27 12:51:23 +00:00
|
|
|
if [ -n "$SUFFIX" ] && [ -f "$LANGPACKS_PATH/${lang}_${SUFFIX}.json" ]; then
|
2023-05-18 11:59:02 +00:00
|
|
|
suffixfilepath="$LANGPACKS_PATH/${lang}_${SUFFIX}.json"
|
2023-06-27 12:51:23 +00:00
|
|
|
jq --indent 4 -s --sort-keys ".[0] + .[1]" "$langfilepath" "$suffixfilepath" > /tmp/moodle-langtmp.json
|
|
|
|
mv /tmp/moodle-langtmp.json "$langfilepath"
|
2023-05-18 11:59:02 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Remove strings non exiting on langindex.
|
|
|
|
query="with_entries(select([.key] | inside([$index_keys])))"
|
2023-06-27 12:51:23 +00:00
|
|
|
jq --indent 2 -r "$query" "$langfilepath" > /tmp/moodle-langtmp.json
|
|
|
|
mv /tmp/moodle-langtmp.json "$langfilepath"
|
|
|
|
|
|
|
|
name=$(jq -r .\""$lang"\".name "$LANGPACKS_PATH"/languages.json)
|
|
|
|
local=$(jq -r .\""$lang"\".local "$LANGPACKS_PATH"/languages.json)
|
|
|
|
translated=$(jq -r '. | length' "$langfilepath")
|
|
|
|
percentage=$(echo "($translated * 100) /$LANGINDEX_STRINGS" | bc)
|
|
|
|
progressbar "$percentage"
|
2023-05-18 11:59:02 +00:00
|
|
|
echo -e "Generated $hyphenlang\t $translated of $LANGINDEX_STRINGS\t $bar ($local local)"
|
|
|
|
|
|
|
|
|
|
|
|
# Add or update language name to config.
|
|
|
|
newlang="{\"$hyphenlang\": \"$name\"}"
|
2023-06-27 12:51:23 +00:00
|
|
|
languages=$(jq -s --sort-keys ".[0].languages + $newlang" ../moodle.config.json)
|
2023-05-18 11:59:02 +00:00
|
|
|
jq --indent 4 -s ".[0].languages = $languages | .[0]" ../moodle.config.json > /tmp/moodle-langtmp.json
|
|
|
|
mv /tmp/moodle-langtmp.json ../moodle.config.json
|
|
|
|
}
|
|
|
|
|
|
|
|
function detect_lang {
|
|
|
|
lang=$1
|
|
|
|
|
2023-06-27 12:51:23 +00:00
|
|
|
name=$(jq -r .\""$lang"\".name "$LANGPACKS_PATH"/languages.json)
|
2023-05-18 11:59:02 +00:00
|
|
|
if [ -z "$name" ] || [ "$name" == 'null' ]; then
|
2023-06-27 12:51:23 +00:00
|
|
|
return
|
2023-05-18 11:59:02 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
hyphenlang=${lang/_/-}
|
2023-06-27 12:51:23 +00:00
|
|
|
if [ -f "$LANG_PATH"/"$hyphenlang".json ]; then
|
2023-05-18 11:59:02 +00:00
|
|
|
# Already exists
|
2023-06-27 12:51:23 +00:00
|
|
|
return
|
2023-05-18 11:59:02 +00:00
|
|
|
fi
|
|
|
|
|
2023-06-27 12:51:23 +00:00
|
|
|
local=$(jq -r .\""$lang"\".local "$LANGPACKS_PATH"/languages.json)
|
|
|
|
translated=$(jq -r .\""$lang"\".translated "$LANGPACKS_PATH"/languages.json)
|
|
|
|
percentage=$(echo "($translated * 100) /$TOTAL_STRINGS" | bc)
|
|
|
|
progressbar "$percentage"
|
2023-05-18 11:59:02 +00:00
|
|
|
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
|
2023-06-27 12:51:23 +00:00
|
|
|
name=$(jq -r .\""$lang"\".name "$LANGPACKS_PATH"/languages.json)
|
2023-05-18 11:59:02 +00:00
|
|
|
echo "*** NEW LANGUAGE DETECTED $lang - $name ***"
|
|
|
|
|
2023-06-27 12:51:23 +00:00
|
|
|
copy_lang "$lang"
|
2023-05-18 11:59:02 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function load_langpacks {
|
|
|
|
get_app_version
|
|
|
|
|
|
|
|
print_title 'Getting local mobile langs'
|
2023-06-27 12:51:23 +00:00
|
|
|
if [ -d "$LANGPACKS_PATH" ]; then
|
|
|
|
pushd "$LANGPACKS_PATH"
|
2023-05-18 11:59:02 +00:00
|
|
|
|
|
|
|
git checkout "langpack_$LANGVERSION"
|
|
|
|
git pull
|
|
|
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "Cannot update language repository"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
popd
|
|
|
|
else
|
2023-06-27 12:51:23 +00:00
|
|
|
git clone --depth 1 --single-branch --branch "langpack_$LANGVERSION" https://github.com/moodlehq/moodle-local_moodlemobileapp.git "$LANGPACKS_PATH"
|
2023-05-18 11:59:02 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "Cannot clone language repository"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-06-27 12:51:23 +00:00
|
|
|
local_strings=$(jq -r '.languages.local' "$LANGPACKS_PATH"/languages.json)
|
|
|
|
TOTAL_STRINGS=$(jq -r '.languages.total' "$LANGPACKS_PATH"/languages.json)
|
|
|
|
LANGINDEX_STRINGS=$(jq -r '. | length' langindex.json)
|
2023-05-18 11:59:02 +00:00
|
|
|
|
|
|
|
print_message "Total strings to translate $TOTAL_STRINGS ($local_strings local)";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Entry function to get all language files.
|
|
|
|
function get_languages {
|
|
|
|
print_title 'Copying existing languages'
|
|
|
|
# Existing languages, copy and clean the files.
|
2023-06-27 12:51:23 +00:00
|
|
|
langs=$(jq -r '.languages | keys[]' ../moodle.config.json)
|
2023-05-18 11:59:02 +00:00
|
|
|
for lang in $langs; do
|
|
|
|
lang=${lang//-/_}
|
2023-06-27 12:51:23 +00:00
|
|
|
copy_lang "$lang"
|
2023-05-18 11:59:02 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Entry function to detect new languages.
|
|
|
|
function detect_languages {
|
|
|
|
# Do not detect new langs when suffix is set.
|
2023-06-27 12:51:23 +00:00
|
|
|
if [ -n "$SUFFIX" ]; then
|
2023-05-18 11:59:02 +00:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
print_title "Detect new languages"
|
2023-06-27 12:51:23 +00:00
|
|
|
langs=$(jq -r 'keys[]' "$LANGPACKS_PATH"/languages.json)
|
2023-05-18 11:59:02 +00:00
|
|
|
for lang in $langs; do
|
|
|
|
if [[ $lang = *_wp ]]; then
|
|
|
|
# Skip Workplace.
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2023-06-27 12:51:23 +00:00
|
|
|
detect_lang "$lang"
|
2023-05-18 11:59:02 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Entry function to generate translation module file.
|
|
|
|
function generate_local_module_file {
|
|
|
|
if [ ! -d "../../moodle-$APPMODULENAME" ]; then
|
|
|
|
print_error "Module $APPMODULENAME directory does not exists, skipping..."
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
print_title "Generating $APPMODULENAME..."
|
|
|
|
|
2023-10-03 07:08:58 +00:00
|
|
|
gulp
|
|
|
|
|
2023-05-18 11:59:02 +00:00
|
|
|
module_translations=''
|
|
|
|
|
2023-06-27 12:51:23 +00:00
|
|
|
keys=$(jq -r 'map_values(select(contains("local_moodlemobileapp"))) | keys[]' langindex.json)
|
2023-05-18 11:59:02 +00:00
|
|
|
for key in $keys; do
|
|
|
|
# Check if already parsed.
|
2023-06-27 12:51:23 +00:00
|
|
|
translation=$(jq -r .\""$key"\" "$LANG_PATH"/en.json)
|
2023-10-03 07:08:58 +00:00
|
|
|
if [ -z "$translation" ] || [ "$translation" == 'null' ]; then
|
2023-05-18 11:59:02 +00:00
|
|
|
echo "Key $key not translated!"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
translation="${translation//\'/\\\'}"
|
|
|
|
module_translations="$module_translations\$string['$key'] = '$translation';\n";
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -z "$module_translations" ]; then
|
|
|
|
print_error "ERROR, translations not found, you probably didn't run gulp lang!";
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2023-06-27 12:51:23 +00:00
|
|
|
echo -e "$module_translations" > /tmp/translations.php
|
2023-05-18 11:59:02 +00:00
|
|
|
|
|
|
|
filepath="../../moodle-$APPMODULENAME/lang/en/$APPMODULENAME.php";
|
|
|
|
|
|
|
|
BEGIN_GEN=$(cat $filepath | grep -n '\/\* AUTO START \*\/' | sed 's/\(.*\):.*/\1/g')
|
|
|
|
END_GEN=$(cat $filepath | grep -n '\/\* AUTO END \*\/' | sed 's/\(.*\):.*/\1/g')
|
2023-06-27 12:51:23 +00:00
|
|
|
cat <(head -n "$BEGIN_GEN" $filepath) /tmp/translations.php <(tail -n +"$END_GEN" $filepath) > /tmp/translations_temp.php
|
2023-05-18 11:59:02 +00:00
|
|
|
mv /tmp/translations_temp.php $filepath
|
|
|
|
|
|
|
|
cp langindex.json ../../moodle-$APPMODULENAME
|
|
|
|
}
|