I18n locale
This commit is contained in:
+19
-1
@@ -318,7 +318,7 @@ function dump_stderr {
|
|||||||
local last_error="${tmp_error[-1]}"
|
local last_error="${tmp_error[-1]}"
|
||||||
if [[ "$last_error" =~ $MIAOU_ERROR_REGEX ]]; then
|
if [[ "$last_error" =~ $MIAOU_ERROR_REGEX ]]; then
|
||||||
tmp_error_count=$((tmp_error_count - 1))
|
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]'
|
unset 'tmp_error[-1]'
|
||||||
if [[ $tmp_error_count == 0 ]]; then
|
if [[ $tmp_error_count == 0 ]]; then
|
||||||
# IDEA: grab last_stdout and test no carriage_return, cf: ../test/stub/tee_last_line.bash
|
# 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 "$@"
|
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
|
## main
|
||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
@@ -685,6 +702,7 @@ else
|
|||||||
exec 4>/dev/tty
|
exec 4>/dev/tty
|
||||||
fi
|
fi
|
||||||
exec 3>&2 2>"$MIAOU_BASH_ERROR"
|
exec 3>&2 2>"$MIAOU_BASH_ERROR"
|
||||||
|
load_locale
|
||||||
miaou_debug "<main file=$BASH_ARGV0 pid=${BASHPID}/>"
|
miaou_debug "<main file=$BASH_ARGV0 pid=${BASHPID}/>"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
# shellcheck disable=SC2034
|
||||||
|
MSG_UNBOUND_VARIABLE_REGEX='^(.*): line ([0-9]+): (.*): unbound variable$'
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# shellcheck disable=SC2034
|
||||||
|
|
||||||
|
MSG_UNBOUND_VARIABLE_REGEX='^(.*): line ([0-9]+): (.*): unbound variable$'
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
# shellcheck disable=SC2034
|
||||||
|
MSG_UNBOUND_VARIABLE_REGEX='^(.*): line ([0-9]+): (.*): unbound variable$'
|
||||||
@@ -137,6 +137,47 @@ function fail_miaou_count {
|
|||||||
failures+=("$cmd 3>$TMP_MIAOU")
|
failures+=("$cmd 3>$TMP_MIAOU")
|
||||||
return 1
|
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
|
# main
|
||||||
|
|
||||||
set -uo pipefail
|
set -uo pipefail
|
||||||
@@ -181,6 +222,7 @@ time {
|
|||||||
fail_stderr_grep script_not_found "ERROR: script 'doesnt_exist' not found!"
|
fail_stderr_grep script_not_found "ERROR: script 'doesnt_exist' not found!"
|
||||||
fail_stderr_grep no_script 'ERROR: script expected!'
|
fail_stderr_grep no_script 'ERROR: script expected!'
|
||||||
|
|
||||||
|
assert_locales_equally_populated en es fr
|
||||||
# TODO: I18n tests for:
|
# TODO: I18n tests for:
|
||||||
# /opt/miaou-bash/test/stub/scenario.bash unbound_variable
|
# /opt/miaou-bash/test/stub/scenario.bash unbound_variable
|
||||||
# /opt/miaou-bash/test/stub/scenario.bash unbound_associative
|
# /opt/miaou-bash/test/stub/scenario.bash unbound_associative
|
||||||
|
|||||||
Reference in New Issue
Block a user