Subject: Re: tcl and language support - DN [1]
Frederic BONNET <frederic.bonnet@ciril.fr> - 30 Apr 1999 - comp.lang.tcl
Hi,
Christoph Panwinkler wrote:
> Is there a possibility to configure widgets in specific language, e.g.
> german language.
> For example, I would like to configure the "yes" and "no" buttons of a
> message box to "ja" and "nein".
Here's a quick and effective way of defining language-specific strings and even
changing them on the fly:
People usually specify widget strings using -text option:
label .l -text "blah"
However there is another method that, when cleverly used, can make multilingual
GUIs very easy to build. Most widgets that provide -text option also provide
-textvariable or similar option. The idea is to provide the name of a global
variable that contains the string, rather than the string itself:
label .l -textvariable var
set var "blah"
The advantage is that you can change widget strings with "set" rather than
"configure". You can also share strings between widgets.
Now let's see what happens if we choose to associate a widget to an array
element:
button .yes -textvariable Lang(Yes)
button .no -textvariable Lang(No)
The Tcl array syntax makes it very easy to set a series of array elements:
array set English {
Yes "Yes"
No "No"
}
With one line you can also set the values of an array to the values of another:
array set Lang [array get English]
The complete script:
# Create buttons with Lang array elements as textvariables
button .yes -textvariable Lang(Yes)
button .no -textvariable Lang(No)
pack .yes .no
# Define interface strings in several languages: associate
# symbolic names (eg. Yes) to language-specific values (eg. "Ja")
array set English {
Yes "Yes"
No "No"
}
array set Deutsch {
Yes "Ja"
No "Nein"
}
# Procedure to change language on the fly
proc setLang {newLang} {
global Lang $newLang
array set Lang [array get $newLang]
}
setLang English
Now type on the console and enjoy:
setLang Deutsch
That way you can build localizable apps very easily: you can put all the strings
for one language into one file and source it when needed. It also makes
translation very easy (you just have to translate one file).
See you, Fred
--
Frédéric BONNET frederic.bonnet@ciril.fr
---------------------------------------------------------------
"Theory may inform, but Practice convinces"
George Bain
Last modified
1999-09-27
1999-09-27
(195.108.246.50)
Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated
