You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.2 KiB
42 lines
1.2 KiB
#!/bin/env bash |
|
# ensure-locale.sh |
|
# Ensures a UTF-8 locale is available and exported (works safely on hosts and in containers). |
|
|
|
ensure_utf8_locale() { |
|
# If already UTF-8, nothing to do |
|
if locale | grep -qi 'utf-8'; then |
|
return 0 |
|
fi |
|
|
|
# If en_US.UTF-8 (or equivalent) exists, just use it |
|
if locale -a 2>/dev/null | grep -qi 'en_US\.utf8'; then |
|
export LANG=en_US.UTF-8 |
|
export LC_ALL=en_US.UTF-8 |
|
return 0 |
|
fi |
|
|
|
# Try to generate one if possible (Debian/Ubuntu) |
|
if command -v locale-gen >/dev/null 2>&1; then |
|
echo "[ensure-locale] Generating en_US.UTF-8 locale..." |
|
locale-gen en_US.UTF-8 2>/dev/null || true |
|
# If update-locale exists, register it |
|
command -v update-locale >/dev/null 2>&1 && update-locale LANG=en_US.UTF-8 || true |
|
export LANG=en_US.UTF-8 |
|
export LC_ALL=en_US.UTF-8 |
|
return 0 |
|
fi |
|
|
|
# Alpine / musl–based systems: locale-gen doesn't exist |
|
if [ -f /usr/lib/locale/locale-archive ] || grep -q musl /proc/self/maps 2>/dev/null; then |
|
export LANG=C.UTF-8 |
|
export LC_ALL=C.UTF-8 |
|
return 0 |
|
fi |
|
|
|
# Fallback if nothing else works |
|
export LANG=C.UTF-8 |
|
export LC_ALL=C.UTF-8 |
|
} |
|
|
|
# Call it immediately if script is sourced/run directly |
|
ensure_utf8_locale
|
|
|