slitaz-tools view tinyutils/tazlocale @ rev 1034

Update tazinst.html link (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Aug 21 09:05:49 2020 +0000 (2020-08-21)
parents 96dacc7bbf6d
children 0df361614ee6
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-2017 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.'
18 newline
19 boldify "$(_ 'Usage:')"
20 echo " tazlocale [$(_ 'option')]"
21 newline
22 boldify "$(_ 'Options:')"
23 optlist "\
24 info $(_ 'Show info about config file and current locale.')
25 list $(_ 'Show list of available locales.')"
26 newline
27 _ 'Any other option treated as locale - set locale (root).'
28 _ 'Display locale selecting dialog if no option given (root).'
29 newline
30 }
33 mksymlink() {
34 [ "$(readlink $2)" = "$1" ] || ln -fs "$1" "$2"
35 }
37 # Make symlink to file, substitute "%%" to "ll_CC", "ll" or "en" according to
38 # current language settings and file existence
39 # (where "ll_CC" - full locale format (lang and country, and maybe, modifier).
41 make_i18n_link() {
42 if [ -d $(dirname ${1/.%%/}) ]; then
43 cd $(dirname ${1/.%%/})
45 if [ -e ${1/%%/$LANG} ]; then
46 mksymlink $(basename ${1/%%/$LANG}) ${1/.%%/}
47 else
48 if [ -e ${1/%%/$LANGUAGE} ]; then
49 mksymlink $(basename ${1/%%/$LANGUAGE}) ${1/.%%/}
50 else
51 mksymlink $(basename ${1/%%/en}) ${1/.%%/}
52 fi
53 fi
54 fi
55 }
58 # Create symlink to translated files provided by SliTaz language pack,
59 # doc and config files.
61 link_language_files() {
62 . /etc/locale.conf
63 LANGUAGE=${LANG%_*}
64 [ "$LANG" = 'POSIX' ] && LANGUAGE='en'
66 # Openbox menu
67 make_i18n_link /etc/xdg/openbox/menu.%%.xml
69 # Documentation & manuals
70 find /usr/share/doc -name '*.en.html' | \
71 while read doc; do
72 make_i18n_link ${doc/.en./.%%.}
73 done
75 # SliTaz TazWeb "My Web Home"
76 make_i18n_link /usr/share/tazweb/home.%%.html
78 # SliTaz WebHome
79 make_i18n_link /usr/share/webhome/index.%%.html
81 # TazPanel Doc under www
82 make_i18n_link /var/www/tazpanel/doc/tazpanel.%%.html
83 }
86 # Locale name displayed.
88 locale_names() {
89 [ -d /usr/share/i18n/locales ] &&
90 ls -1 /usr/share/i18n/locales | grep [a-z]_[A-Z]
91 }
93 get_locale_name() {
94 for i in $(locale_names); do
95 echo -n "$i "
96 desc=$(grep ^title /usr/share/i18n/locales/$i | cut -d '"' -f 2)
97 if [ -n "$desc" ]; then
98 echo "$desc" | tr -c '[A-Za-z0-9\n]' '_'
99 else
100 echo "Locale"
101 fi
102 done
103 }
106 # We have no locale files in /usr/lib/locale by default.
107 # Run localedef in background to have a faster boot.
109 gen_utf8_locale() {
110 if [ ! -d "/usr/lib/locale/$locale.UTF-8" ]; then
111 mkdir -p /usr/lib/locale
112 localedef -i "$locale" -c -f 'UTF-8' "/usr/lib/locale/$locale.UTF-8" &
113 fi
114 }
117 # Config /etc/locale.conf
119 system_config() {
120 # If locale not defined: show error and exit
121 ls "/usr/share/i18n/locales/$locale" >/dev/null || return
122 export LC_ALL="$locale.UTF-8"
123 action 'Setting system locale to: %s' "$locale.UTF-8"
124 echo -e "LANG=$locale.UTF-8\nLC_ALL=$locale.UTF-8" > /etc/locale.conf
125 status
126 gen_utf8_locale
127 link_language_files
128 # Clean TazPanel cached headers in old language
129 [ -n "$(which tazpanel)" ] && tazpanel cc
130 }
133 # Dialog menu.
135 dialog_menu() {
136 exec 3>&1
137 locale=$($DIALOG --clear \
138 --title "{ $(_n 'SliTaz language setting') }" \
139 --menu "" 20 72 14 \
140 "en" "English" \
141 $(get_locale_name) \
142 2>&1 1>&3)
143 retval=$?
144 exec 3>&-
145 case $retval in
146 0) continue ;;
147 1|255) exit 0 ;;
148 esac
150 # Default: POSIX => English
151 [ "$locale" = 'en' ] && locale='en_US'
152 [ -s /etc/locale.conf ] && RECONFIG='yes'
154 # If it's a reconfiguration give an info message.
155 if [ -n "$RECONFIG" ]; then
156 export LC_ALL=$locale
157 msg=$(_n "\
158 Please logout of your current session and login again to use new locale.")
159 $DIALOG --clear --title " $(_n 'Information') " --msgbox "$msg" 16 70
160 fi
161 system_config
162 }
164 case "$1" in
165 --help|-h)
166 usage ;;
167 info)
168 . /etc/locale.conf
169 _ 'Config file: %s' '/etc/locale.conf'
170 _ 'Current locale: %s' "$LANG"
171 ;;
172 list)
173 list=
174 for i in $(locale_names); do
175 desc=$(fgrep -m1 title /usr/share/i18n/locales/$i | cut -d'"' -f2)
176 list="$list
177 $i $desc"
178 done
179 optlist "$list" ;;
180 "")
181 # No args: display Ncurses dialog.
182 : ${DIALOG=dialog --timeout 60}
183 check_root $@
184 dialog_menu ;;
185 *)
186 # Usage: tazlocale LANG_COUNTRY
187 locale=$1
188 check_root $@
189 system_config ;;
190 esac
192 exit 0