1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # 12_locale.zprofile: Initialize locale settings
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
8 # Make sure that LANG and LC_MESSAGES are either unset or set to something sane,
9 # that is, follow the "xx_ZZ.*" syntax.
16 if [[ -z "$1" || "$locale" =~ '.*_.*' || ${#locale%%_*} -ne 2 ]]; then
19 locale="${locale:0:2}_${locale:0:2:u}"
20 [[ -n "$encoding" ]] && locale="$locale.$encoding"
24 [[ -n "$LANG" ]] && LANG=$(fix_locale "$LANG")
25 [[ -n "$LC_MESSAGES" ]] && LANG=$(fix_locale "$LC_MESSAGES")
28 # Validate the locale(7) settings in interactive shells and try to mimic the
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/\"//
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
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!
48 echo "ax-zsh: unknown/unsupported locale ${lc_messages:-unknown}" >&2
50 while [[ -z "$locale" ]]; do
51 if ! read "locale?Locale? "; then
56 if [[ -n "$locale" ]]; then
58 unset LANGUAGE LC_ALL LC_MESSAGES
61 unset lc_messages locale encoding
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
69 export LANGUAGE=${LANG%.*}:${${LANG%.*}%_*}