Browse Source

I18n locale

main
pvincent 1 week ago
parent
commit
639b7d967e
  1. 20
      bin/miaou-bash
  2. 2
      locales/en.sh
  3. 3
      locales/es.sh
  4. 2
      locales/fr.sh
  5. 42
      test/miaou-bash.test.bash

20
bin/miaou-bash

@ -318,7 +318,7 @@ function dump_stderr {
local last_error="${tmp_error[-1]}"
if [[ "$last_error" =~ $MIAOU_ERROR_REGEX ]]; then
tmp_error_count=$((tmp_error_count - 1))
miaou_debug "<hide_latest_miaou_error line=${last_error} count=$tmp_error_count/>"
miaou_debug "<hide_latest_miaou_error count=$tmp_error_count line={${last_error}}/>"
unset 'tmp_error[-1]'
if [[ $tmp_error_count == 0 ]]; then
# IDEA: grab last_stdout and test no carriage_return, cf: ../test/stub/tee_last_line.bash
@ -653,6 +653,23 @@ function read {
2>&1 builtin read "$@"
}
function load_locale {
MIAOU_LOCALE="${LANG%%_*}" # Get 'fr' from 'fr_FR.UTF-8'
if [[ $MIAOU_LOCALE =~ en|es|fr ]]; then
miaou_debug "<load_locale language=$MIAOU_LOCALE LANG=$LANG/>"
else
# fallback C/POSIX => en.sh
MIAOU_LOCALE=en
miaou_debug "<load_locale fallback=$MIAOU_LOCALE LANG=$LANG convert_to=C/>"
LANG=C
export LANG
fi
local locale_file
locale_file="${MIAOU_BASH_DIR}/locales/${MIAOU_LOCALE}.sh"
# shellcheck source=/dev/null
source "$locale_file"
}
## main
set -euo pipefail
@ -685,6 +702,7 @@ else
exec 4>/dev/tty
fi
exec 3>&2 2>"$MIAOU_BASH_ERROR"
load_locale
miaou_debug "<main file=$BASH_ARGV0 pid=${BASHPID}/>"
fi

2
locales/en.sh

@ -0,0 +1,2 @@
# shellcheck disable=SC2034
MSG_UNBOUND_VARIABLE_REGEX='^(.*): line ([0-9]+): (.*): unbound variable$'

3
locales/es.sh

@ -0,0 +1,3 @@
# shellcheck disable=SC2034
MSG_UNBOUND_VARIABLE_REGEX='^(.*): line ([0-9]+): (.*): unbound variable$'

2
locales/fr.sh

@ -0,0 +1,2 @@
# shellcheck disable=SC2034
MSG_UNBOUND_VARIABLE_REGEX='^(.*): line ([0-9]+): (.*): unbound variable$'

42
test/miaou-bash.test.bash

@ -137,6 +137,47 @@ function fail_miaou_count {
failures+=("$cmd 3>$TMP_MIAOU")
return 1
}
function assert_locales_equally_populated {
((test_count++))
local locales_dir="$MIAOU_BASH_DIR"/locales
local main_locale_file="$locales_dir"/"$1".sh
if [[ ! -f $main_locale_file ]]; then
echo -n F
failures+=("missing locale file: $main_locale_file for locale: $1")
return 1
fi
shift
if [[ $# -gt 0 ]]; then
local extra_locale_file difference variable_name
for extra in "$@"; do
extra_locale_file="$locales_dir"/"$extra".sh
if [[ ! -f $extra_locale_file ]]; then
echo -n F
failures+=("missing locale file: $extra_locale_file for locale: $extra")
return 1
fi
mapfile -t difference < <(
diff <(grep -oP '^\s*[A-Za-z_][A-Za-z0-9_]*' "$main_locale_file" | sort) \
<(grep -oP '^\s*[A-Za-z_][A-Za-z0-9_]*' "$extra_locale_file" | sort) \
2>&1 | head -n2
)
if [[ ${#difference[@]} -gt 0 ]]; then
variable_name=$(echo "${difference[1]}" | cut -d' ' -f2)
change_type="${difference[0]}"
echo -n F
if [[ "$change_type" =~ d ]]; then
failures+=("locale=$extra (in $extra_locale_file) misses variable (to add): ${variable_name}")
else
failures+=("locale=$extra (in $extra_locale_file) useless variable (to remove): ${variable_name}")
fi
return 1
fi
done
fi
echo -n .
}
# main
set -uo pipefail
@ -181,6 +222,7 @@ time {
fail_stderr_grep script_not_found "ERROR: script 'doesnt_exist' not found!"
fail_stderr_grep no_script 'ERROR: script expected!'
assert_locales_equally_populated en es fr
# TODO: I18n tests for:
# /opt/miaou-bash/test/stub/scenario.bash unbound_variable
# /opt/miaou-bash/test/stub/scenario.bash unbound_associative

Loading…
Cancel
Save