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.
74 lines
2.5 KiB
74 lines
2.5 KiB
#!/bin/sh |
|
# ensure-locale.sh |
|
# Ensures a UTF-8 locale is available and active. |
|
# Works safely on hosts and pure/minimal Docker containers (Alpine, Debian, Ubuntu, RHEL). |
|
|
|
ensure_utf8_locale() { |
|
# 1. Prefer C.UTF-8 if available (standard in modern containers, avoids generation overhead) |
|
if command -v locale >/dev/null 2>&1 && locale -a 2>/dev/null | grep -qi '^C\.utf'; then |
|
export LANG=C.UTF-8 |
|
export LC_ALL=C.UTF-8 |
|
return 0 |
|
fi |
|
|
|
# 2. Check if current active charmap is ALREADY UTF-8 |
|
if command -v locale >/dev/null 2>&1; then |
|
if[ "$(locale charmap 2>/dev/null)" = "UTF-8" ]; then |
|
# Already UTF-8; ensure variables are cleanly exported for child processes |
|
export LANG="${LANG:-C.UTF-8}" |
|
export LC_ALL="${LC_ALL:-$LANG}" |
|
return 0 |
|
fi |
|
fi |
|
|
|
# 3. Check for an existing en_US.UTF-8 |
|
if command -v locale >/dev/null 2>&1 && locale -a 2>/dev/null | grep -qi '^en_US\.utf'; then |
|
export LANG=en_US.UTF-8 |
|
export LC_ALL=en_US.UTF-8 |
|
return 0 |
|
fi |
|
|
|
# 4. Try to generate en_US.UTF-8 (Debian/Ubuntu/Arch) |
|
if command -v locale-gen >/dev/null 2>&1; then |
|
echo "[ensure-locale] Generating en_US.UTF-8 locale..." >&2 |
|
|
|
# Debian requires uncommenting the locale in /etc/locale.gen first |
|
if[ -w /etc/locale.gen ]; then |
|
sed -i 's/^# *\(en_US.UTF-8 UTF-8\)/\1/' /etc/locale.gen 2>/dev/null || true |
|
fi |
|
|
|
locale-gen en_US.UTF-8 >/dev/null 2>&1 || true |
|
|
|
if locale -a 2>/dev/null | grep -qi '^en_US\.utf'; then |
|
export LANG=en_US.UTF-8 |
|
export LC_ALL=en_US.UTF-8 |
|
|
|
# Update system defaults if possible |
|
if command -v update-locale >/dev/null 2>&1; then |
|
update-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 2>/dev/null || true |
|
fi |
|
return 0 |
|
fi |
|
fi |
|
|
|
# 5. Fallback for pure Alpine / minimal musl containers without 'locale' installed |
|
export LANG=C.UTF-8 |
|
export LC_ALL=C.UTF-8 |
|
} |
|
|
|
# Run the function |
|
ensure_utf8_locale |
|
|
|
# Persist the environment variables for future shell sessions (if running as root) |
|
if[ "$(id -u 2>/dev/null || echo 1)" = "0" ]; then |
|
if [ -d /etc/profile.d ] &&[ -w /etc/profile.d ]; then |
|
echo "export LANG=\"$LANG\"" > /etc/profile.d/00-locale.sh |
|
echo "export LC_ALL=\"$LC_ALL\"" >> /etc/profile.d/00-locale.sh |
|
fi |
|
fi |
|
|
|
# Allow script to be used as a Docker ENTRYPOINT wrapper |
|
# e.g., ./ensure-locale.sh python app.py |
|
if[ $# -gt 0 ]; then |
|
exec "$@" |
|
fi
|
|
|