feat(i18n): add a Bash helper for the translations of the shell tools

It loads every text of the given namespaces with a single fws-i18n call
instead of starting Python for each message, and accepts translated
yes/no answers.
This commit is contained in:
nocode committed 2026-11-26 18:18:25 +01:00
1 parent c465593078
commit 6e0eb8f6fc
1 file changed
+40
@@ -0,0 +1,40 @@
# FWS — translations for the FWS shell tools (Bash, sourced).
#
# . /usr/local/lib/fws/i18n.sh <namespace>...
# say "$(t <key> [name=value]...)"
#
# Loads every text of the given namespaces of system-translations.toml with a
# single fws-i18n call, in the language of the environment; t then fills the
# {placeholders} in Bash. A missing text prints its key, like the Python tools.
# Literal braces ({{ }}) are not supported in texts used from shell.
declare -gA FWS_T=()
eval "$(/usr/local/bin/fws-i18n --catalog system --shell "$@" 2>/dev/null)" || true
t() {
local text="${FWS_T[$1]-$1}" pair
shift
for pair in "$@"; do
text="${text//"{${pair%%=*}}"/"${pair#*=}"}"
done
printf '%s' "$text"
}
# The answers a yes/no prompt accepts: the translated word, its first letter,
# the same without accents ("si" for "sí": not every keyboard has them), and
# the English ones that every keyboard can type.
t_is_yes() {
local answer="${1,,}" yes plain
yes="$(t common.yes)"
yes="${yes,,}"
[ -n "$answer" ] || return 1
# C.UTF-8 is built into glibc, so transliteration works even when the
# session's locale is not generated; anything else than plain letters
# (a non-Latin word comes out as "?" marks) keeps the word as written.
plain="$(printf '%s' "$yes" | LC_ALL=C.UTF-8 iconv -f UTF-8 -t ASCII//TRANSLIT 2>/dev/null)"
[[ "$plain" =~ ^[a-z]+$ ]] || plain="$yes"
case "$answer" in
y | yes | "$yes" | "${yes:0:1}" | "$plain" | "${plain:0:1}") return 0 ;;
esac
return 1
}