]> arthur.barton.de Git - ax-zsh.git/blob - core/12_locale/12_locale.zprofile
e6256eeba610a72c05205a4092431900a54cc71f
[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=$lc_messages:gs/\"//
13         locale=$lc_messages:r
14         [[ "$OSTYPE" = 'linux-gnu' && $locale != 'C' ]] \
15                 && encoding=$lc_messages:e:l:gs/-// \
16                 || encoding=$lc_messages:e
17         [[ -n "$encoding" ]] && locale="$locale.$encoding"
18         [[ -z "$LANG$LANGUAGE$LC_ALL$LC_MESSAGES" ]] && unset lc_messages
19
20         if [[ -n "$LANG$LANGUAGE$LC_ALL$LC_MESSAGES" ]] && locale -a 2>/dev/null | grep "^$locale\$" >/dev/null; then
21                 # The locale setting seems to be valid: one of the LANG,
22                 # LANGUAGE, LC_ALL and/or LC_MESSAGES is set and the locale is
23                 # included in "locale -a" output. Good!
24                 break
25         fi
26
27         echo "ax-zsh: unknown/unsupported locale ${lc_messages:-unknown}" >&2
28         unset locale
29         while [[ -z "$locale" ]]; do
30                 if ! read "locale?Locale? "; then
31                         echo >&2
32                         break 2
33                 fi
34         done
35         if [[ -n "$locale" ]]; then
36                 export LANG=$locale
37                 unset LANGUAGE LC_ALL LC_MESSAGES
38         fi
39 done
40
41 unset lc_messages locale encoding