From 67d0c20e77ba5ebe507bfe49a16f11d10a2712b9 Mon Sep 17 00:00:00 2001 From: chodak166 Date: Sun, 19 Apr 2026 16:12:51 +0200 Subject: [PATCH] updated ensure-locale.sh --- .dotfiles.d/init/ensure-locale.sh | 100 ++++++++++++++++++++---------- .dotfiles.d/init/init-shell.sh | 2 +- 2 files changed, 67 insertions(+), 35 deletions(-) diff --git a/.dotfiles.d/init/ensure-locale.sh b/.dotfiles.d/init/ensure-locale.sh index 13218a4..b5cdada 100644 --- a/.dotfiles.d/init/ensure-locale.sh +++ b/.dotfiles.d/init/ensure-locale.sh @@ -1,42 +1,74 @@ -#!/bin/env bash +#!/bin/sh # ensure-locale.sh -# Ensures a UTF-8 locale is available and exported (works safely on hosts and in containers). +# 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() { - # 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 + # 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 - 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 +# 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 diff --git a/.dotfiles.d/init/init-shell.sh b/.dotfiles.d/init/init-shell.sh index 978acf5..5b8b35a 100644 --- a/.dotfiles.d/init/init-shell.sh +++ b/.dotfiles.d/init/init-shell.sh @@ -16,6 +16,6 @@ mkdir -p ~/.local/share/fonts cd ~/.local/share/fonts fontName=DejaVuSansMono -curl -L -O 'https://github.com/ryanoasis/nerd-fonts/releases/download/v3.4.0/${fontName}.zip' +curl -L -O "https://github.com/ryanoasis/nerd-fonts/releases/download/v3.4.0/${fontName}.zip" unzip ${fontName}.zip rm ${fontName}.zip