locale_setup.sh 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#!/bin/bash
#
# Copyright (c) 2013 - Manuel Tortosa <manutortosa@chakra-project.org>
#
# This script is released under the LGPL2+ 


# $1: the parameter whose value you want to get
# returns: the value of the parameter, if existant
get_bootparam_value() {
  [ -z "$CMDLINE" ] && CMDLINE="$(< /proc/cmdline)"
  case "$CMDLINE" in *\ $1=*) ;; *) return 1; ;; esac
  local result="${CMDLINE##*$1=}"
  echo ${result%%[ ]*}
}

# returns: the country of the user, if set as kernel parameter
get_country() {
  local COUNTRY=$(get_bootparam_value lang)
  echo $COUNTRY
}

get_keyboard() {
  local KEYBOARD=$(get_bootparam_value keymap)
  echo $KEYBOARD
}

get_layout {
  local LAYOUT=$(get_bootparam_value layout)
  echo $LAYOUT
}

# sets the locale as well the keymap
set_locale() {
  # hack to be able to set the locale on bootup
  local LOCALE=$(get_country)
  local KEYMAP=$(get_keyboard)
  local KBLAYOUT=$(get_layout)
		
  # set a default value, in case something goes wrong, or a language doesn't have
  # good defult settings
  [ -n "$LOCALE" ] || LOCALE="en_US"
  [ -n "$KEYMAP" ] || KEYMAP="us"
  [ -n "$kBLAYOUT" ] || KBLAYOUT="us"

  # comment out all locales which we don't need
  sed  -i "s/^/#/g" /etc/locale.gen

  # copy the keyboard.conf file to it's place
  cp -f /etc/skel/10-keyboard.conf /etc/X11/xorg.conf.d/10-keyboard.conf
  sed -i "/XkbLayout/ s/us/"${KBLAYOUT}"/" /etc/X11/xorg.conf.d/10-keyboard.conf

  # set systemwide language
  echo "LANG=${LOCALE}.UTF-8" > /etc/locale.conf
  echo "LC_MESSAGES=${LOCALE}.UTF-8" >> /etc/locale.conf

  # generate LOCALE
  local TLANG=${LOCALE%.*} # remove everything after the ., including the dot from LOCALE
  sed -i -r "s/#(.*${TLANG}.*UTF-8)/\1/g" /etc/locale.gen
  # add also American English as safe default
  sed -i -r "s/#(en_US.*UTF-8)/\1/g" /etc/locale.gen
  locale-gen
}