Change locales detection methodology and add failsafe mechanism (#2561)

In case they are not deteced for some reason
This commit is contained in:
Igor Pečovnik 2021-01-19 23:24:57 +01:00 committed by GitHub
parent d95a4f7bf6
commit 57604fa275
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -83,44 +83,50 @@ set_timezone_and_locales()
TZDATA=$(echo ${RES} | cut -d"," -f1)
STATE=$(echo ${RES} | cut -d"," -f2)
LOCALES=$(grep territory /usr/share/i18n/locales/* | grep "$STATE" | cut -d ":" -f 1 | cut -d "/" -f 6 | xargs -I{} grep {} /usr/share/i18n/SUPPORTED | grep "\.UTF-8" | cut -d " " -f 1)
CCODE=$(echo ${RES} | cut -d"," -f3 | xargs)
KEYBOARD="${CCODE,,}"
LOCALES=$(grep territory /usr/share/i18n/locales/* | grep "$CCODE" | cut -d ":" -f 1 | cut -d "/" -f 6 | xargs -I{} grep {} /usr/share/i18n/SUPPORTED | grep "\.UTF-8" | cut -d " " -f 1)
# UTF8 is not present everywhere so check again in case it returns empty value
[[ -z "$LOCALES" ]] && LOCALES=$(grep territory /usr/share/i18n/locales/* | grep "$STATE" | cut -d ":" -f 1 | cut -d "/" -f 6 | xargs -I{} grep {} /usr/share/i18n/SUPPORTED | cut -d " " -f 1)
CCODE=$(echo ${RES} | cut -d"," -f3 | awk '{print tolower($0)}' | xargs)
options=(`echo ${LOCALES}`);
[[ -z "$LOCALES" ]] && LOCALES=$(grep territory /usr/share/i18n/locales/* | grep "$CCODE" | cut -d ":" -f 1 | cut -d "/" -f 6 | xargs -I{} grep {} /usr/share/i18n/SUPPORTED | cut -d " " -f 1)
# reconfigure tzdata
timedatectl set-timezone "${TZDATA}"
dpkg-reconfigure --frontend=noninteractive tzdata > /dev/null 2>&1
# change it only if we have a match
if [[ -n "$LOCALES" ]]; then
echo -e "Detected timezone: \x1B[92m$(LC_ALL=C timedatectl | grep "Time zone" | cut -d":" -f2 | xargs)\x1B[0m"
# when having more locales, prompt for choosing one
if [[ "${#options[@]}" -gt 1 ]]; then
echo ""
echo -e "\nAt your location, more locales are possible:\n"
PS3='Please enter your choice:'
select opt in "${options[@]}"
do
if [[ " ${options[@]} " =~ " ${opt} " ]]; then
LOCALES=${opt}
break
fi
done
options=(`echo ${LOCALES}`);
# reconfigure tzdata
timedatectl set-timezone "${TZDATA}"
dpkg-reconfigure --frontend=noninteractive tzdata > /dev/null 2>&1
echo -e "Detected timezone: \x1B[92m$(LC_ALL=C timedatectl | grep "Time zone" | cut -d":" -f2 | xargs)\x1B[0m"
# when having more locales, prompt for choosing one
if [[ "${#options[@]}" -gt 1 ]]; then
echo ""
echo -e "\nAt your location, more locales are possible:\n"
PS3='Please enter your choice:'
select opt in "${options[@]}"
do
if [[ " ${options[@]} " =~ " ${opt} " ]]; then
LOCALES=${opt}
break
fi
done
fi
# generate locales
sed -i 's/# '"${LOCALES}"'/'"${LOCALES}"'/' /etc/locale.gen
echo -e "Generating locales: \x1B[92m${LOCALES}\x1B[0m"
locale-gen $LOCALES > /dev/null 2>&1
# adding another keyboard layout
if grep -q " $CCODE " /usr/share/X11/xkb/rules/base.lst ; then
echo -e "Adding console keyboard layout: \x1B[92m$CCODE\x1B[0m"
CCODE=$(cat /etc/default/keyboard | grep XKBLAYOUT | awk -F'"' '$0=$2')",$CCODE"
sed -i "s/XKBLAYOUT=.*/XKBLAYOUT=\"$KEYBOARD\"/" /etc/default/keyboard
setupcon -k --force
fi
fi
# generate locales
sed -i 's/# '"${LOCALES}"'/'"${LOCALES}"'/' /etc/locale.gen
echo -e "Generating locales: \x1B[92m${LOCALES}\x1B[0m"
locale-gen $LOCALES > /dev/null 2>&1
# adding another keyboard layout
if grep -q " $CCODE " /usr/share/X11/xkb/rules/base.lst ; then
echo -e "Adding console keyboard layout: \x1B[92m$CCODE\x1B[0m"
CCODE=$(cat /etc/default/keyboard | grep XKBLAYOUT | awk -F'"' '$0=$2')",$CCODE"
sed -i "s/XKBLAYOUT=.*/XKBLAYOUT=\"$CCODE\"/" /etc/default/keyboard
setupcon -k --force
fi
fi
}