]> arthur.barton.de Git - ax-zsh.git/blob - core/12_locale/12_locale.zprofile
12_locale: Detect and revert auto-correction of LC_* variables
[ax-zsh.git] / core / 12_locale / 12_locale.zprofile
1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # 12_locale.zprofile: Initialize locale settings
3
4 # This is only relevant for interactive shells (because the user has to
5 # manually enter data when the validation fails):
6 [[ -z "$PS1" ]] && return 0
7
8 # Validate the locale(7) settings in interactive shells and try to mimic the
9 # tset(1) behaviour.
10 while true; do
11         lc_messages=$(locale 2>/dev/null | fgrep LC_MESSAGES | cut -d'=' -f2)
12         [[ "$lc_messages" = '"C"' && "$LANG" != 'C' && "$LC_ALL" != 'C' ]] && lc_messages=$LANG
13         lc_messages=$lc_messages:gs/\"//
14         locale=$lc_messages:r
15         [[ "$OSTYPE" = 'linux-gnu' && $locale != 'C' ]] \
16                 && encoding=$lc_messages:e:l:gs/-// \
17                 || encoding=$lc_messages:e
18         [[ -n "$encoding" ]] && locale="$locale.$encoding"
19         [[ -z "$LANG$LANGUAGE$LC_ALL$LC_MESSAGES" ]] && unset lc_messages
20
21         if [[ -n "$LANG$LANGUAGE$LC_ALL$LC_MESSAGES" ]] && locale -a 2>/dev/null | grep "^$locale\$" >/dev/null; then
22                 # The locale setting seems to be valid: one of the LANG,
23                 # LANGUAGE, LC_ALL and/or LC_MESSAGES is set and the locale is
24                 # included in "locale -a" output. Good!
25                 break
26         fi
27
28         echo "ax-zsh: unknown/unsupported locale ${lc_messages:-unknown}" >&2
29         unset locale
30         while [[ -z "$locale" ]]; do
31                 if ! read "locale?Locale? "; then
32                         echo >&2
33                         break 2
34                 fi
35         done
36         if [[ -n "$locale" ]]; then
37                 export LANG=$locale
38                 unset LANGUAGE LC_ALL LC_MESSAGES
39         fi
40 done
41
42 unset lc_messages locale encoding