From: Michael S. <sc...@un...> - 2004-12-11 09:38:48
|
Ricardo Jorge wrote: > Hi, > > I need to customize menus for customers, based on their "preffered > language" set in their browsers. > > To test I changed mypage.tcl ( included in TCLHTTPD package ) to > present a different header ( menu header ) for each customer using > env(LANG) variable, but it seems to take that info ( current language ) > from local machine. I mean, the one tclhttpd is running. > > The menus are beeing changed to the correct "local" language, so msgcat > implementation seems to be correct. > > I need a way to get (LANG) or whatever variable name, that shows the > language set at the remote browser, not local machine. > > What is the trick ? Did someone else achieve this feature ? Thanks > Try this little example: <html> [ Doc_Dynamic package require msgcat # set the locale set old [msgcat::mclocale] msgcat::mcset de "User set language preference" "Der Nutzer hat eine Sprachpräferenz gesetzt" array unset headers array set headers [Httpd_DumpHeaders [Httpd_CurrentSocket]] if {[info exists headers(accept-language)]} { set items [split $headers(accept-language) {,;}] set out "User languages<br>\n" set ll [list] set langs [list] foreach item $items { if {[string match "q=*" $item]} { set weight [string range $item 2 end] lappend langs [list $weight $ll] set ll [list] } else { lappend ll $item } } set langs [lsort -index 0 -real -decreasing $langs] append out "<table>" foreach row $langs { foreach {weight lang} $row { append out "<tr><td>$weight</td><td>$lang</td></tr>\n" } } append out "</table>" # if we want to use msgcat we have to convert the lang into a format useful for # msgcat and use msgcat::mclocale $lang to set it # this simply assumes the browser sends appropriate lang info set prefered [lindex $langs 0 1 0] set mcpref [string map {- _} $prefered] msgcat::mclocale $mcpref # now use msgcat::mc for outputting text append out "<h2>[msgcat::mc "User set language preference"] : $mcpref</h2>" } else { set out "User has set no language preference" } #reset the locale to the old setting msgcat::mclocale $old set out ] </html> Michael |