Version 3 of Tk and msgcat

Updated 2007-11-08 14:18:54 by MB

The purpose of this page is to illustrate the use of the msgcat package to make a Tk software internationalized automatically. The recommended usage of the msgcat package to translate messages is to insert msgcat::mc instructions like this (http://wiki.tcl.tk/1488 ):

Instead of

 label .x -text Files

you write

 label .x -text [msgcat::mc Files]

This is good, but forces the developper to insert "msgcat::mc" everywhere into the current Tcl script. Of course, it is well known that the Frink tool can instrument a Tcl script and insert the msgcat::mc automatically. But the resulting script is modified, which may be unwanted (http://wiki.tcl.tk/2611 ).

What should be available is a method to translate automatically the software, """without modification""" of the original script. The only things which should be done is :

- create the "yourlanguage.msg" catalog, - use msgcat and configure it with the desired language.

One can try to overload the Tk widgets to use the msgcat::mc automatically. The following is an example. The first call to "tk_chooseDirectory" is untranslated. Then one creates a translation command inside the "mynamespace" namespace. Then the tk_chooseDirectory is overloaded so that the next use of "tk_chooseDirectory" automatically uses the msgcat::mc command with the provided translation command.

 package require Tcl
 package require Tk
 package require msgcat
 ::msgcat::mclocale fr

 set dirname [tk_chooseDirectory -title "This is an untranslated title"]

 msgcat::mcset fr "This is an untranslated title" "C'est mon titre"
 namespace eval mynamespace {
    proc translate { original } {
        set translated [msgcat::mc $original]
        return $translated
    }
 }

 if {[info commands "::tk_chooseDirectory"]=="::tk_chooseDirectory"} then {
    rename ::tk_chooseDirectory ::_tk_chooseDirectory
    proc ::tk_chooseDirectory {args} {
        set command [list "::_tk_chooseDirectory"]
        foreach {key value} $args {
            if {$key=="-title" || $key=="-message"} then {
                set value [mynamespace::translate $value]
            }
            lappend command $key $value
        }
        set answer [eval $command]
        return $answer
    }
 }
 set dirname [tk_chooseDirectory -title "This is an untranslated title"]



See also:


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

Category Command - Category Local - Category String Processing