lang bug fix

This commit is contained in:
2026-10-13 13:42:56 +02:00
parent f7ff06cce6
commit 2b4732d0e3
3 changed files with 54 additions and 9 deletions
+47 -8
View File
@@ -58,7 +58,7 @@ locale_check()
local req_cs_norm="${req_cs,,}"
req_cs_norm="${req_cs_norm//[^a-z0-9]/}"
local cand body cs mod cs_norm
local cand body cs mod cs_norm fallback=""
while IFS= read -r cand; do
[[ -z $cand ]] && continue
body="$cand"; cs=""; mod=""
@@ -73,10 +73,23 @@ locale_check()
# accept any codeset the system provides.
[[ -z $req_cs_norm || $cs_norm == "$req_cs_norm" ]] || continue
# With no codeset requested, UTF-8 is preferred: "locale -a" may list
# an older charset first (fr_FR, fr_FR.iso88591, fr_FR.utf8). Any other
# match is kept as a fallback in case no UTF-8 variant exists.
if [[ -z $req_cs_norm && $cs_norm != "utf8" ]]; then
[[ -z $fallback ]] && fallback="$cand"
continue
fi
printf '%s\n' "$cand"
return 0
done < <(locale -a 2>/dev/null)
if [[ -n $fallback ]]; then
printf '%s\n' "$fallback"
return 0
fi
disp W "Locale '$requested' is not installed on this system."
return 1
}
@@ -95,9 +108,8 @@ setlocale()
disp E "Invalid options, use \"setlocale --help\" to display usage."
return 1
fi
local -a _pargs=()
readarray -td '' _pargs < <(printf '%s' "$PARSED" | xargs printf '%s\0')
set -- "${_pargs[@]}"
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
while true; do
case "$1" in
-h|--help)
@@ -125,6 +137,12 @@ setlocale()
# various codeset writings (UTF-8, UTF8, utf8, utf-8, ...).
loc=$(locale_check "$loc") || return 1
# LC_ALL overrides every other variable (setc sets it), and so does any
# per-category variable inherited from the environment: drop them all so
# that the new locale really takes effect.
unset LC_ALL LC_ADDRESS LC_IDENTIFICATION LC_MEASUREMENT LC_NAME \
LC_PAPER LC_TELEPHONE
export LANG=$loc
export LC_MESSAGES=$loc
export LC_TIME=$loc
@@ -144,6 +162,12 @@ export -f setlocale
# Usage: setc
setc()
{
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
printf "setc: Set the console locale to the standard C (POSIX) locale.\n"
printf "Usage: setc\n"
return 0
fi
# Locale definitions
export LC_ALL=C
export LANG=C
@@ -202,19 +226,34 @@ build_locale_shortcuts()
fname="set${alias}"
# The generated function must never replace the real commands
if [[ "$fname" == "setlocale" || "$fname" == "setc" ]]; then
disp W "Ignoring locale alias '$alias' in SET_LOCALE: '$fname' is a profile command."
continue
fi
# Optional collision warning
if declare -F "$fname" >/dev/null 2>&1; then
disp W "Overriding existing function '$fname'."
fi
# Build function dynamically
# shellcheck disable=SC2016
eval "${fname}() { setlocale \"$loc\"; }"
# Build function dynamically. %q quotes the locale so that nothing in
# the configuration value can be interpreted by eval. The shortcut
# answers --help without changing anything ("help <command>" calls it).
local qloc
printf -v qloc '%q' "$loc"
eval "${fname}() {
if [[ \"\${1:-}\" == -h || \"\${1:-}\" == --help ]]; then
printf '%s: switch the console locale (see setlocale --help).\n' \"\${FUNCNAME[0]}\"
return 0
fi
setlocale ${qloc}
}"
# shellcheck disable=SC2163
export -f "$fname"
done
unset cfg item alias loc fname locale_items
unset cfg item alias loc fname locale_items qloc
}
# ------------------------------------------------------------------------------