slitaz-tools view tinyutils/tazlocale @ rev 1010

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