diff --git a/profile.d/lang.sh b/profile.d/lang.sh index 6b1988e..cc7b865 100644 --- a/profile.d/lang.sh +++ b/profile.d/lang.sh @@ -109,27 +109,73 @@ export -f setc # ------------------------------------------------------------------------------ -# Change locale to French -# Usage: setfr -setfr() +# Build dynamic locale shortcuts from SET_LOCALE +# Expected format: +# 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 - setlocale "fr_FR.UTF-8" + local cfg="${SET_LOCALE:-}" + 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 -# ------------------------------------------------------------------------------ - - -# ------------------------------------------------------------------------------ -# Change locale to US (needed by Steam) -# Usage: setus -setus() -{ - setlocale "en_US.UTF-8" -} -export -f setus +export -f build_locale_shortcuts # ------------------------------------------------------------------------------ +load_conf lang +build_locale_shortcut # ------------------------------------------------------------------------------ # EOF