Version 0 of Multilingual menu

Updated 2002-02-05 08:12:11

Richard Suchenwirth - Here is a little menu demo that allows localization for different languages, either at startup (by specifying one of the language codes de, en, fr as first argument, and on-the-fly while running, by using inlined msgcat commands, so the single file is self-contained.

For the on-the-fly functionality, I originally just wanted to entryconfigure the labels, but stubbornly got the error "unknown option "-label"". There's always a workaround, in this case the ugly one of throwing away the whole menu and creating it anew, which causes a little screen flicker both on Sun and Win2K. If anybody knows better, please add it here!

 package require msgcat
 namespace import msgcat::*

 mcset de Language Sprache
 mcset de English  Englisch
 mcset de German   Deutsch
 mcset de French   Franz�sisch

 mcset fr Language Langage
 mcset fr English  Anglais
 mcset fr German   Allemand
 mcset fr French   Francais

 set choice [lindex $argv 0]
 if {[lsearch "en de fr" $choice]>=0} {mclocale $choice}

 proc makeMenu {} {
    catch {destroy .m}
    . config -menu [menu .m]
    menu .m.m2
    .m add cascade -label [mc Language] -menu .m.m2
    foreach {language code} {English en French fr German de} {
        .m.m2 add command -label [mc $language] -command [list reset $code]
    }
 }
 proc reset code {
    mclocale $code
    makeMenu ;# throw away and build anew - not nice, but...

    # ... the following throws "error: unknown option "-label""
    # even though "-label" is shown by entryconfig without arguments!

    #.m.m2 config -label [mc Language]           ;# ERROR
    foreach i {0 1 2} label {English French German} {
        #.m.m2 entryconfig $i -label $label      ;# ERROR 
        puts [join [.m.m2 entryconfigure $i] \n] ;# WORKS
    }
 }
 makeMenu

Tk syntax help - Arts and crafts of Tcl-Tk programming