]> arthur.barton.de Git - ax-zsh.git/blob - core/12_locale/12_locale.zprofile
12_locale: Enhance matching of Linux systems
[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 # Make sure that LANG and LC_MESSAGES are either unset or set to something sane,
9 # that is, follow the "xx_ZZ.*" syntax.
10 fix_locale() {
11         local encoding locale
12
13         locale="$1:r"
14         encoding="$1:e"
15
16         if [[ -z "$1" || "$locale" =~ '.*_.*' || ${#locale%%_*} -ne 2 ]]; then
17                 echo "$1"
18         else
19                 locale="${locale:0:2}_${locale:0:2:u}"
20                 [[ -n "$encoding" ]] && locale="$locale.$encoding"
21                 echo "$locale"
22         fi
23 }
24 [[ -n "$LANG" ]] && LANG=$(fix_locale "$LANG")
25 [[ -n "$LC_MESSAGES" ]] && LANG=$(fix_locale "$LC_MESSAGES")
26 unfunction fix_locale
27
28 # Validate the locale(7) settings in interactive shells and try to mimic the
29 # tset(1) behaviour.
30 while true; do
31         lc_messages=$(locale 2>/dev/null | fgrep LC_MESSAGES | cut -d'=' -f2)
32         [[ "$lc_messages" = '"C"' && "$LANG" != 'C' && "$LC_ALL" != 'C' ]] && lc_messages=$LANG
33         lc_messages=$lc_messages:gs/\"//
34         locale=$lc_messages:r
35         [[ ( "$OSTYPE" =~ '^linux-gnu' || "$OSTYPE" = 'cygwin') && $locale != 'C' ]] \
36                 && encoding=$lc_messages:e:l:gs/-// \
37                 || encoding=$lc_messages:e
38         [[ -n "$encoding" ]] && locale="$locale.$encoding"
39         [[ -z "$LANG$LANGUAGE$LC_ALL$LC_MESSAGES" ]] && unset lc_messages
40
41         if [[ -n "$LANG$LANGUAGE$LC_ALL$LC_MESSAGES" ]] && locale -a 2>/dev/null | grep "^$locale\$" >/dev/null; then
42                 # The locale setting seems to be valid: one of the LANG,
43                 # LANGUAGE, LC_ALL and/or LC_MESSAGES is set and the locale is
44                 # included in "locale -a" output. Good!
45                 break
46         fi
47
48         echo "ax-zsh: unknown/unsupported locale ${lc_messages:-unknown}" >&2
49         unset locale
50         while [[ -z "$locale" ]]; do
51                 if ! read "locale?Locale? "; then
52                         echo >&2
53                         break 2
54                 fi
55         done
56         if [[ -n "$locale" ]]; then
57                 export LANG=$locale
58                 unset LANGUAGE LC_ALL LC_MESSAGES
59         fi
60 done
61 unset lc_messages locale encoding
62
63 case "$LANG" in
64         *_*)
65                 # LANG contains at least one "_" ("aa_BB" form).
66                 if [[ -z "$LANGUAGE" ]]; then
67                         # But LANGUAGE isn't set, so set it automatically to
68                         # "aa_BB:aa" form.
69                         export LANGUAGE=${LANG%.*}:${${LANG%.*}%_*}
70                 fi
71 esac