From 6e0eb8f6fcd96885cbfb6d999dd2a77d163a389a Mon Sep 17 00:00:00 2001 From: fkDead Date: Thu, 26 Nov 2026 18:18:25 +0100 Subject: [PATCH] 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. --- .../airootfs/usr/local/lib/fws/i18n.sh | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 configs/baseline/airootfs/usr/local/lib/fws/i18n.sh diff --git a/configs/baseline/airootfs/usr/local/lib/fws/i18n.sh b/configs/baseline/airootfs/usr/local/lib/fws/i18n.sh new file mode 100644 index 0000000..ac85ebb --- /dev/null +++ b/configs/baseline/airootfs/usr/local/lib/fws/i18n.sh @@ -0,0 +1,40 @@ +# FWS — translations for the FWS shell tools (Bash, sourced). +# +# . /usr/local/lib/fws/i18n.sh ... +# say "$(t [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 +}