_msg: Refactor

This commit is contained in:
Stig-Ørjan Smelror 2024-02-14 13:06:44 +01:00
parent 918e4d2fd8
commit 769661cee0
2 changed files with 12 additions and 13 deletions

View File

@ -1,5 +1,6 @@
typeset -A localization typeset -A localization
localization=(_qsgen2_msg_2 "- Tvungen oppdatering: Genererer alt" localization=(
_qsgen2_msg_2 "- Tvungen oppdatering: Genererer alt"
_qsgen2_msg_3 "* %SCRIPT_NAME% kan ikke analysere dette nettstedet. Avslutter. *" _qsgen2_msg_3 "* %SCRIPT_NAME% kan ikke analysere dette nettstedet. Avslutter. *"
_qsgen2_msg_4 "Vennligst installer Pandoc." _qsgen2_msg_4 "Vennligst installer Pandoc."
_qsgen2_msg_5 "Ingen gyldig generator funnet. Er du sikker på at du har valgt riktig generator i 'config'?" _qsgen2_msg_5 "Ingen gyldig generator funnet. Er du sikker på at du har valgt riktig generator i 'config'?"

22
qsgen2
View File

@ -16,7 +16,7 @@ QSGEN="Quick Site Generator 2"
# Set to true or false # Set to true or false
# This will show debug information from every function in this script # This will show debug information from every function in this script
# You can also set debug=true in a single function if you want to debug only that specific one. # You can also set debug=true in a single function if you want to debug only that specific one.
globaldebug=true globaldebug=false
# Use Zsh fpath to set the path to some extra functions # Use Zsh fpath to set the path to some extra functions
fpath=(${HOME}/bin/include/common ${HOME}/bin/include/qsgen2/lang $fpath) fpath=(${HOME}/bin/include/common ${HOME}/bin/include/qsgen2/lang $fpath)
@ -69,19 +69,17 @@ fi
function _msg() { function _msg() {
local type=$1 local type=$1
shift # Remove the first argument so $@ now contains only message keys or parts shift # Remove the first argument (type) so $@ now contains only keys or additional strings
local full_msg="" local full_msg=""
for part in "$@"; do for key in "$@"; do
if [[ -n "${localization[$part]}" ]]; then if [[ "${localization[$key]}" ]]; then
# Part is a localization key # If key exists in the localization array, append its value
part="${localization[$part]}" full_msg+="${localization[$key]}"
elif [[ "$part" == "%SCRIPT_NAME%" ]]; then else
# Special handling for script name placeholder # If key does not exist, treat it as a literal string (useful for dynamic content like ${0:t})
part=${0:t} full_msg+="$key"
fi fi
# Append part to the full message
full_msg+="$part"
done done
# Apply color formatting based on type and use printf for safe output # Apply color formatting based on type and use printf for safe output