made langage definition shortcuts configurable
This commit is contained in:
@@ -109,27 +109,73 @@ export -f setc
|
|||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# Change locale to French
|
# Build dynamic locale shortcuts from SET_LOCALE
|
||||||
# Usage: setfr
|
# Expected format:
|
||||||
setfr()
|
# SET_LOCALE="fr:fr_FR.UTF-8,us:en_US.UTF-8,es:es_ES.UTF-8"
|
||||||
|
# This creates functions:
|
||||||
|
# setfr, setus, setes, ...
|
||||||
|
build_locale_shortcuts()
|
||||||
{
|
{
|
||||||
# Set fr locale definitions
|
local cfg="${SET_LOCALE:-}"
|
||||||
setlocale "fr_FR.UTF-8"
|
local item="" alias="" loc="" fname=""
|
||||||
|
local -a locale_items=()
|
||||||
|
|
||||||
|
[[ -z "$cfg" ]] && return 0
|
||||||
|
|
||||||
|
IFS=',' read -r -a locale_items <<< "$cfg"
|
||||||
|
for item in "${locale_items[@]}"; do
|
||||||
|
# Trim surrounding spaces
|
||||||
|
item="${item#"${item%%[![:space:]]*}"}"
|
||||||
|
item="${item%"${item##*[![:space:]]}"}"
|
||||||
|
|
||||||
|
[[ -z "$item" ]] && continue
|
||||||
|
|
||||||
|
if [[ "$item" != *:* ]]; then
|
||||||
|
disp W "Ignoring invalid SET_LOCALE entry: '$item' (expected alias:locale)."
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
alias="${item%%:*}"
|
||||||
|
loc="${item#*:}"
|
||||||
|
|
||||||
|
# Trim alias/locale spaces
|
||||||
|
alias="${alias#"${alias%%[![:space:]]*}"}"
|
||||||
|
alias="${alias%"${alias##*[![:space:]]}"}"
|
||||||
|
loc="${loc#"${loc%%[![:space:]]*}"}"
|
||||||
|
loc="${loc%"${loc##*[![:space:]]}"}"
|
||||||
|
|
||||||
|
# Validate alias for safe function names
|
||||||
|
if [[ ! "$alias" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
|
||||||
|
disp W "Ignoring unsafe locale alias '$alias' in SET_LOCALE."
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
[[ -z "$loc" ]] && {
|
||||||
|
disp W "Ignoring empty locale for alias '$alias' in SET_LOCALE."
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
fname="set${alias}"
|
||||||
|
|
||||||
|
# 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\"; }"
|
||||||
|
# shellcheck disable=SC2163
|
||||||
|
export -f "$fname"
|
||||||
|
done
|
||||||
|
|
||||||
|
unset cfg item alias loc fname locale_items
|
||||||
}
|
}
|
||||||
export -f setfr
|
export -f build_locale_shortcuts
|
||||||
# ------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------
|
|
||||||
# Change locale to US (needed by Steam)
|
|
||||||
# Usage: setus
|
|
||||||
setus()
|
|
||||||
{
|
|
||||||
setlocale "en_US.UTF-8"
|
|
||||||
}
|
|
||||||
export -f setus
|
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
load_conf lang
|
||||||
|
build_locale_shortcut
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# EOF
|
# EOF
|
||||||
|
|||||||
Reference in New Issue
Block a user