slitaz-base-files view rootfs/lib/libtaz.sh @ rev 177

libtaz.sh, Makefile: fix design of i18n
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon May 21 10:06:27 2012 +0300 (2012-05-21)
parents c09b461ada93
children d8adb319ac56
line source
1 #!/bin/sh
2 #
3 # SliTaz Base functions used from boot scripts to end user tools. Use
4 # gettext and not echo for messages. Keep output suitable for GTK boxes
5 # and Ncurses dialog. LibTaz should not depend on any configuration file.
6 # No bloated code here, functions must be used by at least 3-4 tools.
7 #
8 # Documentation: man libtaz or /usr/share/doc/slitaz/libtaz.txt
9 #
10 # Copyright (C) 2012 SliTaz GNU/Linux - BSD License
11 #
13 # Internationalization.
14 . /usr/bin/gettext.sh
15 # We can't export TEXTDOMAIN because this script includes to other scripts
16 # with other TEXTDOMAIN exported
17 ## TEXTDOMAIN='slitaz-base'
18 ## export TEXTDOMAIN
20 # xgettext (from Makefile) can't extract strings from above example:
21 # gettext -d 'slitaz-base' 'Done'
22 # so, I define own function (and add it as option to xgettext to Makefile)
23 lgettext() {
24 gettext -d 'slitaz-base' $1
25 }
27 # Internal variables.
28 okmsg="$(lgettext 'Done')"
29 ermsg="$(lgettext 'Failed')"
30 : ${okcolor=32}
31 : ${ercolor=31}
32 : ${decolor=36}
34 # Parse cmdline options and store values in a variable.
35 for opt in "$@"
36 do
37 case "$opt" in
38 --*=*) export ${opt#--} ;;
39 --*) export ${opt#--}="yes" ;;
40 esac
41 done
42 [ "$HTTP_REFERER" ] && output="html"
44 # Return command status. Default to colored console output.
45 status() {
46 local check=$?
47 case $output in
48 raw|gtk)
49 done=" $okmsg"
50 error=" $ermsg" ;;
51 html)
52 done=" <span style='color: $okcolor;'>$okmsg</span>"
53 error=" <span style='color: $ercolor;'>$ermsg</span>" ;;
54 *)
55 cols=$(stty -a -F /dev/tty | head -n 1 | cut -d ";" -f 3 | awk '{print $2}')
56 local scol=$(($cols - 10))
57 done="\\033[${scol}G[ \\033[1;${okcolor}m${okmsg}\\033[0;39m ]"
58 error="\\033[${scol}G[ \\033[1;${ercolor}m${ermsg}\\033[0;39m ]" ;;
59 esac
60 if [ $check = 0 ]; then
61 echo -e "$done"
62 else
63 echo -e "$error"
64 fi
65 }
67 # Line separator.
68 separator() {
69 local sepchar="="
70 [ "$HTTP_REFERER" ] && local sepchar="<hr />"
71 case $output in
72 raw|gtk) local sepchar="-" && local cols="8" ;;
73 html) local sepchar="<hr />" ;;
74 *) local cols=$(stty -a -F /dev/tty | head -n 1 | cut -d ";" -f 3 | awk '{print $2}') ;;
75 esac
76 for c in $(seq 1 $cols); do
77 echo -n "$sepchar"
78 done && echo ""
79 }
81 # New line for echo -n or gettext.
82 newline() {
83 echo ""
84 }
86 # Display a bold message. GTK Yad: Works only in --text=""
87 boldify() {
88 case $output in
89 raw) echo "$@" ;;
90 gtk) echo "<b>$@</b>" ;;
91 html) echo "<strong>$@</strong>" ;;
92 *) echo -e "\\033[1m$@\\033[0m" ;;
93 esac
94 }
96 # Usage: colorize "Message" colorNB or use --color=NB option
97 # when running a tool. Default to white/38 and no html or gtk.
98 colorize() {
99 : ${color=$1}
100 shift
101 local content="$@"
102 case $output in
103 raw|gtk|html) echo "$content" ;;
104 *)
105 [ "$color" ] || color=38
106 echo -e "\\033[1;${color}m${content}\\033[0;39m" ;;
107 esac
108 unset color
109 }
111 # Indent text $1 spaces.
112 indent() {
113 local in="$1"
114 shift
115 echo -e "\033["$in"G $@";
116 }
118 # Check if user is logged as root.
119 check_root() {
120 if [ $(id -u) != 0 ]; then
121 lgettext "You must be root to execute:" && echo " $(basename $0) $@"
122 exit 1
123 fi
124 }
126 # Display debug info when --debug is used.
127 debug() {
128 [ "$debug" ] && echo "$(colorize $decolor "DEBUG:") $1"
129 }
131 # Gettextize yes/no.
132 translate_query() {
133 case $1 in
134 y) lgettext "y" ;;
135 Y) lgettext "Y" ;;
136 n) lgettext "n" ;;
137 N) lgettext "N" ;;
138 # Support other cases but keep them untranslated.
139 *) echo "$1" ;;
140 esac
141 }
143 # Usage: echo -n "The question" && confirm
144 confirm() {
145 [ "$yes" ] && true
146 echo -n " ($(translate_query y)/$(translate_query N)) ? "
147 read answer
148 [ "$answer" == "$(translate_query y)" ]
149 }
151 # Log activities. $activity should be set by the script. The log format
152 # is suitable for web interfaces like cook. Usage: log "String"
153 log() {
154 [ "$activity" ] || activity=/var/log/slitaz/libtaz.log
155 echo "$(date '+%Y-%m-%d %H:%M') : $@" >> $activity
156 }