slitaz-tools annotate tinyutils/tazlocale @ rev 961

tazlocale: accept only existing locales, show locale description; translations: un-fuzzy already translated messages
author Aleksej Bobylev <al.bobylev@gmail.com>
date Wed Nov 25 23:50:30 2015 +0200 (2015-11-25)
parents 3be081525506
children bb7be0ca546f
rev   line source
pankso@5 1 #!/bin/sh
pankso@5 2 #
pankso@5 3 # Tazlocale: SliTaz GNU/Linux locale setting using dialog boxes.
pankso@752 4 # Configuration file is: /etc/locale.conf
pankso@5 5 #
al@939 6 # Copyright (C) 2008-2015 SliTaz GNU/Linux - BSD License
pankso@5 7 #
pankso@749 8 # Author: Christophe Lincoln <pankso@slitaz.org>
pankso@749 9 #
al@939 10
pankso@749 11 . /lib/libtaz.sh
al@813 12 export TEXTDOMAIN='slitaz-tools' #i18n
pankso@5 13
al@939 14
al@813 15 usage() {
al@813 16 newline
al@813 17 _ 'SliTaz GNU/Linux locale setting using dialog boxes.'
al@813 18
al@813 19 newline; boldify "$(_ 'Usage:')"
al@813 20 echo " tazlocale [$(_ 'option')]"
al@813 21
al@813 22 newline; boldify "$(_ 'Options:')"
al@813 23 optlist "\
al@813 24 info $(_ 'Show info about config file and current locale.')
al@813 25 list $(_ 'Show list of available locales.')"
al@813 26
al@813 27 newline
al@813 28 _ 'Any other option treated as locale - set locale (root).'
al@813 29 _ 'Display locale selecting dialog if no option given (root).'
al@813 30 newline
al@813 31 }
al@813 32
al@939 33
al@813 34 # Make symlink to file, substitute "%%" to "ll_CC", "ll" or "en" according to
paul@835 35 # current language settings and file existence
al@813 36 # (where "ll_CC" - full locale format (lang and country, and maybe, modifier).
al@939 37
al@813 38 make_i18n_link() {
al@813 39 if [ -d $(dirname ${1/.%%/}) ]; then
al@813 40 cd $(dirname ${1/.%%/})
al@813 41
al@813 42 if [ -e ${1/%%/$LANG} ]; then
al@813 43 ln -fs $(basename ${1/%%/$LANG}) ${1/.%%/}
al@813 44 else
al@813 45 if [ -e ${1/%%/$LANGUAGE} ]; then
al@813 46 ln -fs $(basename ${1/%%/$LANGUAGE}) ${1/.%%/}
al@813 47 else
al@813 48 ln -fs $(basename ${1/%%/en}) ${1/.%%/}
al@813 49 fi
al@813 50 fi
al@813 51 fi
al@813 52 }
pankso@622 53
al@939 54
paul@725 55 # Create symlink to translated files provided by SliTaz language pack,
pankso@338 56 # doc and config files.
al@939 57
al@813 58 link_language_files() {
pankso@338 59 . /etc/locale.conf
pankso@338 60 LANGUAGE=${LANG%_*}
al@943 61 [ "$LANG" == 'POSIX' ] && LANGUAGE='en'
al@939 62
al@813 63 # Openbox menu
al@813 64 make_i18n_link /etc/xdg/openbox/menu.%%.xml
al@939 65
al@813 66 # Documentation
al@813 67 make_i18n_link /usr/share/doc/slitaz/index.%%.html
al@939 68
pankso@338 69 # SliTaz Software Manuals
al@813 70 for soft in tazpkg tazlito tazusb tazwok tazweb cookutils; do
al@813 71 make_i18n_link /usr/share/doc/$soft/$soft.%%.html
pankso@338 72 done
al@939 73
al@716 74 # SliTaz TazWeb "My Web Home"
al@813 75 make_i18n_link /usr/share/tazweb/home.%%.html
al@939 76
al@728 77 # SliTaz WebHome
al@813 78 make_i18n_link /usr/share/webhome/index.%%.html
al@939 79
al@813 80 # TazPanel Doc under www
al@813 81 make_i18n_link /var/www/tazpanel/doc/tazpanel.%%.html
al@939 82
pankso@622 83 # SliTaz Tools Manuals
al@813 84 for soft in burnbox tazinst; do
al@813 85 make_i18n_link /usr/share/doc/slitaz-tools/$soft.%%.html
pankso@622 86 done
pankso@338 87 }
pankso@338 88
al@939 89
pankso@170 90 # Locale name displayed.
al@939 91
al@813 92 get_locale_name() {
al@813 93 for i in $(ls -1 /usr/share/i18n/locales | grep [a-z]_[A-Z]); do
al@961 94 echo -n "$i "
al@961 95 desc=$(grep ^title /usr/share/i18n/locales/$i | cut -d '"' -f 2)
al@961 96 if [ -n "$desc" ]; then
al@961 97 echo "$desc" | tr -c '[A-Za-z0-9\n]' '_'
al@961 98 else
al@961 99 echo "Locale"
al@961 100 fi
pankso@170 101 done
pankso@170 102 }
pankso@170 103
al@939 104
al@813 105 # We have no locale files in /usr/lib/locale by default.
al@813 106 # Run localedef in background to have a faster boot.
al@939 107
al@813 108 gen_utf8_locale() {
al@961 109 if [ ! -d "/usr/lib/locale/$locale.UTF-8" ]; then
al@961 110 localedef -i "$locale" -c -f 'UTF-8' "/usr/lib/locale/$locale.UTF-8" &
al@961 111 fi
pankso@426 112 }
pankso@426 113
al@939 114
pankso@749 115 # Config /etc/locale.conf
al@939 116
pankso@749 117 system_config() {
al@961 118 # If locale not defined: show error and exit
al@961 119 ls "/usr/share/i18n/locales/$locale" >/dev/null || return
al@961 120 export LC_ALL="$locale.UTF-8"
al@961 121 action 'Setting system locale to: %s' "$locale.UTF-8"
al@961 122 echo -e "LANG=$locale.UTF-8\nLC_ALL=$locale.UTF-8" > /etc/locale.conf
al@813 123 status
pankso@749 124 gen_utf8_locale
pankso@749 125 link_language_files
al@961 126 # Clean TazPanel cached headers in old language
al@961 127 [ -n "$(which tazpanel)" ] && tazpanel cc
pankso@749 128 }
pankso@749 129
al@939 130
pankso@5 131 # Dialog menu.
al@939 132
al@813 133 dialog_menu() {
pankso@338 134 exec 3>&1
pankso@749 135 locale=$($DIALOG --clear \
pankso@837 136 --title "{ $(_n 'SliTaz language setting') }" \
pankso@837 137 --menu "" 20 72 14 \
pankso@170 138 "en" "English" \
pankso@170 139 $(get_locale_name) \
pankso@749 140 2>&1 1>&3)
pankso@338 141 retval=$?
pankso@338 142 exec 3>&-
pankso@338 143 case $retval in
pankso@622 144 0) continue ;;
pankso@622 145 1|255) exit 0 ;;
pankso@338 146 esac
al@728 147
al@943 148 # Default: POSIX => English
al@943 149 [ "$locale" = 'en' ] && locale='en_US'
al@943 150 [ -s /etc/locale.conf ] && RECONFIG='yes'
al@728 151
pankso@338 152 # If it's a reconfiguration give an info message.
pankso@338 153 if [ -n "$RECONFIG" ]; then
pankso@751 154 export LC_ALL=$locale
al@813 155 msg=$(_n "\
pankso@622 156 Please logout of your current session and login again to use new locale.")
al@813 157 $DIALOG --clear --title " $(_n 'Information') " --msgbox "$msg" 16 70
pankso@338 158 fi
pankso@749 159 system_config
pankso@338 160 }
pankso@5 161
pankso@338 162 case "$1" in
al@813 163 --help|-h)
al@813 164 usage ;;
pankso@753 165 info)
al@961 166 . /etc/locale.conf
al@943 167 _ 'Config file: %s' '/etc/locale.conf'
al@943 168 _ 'Current locale: %s' "$LANG"
al@813 169 ;;
pankso@338 170 list)
al@813 171 list=
al@813 172 for i in $(ls -1 /usr/share/i18n/locales | grep '[a-z]_[A-Z]'); do
al@813 173 desc=$(fgrep -m1 title /usr/share/i18n/locales/$i | cut -d'"' -f2)
al@813 174 list="$list
al@813 175 $i $desc"
al@813 176 done
al@813 177 optlist "$list" ;;
pankso@749 178 "")
pankso@749 179 # No args: display Ncurses dialog.
pascal@916 180 : ${DIALOG=dialog --timeout 60}
al@813 181 check_root $@
pankso@749 182 dialog_menu ;;
pankso@749 183 *)
pankso@749 184 # Usage: tazlocale LANG_COUNTRY
pankso@749 185 locale=$1
al@813 186 check_root $@
al@813 187 system_config ;;
pankso@5 188 esac
pankso@5 189
pankso@5 190 exit 0