tazpanel view locale.cgi @ rev 520
PROVIDE support in package list report
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Tue Aug 04 11:16:14 2015 +0200 (2015-08-04) |
parents | 470ab4241de5 |
children | 551a881b8292 |
line source
1 #!/bin/sh
2 #
3 # Locale settings CGI interface.
4 #
5 # Copyright (C) 2015 SliTaz GNU/Linux - BSD License
6 #
9 # Common functions from libtazpanel
11 . lib/libtazpanel
12 get_config
13 header
15 TITLE=$(_ 'Locale')
19 #############################
20 # Get info from locale file #
21 #############################
23 get_locale_info()
24 {
25 # Commands like `LC_ALL=fr_FR locale -k LC_MEASUREMENT` will do the job
26 # only when your locale is generated and exists in the /usr/lib/locale.
27 # Here we manually parse locale definition files from /usr/share/i18n/locales/.
28 # Strange, bloated and not script-friendly format :(
30 [ ! -e /usr/share/i18n/locales/$1 ] && return
32 # Prepare file
33 if [ ! -e /tmp/tazpanel-$1 ]; then
34 sed 's|^[ \t]*||;/^%/d;/^comment_char/d;/^escape_char/d' /usr/share/i18n/locales/$1 | tr '\n' '&' | sed 's|/&||g' | tr '&' '\n' | sed 's|<U\([0-9a-fA-F]*\)>|\&#x\1;|g' | sed 's|�|\&#x|g' > /tmp/tazpanel-$1
35 fi
37 local ANS=$(grep -e "^$2[ ]" /tmp/tazpanel-$1 | sed 's|^[^ \t][^ \t]* *||')
38 if [ -z "$ANS" ]; then
39 # Not found, then section is copied from other locale definition file...
40 case $2 in
41 measurement)
42 section='LC_MEASUREMENT';;
43 width|height)
44 section='LC_PAPER';;
45 currency_symbol|int_curr_symbol)
46 section='LC_MONETARY';;
47 day|abday|mon|abmon|d_t_fmt|d_fmt|t_fmt|am_pm|t_fmt_ampm|date_fmt)
48 section='LC_TIME';;
49 esac
50 # Recursive call
51 get_locale_info $(sed -n '/^'$section'/,/^END '$section'/p' /tmp/tazpanel-$1 | grep 'copy' | cut -d'"' -f2) $2
52 else
53 case $2 in
54 day|abday|mon|abmon|am_pm) # semicolon-separated list in double quotes
55 echo "$ANS";;
56 *) # single value in double qoutes
57 echo "$ANS" | cut -d'"' -f2;;
58 esac
59 fi
60 }
63 # Get info from locale file about measurement system
65 get_locale_info_measurement()
66 {
67 # faster to use pre-processed values
68 case $1 in
69 en_AG|en_US|es_PR|es_US|nl_AW|yi_US) _ 'US' ;;
70 POSIX) ;;
71 *) _ 'metric' ;;
72 esac
73 }
76 # Get info from locale file about paper size
78 get_locale_info_paper()
79 {
80 # faster to use pre-processed values
81 case $1 in
82 en_AG|en_US|es_PR|es_US|nl_AW|yi_US) echo '8½×11 (US Letter)';;
83 en_CA|en_PH|es_CL|es_CO|es_CR|es_GT|es_MX|es_NI|es_PA|es_SV|es_VE|fil_PH|fr_CA|ik_CA|iu_CA|shs_CA|tl_PH) echo '216×279 (US Letter)';;
84 POSIX) ;;
85 *) echo '210×297 (A4)';;
86 esac
87 }
90 # Get info from locale file about date and time format
92 get_locale_info_date_time()
93 {
94 case $2 in
95 c) get_locale_info $1 d_t_fmt ;;
96 x) get_locale_info $1 d_fmt ;;
97 X) get_locale_info $1 t_fmt ;;
98 r) get_locale_info $1 t_fmt_ampm ;;
99 *) get_locale_info $1 date_fmt ;;
100 esac | sed 's| | |g; s|%|%|g; s|,|,|g; s|-|-|g; s|.|.|g; s|/|/|g; s|:|:|g; s|A|A|g; s|B|B|g; s|C|C|g; s|F|F|g; s|H|H|g; s|I|I|g; s|M|M|g; s|O|O|g; s|R|R|g; s|S|S|g; s|T|T|g; s|X|X|g; s|Y|Y|g; s|Z|Z|g; s|a|a|g; s|b|b|g; s|e|e|g; s|d|d|g; s|k|k|g; s|m|m|g; s|n|n|g; s|o|o|g; s|p|p|g; s|r|r|g; s|t|t|g; s|x|x|g; s|y|y|g; s|z|z|g;'
102 }
105 parse_date()
106 {
107 local weekday month day abday mon abmon rtime d_fmt t_fmt am_pm
108 weekday=$(( $(date +%w) + 1 )) # 1=Sunday ...
109 month=$(date +%-m) # 1=January ...
110 day=$(get_locale_info $1 day | cut -d'"' -f$(( 2 * $weekday )) | sed 's|&|\\\&|g') # translated day of week
111 abday=$(get_locale_info $1 abday | cut -d'"' -f$(( 2 * $weekday )) | sed 's|&|\\\&|g') # same, abbreviated
112 mon=$(get_locale_info $1 mon | cut -d'"' -f$(( 2 * $month )) | sed 's|&|\\\&|g') # translated month
113 abmon=$(get_locale_info $1 abmon | cut -d'"' -f$(( 2 * $month )) | sed 's|&|\\\&|g') # same, abbreviated
114 # next %-codes expanded into other %-codes
115 rtime=$(get_locale_info_date_time $1 r | sed 's|&|\\\&|g') # %r: 12-hour time
116 d_fmt=$(get_locale_info_date_time $1 x | sed 's|&|\\\&|g') # %x: date
117 t_fmt=$(get_locale_info_date_time $1 X | sed 's|&|\\\&|g') # %X: time
119 case $(LC_ALL=POSIX date +%P) in # translated am/pm
120 am) am_pm=$(get_locale_info $1 am_pm | cut -d'"' -f2 | sed 's|&|\\\&|g');;
121 pm) am_pm=$(get_locale_info $1 am_pm | cut -d'"' -f4 | sed 's|&|\\\&|g');;
122 esac
124 # r x X | OC | Y y Oy Ey | m -m Om | d -d Od | e -e Oe | F | H OH k | I OI l | M OM | S OS | R T | Z z | t | P p Op A a B b
125 # Note: %P=am/pm; %p=AM/PM. But here they the same because it is not a simple job to convert letters.
126 echo "$2" | sed "s|%r|$rtime|; s|%x|$d_fmt|; s|%X|$t_fmt|; \
127 s|%OC|S(date +%OC)|; \
128 s|%Y|$(date +%Y)|; s|%y|$(date +%y )|; s|%Oy|$(date +%Oy)|; s|%Ey|$(date +%Ey)|; \
129 s|%m|$(date +%m)|; s|%-m|$(date +%-m)|; s|%Om|$(date +%Om)|; \
130 s|%d|$(date +%d)|; s|%-d|$(date +%-d)|; s|%Od|$(date +%Od)|; \
131 s|%e|$(date +%e)|; s|%-e|$(date +%-e)|; s|%Oe|$(date +%Oe)|; \
132 s|%F|$(date +%F)|; \
133 s|%H|$(date +%H)|; s|%OH|$(date +%OH)|; s|%k|$(date +%k)|; \
134 s|%I|$(date +%I)|; s|%OI|$(date +%OI)|; s|%l|$(date +%l)|; \
135 s|%M|$(date +%M)|; s|%OM|$(date +%OM)|; \
136 s|%S|$(date +%S)|; s|%OS|$(date +%OS)|; \
137 s|%R|$(date +%R)|; s|%T|$(date +%T )|; \
138 s|%Z|$(date +%Z)|; s|%z|$(date +%z )|; \
139 s|%t|\t|; \
140 s|%P|$am_pm|; s|%p|$am_pm|; s|%Op|$am_pm|; s|%A|$day|; s|%a|$abday|; s|%B|$mon|; s|%b|$abmon|;"
142 }
144 list_of()
145 {
146 cd /usr/share/i18n/locales
147 #mon=$(date +%-m); monn=$(( $mon * 2 ))
148 #echo "mon=\"$mon\" monn=\"$monn\""
150 cat <<EOT
151 <table class="wide zebra filelist">
152 <thead>
153 <tr><td>Locale</td>
154 <td>Date format</td>
155 <td>Native date</td>
156 <td>Measurement</td>
157 <td>Paper size</td>
158 </tr>
159 </thead>
160 EOT
162 for LOC in en_US fr_CA be_BY ca_IT el_CY fr_CH ru_RU ru_UA; do
163 case $LOC in
164 iso*|translit*) ;;
165 *)
166 #echo -e "$LOC:\t$(parse_date $LOC $(get_locale_info_date_time $LOC c | sed 's|&|\\\&|g'))";;
167 FMT="$(get_locale_info_date_time $LOC x)"
168 echo "<tr><td>$LOC</td><td>$FMT</td><td>"
169 parse_date $LOC "$FMT"
170 echo "</td><td>"
171 get_locale_info_measurement $LOC
172 echo "</td><td>"
173 get_locale_info_paper $LOC
174 echo "</td></tr>"
175 ;;
176 esac
177 done
178 echo '</table>'
179 }
185 #
186 # Default xHTML content
187 #
189 xhtml_header
190 check_root_tazpanel
192 cat <<EOT
193 <h2>Under construction!</h2>
195 <section>
196 <header>Current date and some i18n values in the different locales:</header>
197 $(list_of)
198 </section>
199 EOT
201 xhtml_footer
202 exit 0