Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions core/tabs/utils/locale-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/bin/sh -e

. ../common-script.sh
. ../common-service-script.sh
Comment thread
technicks89 marked this conversation as resolved.
Outdated

get_suggested_locale() {
key="$1"
default_locale="en_US.UTF-8"

# Use case statement to map keys to values (POSIX standard)
case "$key" in
us) printf "%b" "en_US.UTF-8" ;;
ca) printf "%b" "en_CA.UTF-8" ;;
cn) printf "%b" "zh_CN.UTF-8" ;;
by) printf "%b" "be_BY.UTF-8" ;;
ch) printf "%b" "de_CH.UTF-8" ;;
cz) printf "%b" "cs_CZ.UTF-8" ;;
de) printf "%b" "de_DE.UTF-8" ;;
dk) printf "%b" "da_DK.UTF-8" ;;
es) printf "%b" "es_ES.UTF-8" ;;
et) printf "%b" "et_EE.UTF-8" ;;
fa) printf "%b" "fa_IR.UTF-8" ;;
Comment thread
technicks89 marked this conversation as resolved.
Outdated
fi) printf "%b" "fi_FI.UTF-8" ;;
fr) printf "%b" "fr_FR.UTF-8" ;;
gb) printf "%b" "en_GB.UTF-8" ;;
gr) printf "%b" "el_GR.UTF-8" ;;
hu) printf "%b" "hu_HU.UTF-8" ;;
il) printf "%b" "he_IL.UTF-8" ;;
it) printf "%b" "it_IT.UTF-8" ;;
jp) printf "%b" "ja_JP.UTF-8" ;;
kr) printf "%b" "ko_KR.UTF-8" ;;
lt) printf "%b" "lt_LT.UTF-8" ;;
lv) printf "%b" "lv_LV.UTF-8" ;;
mk) printf "%b" "mk_MK.UTF-8" ;;
nl) printf "%b" "nl_NL.UTF-8" ;;
no) printf "%b" "nb_NO.UTF-8" ;;
ph) printf "%b" "en_PH.UTF-8" ;;
pl) printf "%b" "pl_PL.UTF-8" ;;
ro) printf "%b" "ro_RO.UTF-8" ;;
ru) printf "%b" "ru_RU.UTF-8" ;;
se) printf "%b" "sv_SE.UTF-8" ;;
sg) printf "%b" "en_SG.UTF-8" ;;
si) printf "%b" "sl_SI.UTF-8" ;;
tr) printf "%b" "tr_TR.UTF-8" ;;
ua) printf "%b" "uk_UA.UTF-8" ;;
*) printf "%b" "$default_locale" ;; # Default fallback
esac
}

setLocale() {
if command_exists locale-gen; then

iso=$(curl -4fsSL --max-time 5 https://ifconfig.io/country_code 2>/dev/null | tr '[:upper:]' '[:lower:]') || iso="us"
suggested_locale=$(get_suggested_locale "${iso}")

while true; do
printf "Detected locale: '%s'. Press Enter to accept or type a new one (e.g. de_DE.UTF-8): " "$suggested_locale"
read -r input
LOCALE="${input:-$suggested_locale}"

case "$LOCALE" in
*[![:alnum:]_.@-]*|'') ;;
*.UTF-8) break ;;
esac

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate the locale before persisting it

For any syntactically plausible but unsupported input such as en_UK.UTF-8, this check accepts the value, after which the script writes it to /etc/environment, /etc/locale.gen, and /etc/locale.conf before locale-gen reports failure. The failed run therefore leaves the system configured with an invalid locale; verify the value against the installed locale definitions before modifying any files.

Useful? React with 👍 / 👎.


# If we reach here, the validation failed
printf "ERROR! Locale '%s' does not look valid. Please enter a locale like en_US.UTF-8." "${LOCALE}"
done

if grep -q '^LC_ALL=' /etc/environment 2>/dev/null; then
"$ESCALATION_TOOL" sed -i "s/^LC_ALL=.*/LC_ALL=${LOCALE}/" /etc/environment
else
printf 'LC_ALL=%s\n' "$LOCALE" | "$ESCALATION_TOOL" tee -a /etc/environment >/dev/null
Comment thread
technicks89 marked this conversation as resolved.
Outdated
fi
if ! grep -qxF "${LOCALE} UTF-8" /etc/locale.gen 2>/dev/null; then
printf '%s UTF-8\n' "$LOCALE" | "$ESCALATION_TOOL" tee -a /etc/locale.gen >/dev/null
fi
if grep -q '^LANG=' /etc/locale.conf 2>/dev/null; then
"$ESCALATION_TOOL" sed -i "s/^LANG=.*/LANG=${LOCALE}/" /etc/locale.conf
else
printf 'LANG=%s\n' "$LOCALE" | "$ESCALATION_TOOL" tee -a /etc/locale.conf >/dev/null
fi
"$ESCALATION_TOOL" locale-gen "${LOCALE}"
else
printf "%b\n" "ERROR! locale-gen not found; cannot generate locales on this system."
exit 1
fi
Comment thread
technicks89 marked this conversation as resolved.
}

checkEnv
Comment thread
technicks89 marked this conversation as resolved.
setLocale
11 changes: 11 additions & 0 deletions core/tabs/utils/tab_data.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ description = "This utility is designed to enable Num Lock at boot, rather than
script = "numlock.sh"
task_list = "PFM SS"

[[data]]
name = "Locale Setup"
Comment thread
technicks89 marked this conversation as resolved.
description = "This allows the user to set their locale"
script = "locale-setup.sh"
task_list = "PFM"
Comment thread
technicks89 marked this conversation as resolved.
Outdated
Comment thread
technicks89 marked this conversation as resolved.
Outdated

[[data.preconditions]]
matches = true
data = "command_exists"
values = [ "locale-gen" ]

[[data]]
name = "Ollama"
description = "This utility is designed to manage ollama in your system"
Expand Down
1 change: 1 addition & 0 deletions docs/content/userguide/walkthrough.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ https://github.com/ChrisTitusTech/dwm-titus

- **Bluetooth Manager**: This utility is designed to manage bluetooth in your system
- **Numlock on Startup**: This utility is designed to enable Num Lock at boot, rather than within desktop environments like KDE or GNOME
- **Locale Setup**: This allows the user to set their locale
- **Ollama**: This utility is designed to manage ollama in your system
- **Ranalama**: This utility is designed to manage ramalama in your system
- **Service Manager**: This utility is designed to manage services in your system
Expand Down