Tk internationalization

Bill Poser 2005-08-12: Although tcl has a general mechanism for internationalization, the msgcat package, some Tk widgets are not internationalized. I recently encountered this problem with tk_optionMenu. The problem is that the labels on radiobuttons are the same as the values, which in many cases forces the use of English names since the values must be meaningful to other functions. Here's an internationalized version of tk_optionMenu:


  proc OptionMenu {w varName firstValue args} {
    upvar #0 $varName var

    if {![info exists var]} {
        set var $firstValue
    }
    menubutton $w -text [::msgcat::mc $firstValue] -indicatoron 1 -menu $w.menu \
            -relief raised -bd 2 -highlightthickness 2 -anchor c \
            -direction flush
    menu $w.menu -tearoff 0
    $w.menu add radiobutton -label [::msgcat::mc $firstValue] -variable $varName \
            -value $firstValue -command "UpdateOptionMenuLabel $w $varName"
    foreach i $args {
            $w.menu add radiobutton -label [::msgcat::mc $i] -variable $varName \
            -value $i -command "UpdateOptionMenuLabel $w $varName"
    }
    return $w.menu
  }

  proc UpdateOptionMenuLabel {w v} {
    upvar #0 $v x
    $w configure -text [::msgcat::mc $x]
  }


  # Test:
  package require Tk
  package require msgcat
  namespace import msgcat::mcset
 #msgcat::mclocale en
  msgcat::mclocale de               ;# <=====<< Select language

 # DE - German messages:
  mcset de "red"      "Rot"
  mcset de "yellow"   "Gelb"
  mcset de "green"    "Grün"
  mcset de "blue"     "Blau"
  mcset de "white"    "Weiss"
  mcset de "black"    "Schwarz"

  OptionMenu .f Color red yellow blue green magenta white black
  label .l -textvariable Color
  pack  .l .f

HJG Its always nice to have an example that works by just copy+paste from the wiki...