Blame Scripts/Bash/Functions/cli_getCurrentLocale.sh

4c79b5
#!/bin/bash
4c79b5
#
4c79b5
# cli_getCurrentLocale.sh -- This function checks LANG environment
4c79b5
# variable and returns the current locale from more specific to less
4c79b5
# specific. For example, if the locale 'en_GB' is the current one, it
4c79b5
# should be used instead of just 'en'.
4c79b5
#
72c8a5
# Copyright (C) 2009-2011  Alain Reguera Delgado
4c79b5
# 
4c79b5
# This program is free software; you can redistribute it and/or
4c79b5
# modify it under the terms of the GNU General Public License as
4c79b5
# published by the Free Software Foundation; either version 2 of the
4c79b5
# License, or (at your option) any later version.
4c79b5
# 
4c79b5
# This program is distributed in the hope that it will be useful, but
4c79b5
# WITHOUT ANY WARRANTY; without even the implied warranty of
4c79b5
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c79b5
# General Public License for more details.
4c79b5
#
4c79b5
# You should have received a copy of the GNU General Public License
4c79b5
# along with this program; if not, write to the Free Software
4c79b5
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
4c79b5
# USA.
4c79b5
# 
4c79b5
# ----------------------------------------------------------------------
418249
# $Id$
4c79b5
# ----------------------------------------------------------------------
4c79b5
4c79b5
function cli_getCurrentLocale {
4c79b5
4c79b5
    local -a PATTERNS
4c79b5
    local PATTERN=''
4c79b5
    local CURRENTLOCALE=''
4c79b5
4c79b5
    # Define pattern for current locale using LL_CC.ENCODING format.
4c79b5
    PATTERNS[0]=$LANG
4c79b5
4c79b5
    # Define pattern for current locale using LL_CC format.
4c79b5
    PATTERNS[1]=$(echo $LANG | sed -r 's!(^[a-z]{2,3}_[A-Z]{2}).+$!\1!')
4c79b5
4c79b5
    # Define pattern for current locale using LL format.
4c79b5
    PATTERNS[2]=$(echo $LANG | sed -r 's!^([a-z]{2,3}).+$!\1!')
4c79b5
4c79b5
    # Define which system locale to use as centos-art.sh script
4c79b5
    # current locale. Take care with pattern order, it is relevant for
4c79b5
    # this function to work as expected.
4c79b5
    for PATTERN in "${PATTERNS[@]}";do
4c79b5
        CURRENTLOCALE=$(cli_getLocales | egrep $PATTERN)
4c79b5
        if [[ $CURRENTLOCALE != '' ]];then
4c79b5
            break
4c79b5
        fi
4c79b5
    done
4c79b5
4c79b5
    # Define centos-art.sh script default current locale. If
4c79b5
    # centos-art.sh script doesn't support current system locale, use
4c79b5
    # English (en) as default current locale.
4c79b5
    if [[ $CURRENTLOCALE == '' ]];then
4c79b5
        CURRENTLOCALE='en'
4c79b5
    fi
4c79b5
4c79b5
    # Output current locale. Be sure that just one value is output.
4c79b5
    echo $CURRENTLOCALE | sort | uniq | head -n1
4c79b5
4c79b5
}