diff --git a/bin/miaou-bash b/bin/miaou-bash index 59ec7e2..c5fc94c 100755 --- a/bin/miaou-bash +++ b/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 "" + miaou_debug "" 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 "" + else + # fallback C/POSIX => en.sh + MIAOU_LOCALE=en + miaou_debug "" + 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 "
" fi diff --git a/locales/en.sh b/locales/en.sh new file mode 100644 index 0000000..f242758 --- /dev/null +++ b/locales/en.sh @@ -0,0 +1,2 @@ +# shellcheck disable=SC2034 +MSG_UNBOUND_VARIABLE_REGEX='^(.*): line ([0-9]+): (.*): unbound variable$' diff --git a/locales/es.sh b/locales/es.sh new file mode 100644 index 0000000..b3e7e03 --- /dev/null +++ b/locales/es.sh @@ -0,0 +1,3 @@ +# shellcheck disable=SC2034 + +MSG_UNBOUND_VARIABLE_REGEX='^(.*): line ([0-9]+): (.*): unbound variable$' diff --git a/locales/fr.sh b/locales/fr.sh new file mode 100644 index 0000000..f242758 --- /dev/null +++ b/locales/fr.sh @@ -0,0 +1,2 @@ +# shellcheck disable=SC2034 +MSG_UNBOUND_VARIABLE_REGEX='^(.*): line ([0-9]+): (.*): unbound variable$' diff --git a/test/miaou-bash.test.bash b/test/miaou-bash.test.bash index 58d981e..11d62cb 100755 --- a/test/miaou-bash.test.bash +++ b/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