From: Alexander Barton Date: Mon, 19 Jun 2017 21:18:26 +0000 (+0200) Subject: 12locale: Make sure that LANG and LC_MESSAGES are valid X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ax-zsh.git;a=commitdiff_plain;h=bf35f5d7d23999537b8096831a276c520c7fbe18 12locale: Make sure that LANG and LC_MESSAGES are valid Make sure that LANG and LC_MESSAGES are either unset or set to something sane, that is, follow the "xx_ZZ.*" syntax. --- diff --git a/core/12_locale/12_locale.zprofile b/core/12_locale/12_locale.zprofile index 0c9b554..5e7d880 100644 --- a/core/12_locale/12_locale.zprofile +++ b/core/12_locale/12_locale.zprofile @@ -5,6 +5,26 @@ # manually enter data when the validation fails): [[ -z "$PS1" ]] && return 0 +# Make sure that LANG and LC_MESSAGES are either unset or set to something sane, +# that is, follow the "xx_ZZ.*" syntax. +fix_locale() { + local encoding locale + + locale="$1:r" + encoding="$1:e" + + if [[ -z "$1" || "$locale" =~ '.*_.*' || ${#locale%%_*} -ne 2 ]]; then + echo "$1" + else + locale="${locale:0:2}_${locale:0:2:u}" + [[ -n "$encoding" ]] && locale="$locale.$encoding" + echo "$locale" + fi +} +[[ -n "$LANG" ]] && LANG=$(fix_locale "$LANG") +[[ -n "$LC_MESSAGES" ]] && LANG=$(fix_locale "$LC_MESSAGES") +unfunction fix_locale + # Validate the locale(7) settings in interactive shells and try to mimic the # tset(1) behaviour. while true; do