diff --git a/bin/omarchy-language b/bin/omarchy-language index d39688b9..1e2d3306 100755 --- a/bin/omarchy-language +++ b/bin/omarchy-language @@ -10,7 +10,10 @@ set -euo pipefail locale_conf=/etc/locale.conf if (($# == 0)); then - sed -n 's/^LANG=//p' "$locale_conf" + lang=$(sed -n 's/^LANG=//p' "$locale_conf") + # locale.conf may quote the value. + lang=${lang#[\"\']} + printf '%s\n' "${lang%[\"\']}" exit 0 fi @@ -28,6 +31,7 @@ sudo locale-gen if grep -q '^LANG=' "$locale_conf"; then sudo sed -i "s/^LANG=.*/LANG=$lang/" "$locale_conf" else + sudo sed -i '$a\' "$locale_conf" echo "LANG=$lang" | sudo tee -a "$locale_conf" >/dev/null fi diff --git a/test/shell.d/language-test.sh b/test/shell.d/language-test.sh index 6d1e61d6..7dbed49e 100755 --- a/test/shell.d/language-test.sh +++ b/test/shell.d/language-test.sh @@ -41,10 +41,21 @@ output=$(omarchy-language en) [[ $output == "Log back in to switch the language." ]] || fail "English reminds in English" "$output" pass "switching to English generates en_US and sets LANG" -printf 'LC_TIME=en_DK.UTF-8\n' >"$root/etc/locale.conf" +printf 'LC_TIME=en_DK.UTF-8' >"$root/etc/locale.conf" omarchy-language zh >/dev/null -[[ $(<"$root/etc/locale.conf") == $'LC_TIME=en_DK.UTF-8\nLANG=zh_CN.UTF-8' ]] || fail "a locale.conf without LANG gains one" "$(<"$root/etc/locale.conf")" -pass "a locale.conf without LANG gains one" +[[ $(<"$root/etc/locale.conf") == $'LC_TIME=en_DK.UTF-8\nLANG=zh_CN.UTF-8' ]] || fail "a locale.conf without LANG or a final newline gains LANG on its own line" "$(<"$root/etc/locale.conf")" +: >"$root/etc/locale.conf" +omarchy-language en >/dev/null +[[ $(<"$root/etc/locale.conf") == "LANG=en_US.UTF-8" ]] || fail "an empty locale.conf gains LANG" "$(<"$root/etc/locale.conf")" +pass "a locale.conf without LANG gains one on its own line" + +printf 'LANG="zh_CN.UTF-8"\n' >"$root/etc/locale.conf" +[[ $(omarchy-language) == "zh_CN.UTF-8" ]] || fail "a double-quoted LANG reads unquoted" "$(omarchy-language)" +printf "LANG='en_US.UTF-8'\n" >"$root/etc/locale.conf" +[[ $(omarchy-language) == "en_US.UTF-8" ]] || fail "a single-quoted LANG reads unquoted" "$(omarchy-language)" +printf 'LC_TIME=en_DK.UTF-8\n' >"$root/etc/locale.conf" +[[ -z $(LANG=de_DE.UTF-8 omarchy-language) ]] || fail "a locale.conf without LANG reads empty, not the caller's LANG" +pass "omarchy-language reads quoted values and ignores the caller's LANG" before=$(<"$root/etc/locale.conf") if output=$(omarchy-language fr 2>&1); then