ISO: sample code to dynamically alter Tk's widget label language 
Author Message
 ISO: sample code to dynamically alter Tk's widget label language

Does anyone have some sample code that displays some Tk toolkit widgets,
then provides for the ability to change the language in which those labels
are displayed?

In other words, some sort of radio button, etc. that would allow one to
toggle between english, french, german, etc. and the tk native widget
labels (on things like dialogs, menus, etc. - non-user provided stuff)
would change language?

It's non-obvious to me how this would be written.
--
The Tenth Annual Tcl/Tk Conference <URL: http://www.*-*-*.com/ ;
Even if explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.



Sun, 30 Oct 2005 19:36:41 GMT  
 ISO: sample code to dynamically alter Tk's widget label language
Actually, the "pefered" method is through the msgcat facilities, but
if you want to switch language on the fly (i.e. without restarting the app)
I prefer
to place all messages in an array. I have used this technique in the past
for a
large app, and worked well.

The first step is to create the language files. Each one will define a
global array
with the messages, ie:

English.msg:
array set LocalisedMessage {
  {this is a test} {this is a test}
  ... .... .... ...

Quote:
}

Hellenic.msg:
array set LocalisedMessage {
  {this is a test} "\u????\u????\u????"
  ... ...

Quote:
}

Then in your code, you can create a widget that changes the language. What i
implemented
was a menubutton in the status bar of the app, which scans the directory
with the localised
messages and displays the proper information.

In your code, to use the above, you use the -textvariable option of widgets:

label .text -textvariable ::LocalisedMessage({this is a test})

When a different message file is sourced into tcl, the LocalisedMessage
array is changed and all messages instantly change. However, some
widgets such as menus are more tricky, but also feasible.

Theer is a drawback though: All messages must be defined for the main
language (which gets always sourced), so as to define all entries and not
get an error. (This limitation does not exists with the msgcat facilities).
You can overcome of course this with a read trace that defines
the undefined entries, but I expect it to be inefficient :-)
That is why I proposed an addition to the trace command
(see thread "A new suggestion to the "trace" command...")

George

Quote:

> Does anyone have some sample code that displays some Tk toolkit widgets,
> then provides for the ability to change the language in which those labels
> are displayed?

> In other words, some sort of radio button, etc. that would allow one to
> toggle between english, french, german, etc. and the tk native widget
> labels (on things like dialogs, menus, etc. - non-user provided stuff)
> would change language?

> It's non-obvious to me how this would be written.
> --
> The Tenth Annual Tcl/Tk Conference <URL:

http://www.tcl.tk/community/tcl2003 >
Quote:
> Even if explicitly stated to the contrary, nothing in this posting
> should be construed as representing my employer's opinions.




Sun, 30 Oct 2005 21:19:17 GMT  
 ISO: sample code to dynamically alter Tk's widget label language
I have an example for a simple text editor that I wrote. I can put the
code up on the wiki if you want me to?

- Veronica

Quote:

> Does anyone have some sample code that displays some Tk toolkit widgets,
> then provides for the ability to change the language in which those labels
> are displayed?

> In other words, some sort of radio button, etc. that would allow one to
> toggle between english, french, german, etc. and the tk native widget
> labels (on things like dialogs, menus, etc. - non-user provided stuff)
> would change language?

> It's non-obvious to me how this would be written.



Sun, 30 Oct 2005 21:33:03 GMT  
 ISO: sample code to dynamically alter Tk's widget label language
I put up the code on the wiki. http://wiki.tcl.tk/Mimers%20brunn has a link.

And, yes, this uses msgcat.

- Veronica

Quote:

> I have an example for a simple text editor that I wrote. I can put the
> code up on the wiki if you want me to?

> - Veronica


>> Does anyone have some sample code that displays some Tk toolkit widgets,
>> then provides for the ability to change the language in which those
>> labels
>> are displayed?

>> In other words, some sort of radio button, etc. that would allow one to
>> toggle between english, french, german, etc. and the tk native widget
>> labels (on things like dialogs, menus, etc. - non-user provided stuff)
>> would change language?

>> It's non-obvious to me how this would be written.



Sun, 30 Oct 2005 21:39:48 GMT  
 ISO: sample code to dynamically alter Tk's widget label language


:
:Does anyone have some sample code that displays some Tk toolkit widgets,
:then provides for the ability to change the language in which those labels
:are displayed?
:
:In other words, some sort of radio button, etc. that would allow one to
:toggle between english, french, german, etc. and the tk native widget
:labels (on things like dialogs, menus, etc. - non-user provided stuff)
:would change language?
:
:It's non-obvious to me how this would be written.

It appears that what I was requesting wasn't clear.

The default, out of the box, Tk widgets currently have various words
in them.

As I read the tk.tcl file, I see that the code makes use of msgcat for
these words.  I look in library/msgs and see a number of languages
supported.

What I can't determine is how to write a sample application that would
demonstrate moving between these supported languages.
--
The Tenth Annual Tcl/Tk Conference <URL: http://www.tcl.tk/community/tcl2003 >
Even if explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.



Sun, 30 Oct 2005 22:24:56 GMT  
 ISO: sample code to dynamically alter Tk's widget label language
You would use the same technique as if you had written the messages
yourself. Basically, set the locale, and then "repaint" the widgets.

- Veronica

Quote:


> :
> :Does anyone have some sample code that displays some Tk toolkit widgets,
> :then provides for the ability to change the language in which those labels
> :are displayed?
> :
> :In other words, some sort of radio button, etc. that would allow one to
> :toggle between english, french, german, etc. and the tk native widget
> :labels (on things like dialogs, menus, etc. - non-user provided stuff)
> :would change language?
> :
> :It's non-obvious to me how this would be written.

> It appears that what I was requesting wasn't clear.

> The default, out of the box, Tk widgets currently have various words
> in them.

> As I read the tk.tcl file, I see that the code makes use of msgcat for
> these words.  I look in library/msgs and see a number of languages
> supported.

> What I can't determine is how to write a sample application that would
> demonstrate moving between these supported languages.



Sun, 30 Oct 2005 22:54:53 GMT  
 ISO: sample code to dynamically alter Tk's widget label language
And by the way. We really need to do something about that number of
message-files. I was going to suggest that we consider making use of the
Free Translation Project. http://www.iro.umontreal.ca/contrib/po/HTML/

I would be happy to be the TP coordinator if noone else is interested.
In about a month or so. We could at least start a discussion about this.

We should also consider starting in on the Tcl-core.

Veronica Loell

Quote:


> :
> :Does anyone have some sample code that displays some Tk toolkit widgets,
> :then provides for the ability to change the language in which those labels
> :are displayed?
> :
> :In other words, some sort of radio button, etc. that would allow one to
> :toggle between english, french, german, etc. and the tk native widget
> :labels (on things like dialogs, menus, etc. - non-user provided stuff)
> :would change language?
> :
> :It's non-obvious to me how this would be written.

> It appears that what I was requesting wasn't clear.

> The default, out of the box, Tk widgets currently have various words
> in them.

> As I read the tk.tcl file, I see that the code makes use of msgcat for
> these words.  I look in library/msgs and see a number of languages
> supported.

> What I can't determine is how to write a sample application that would
> demonstrate moving between these supported languages.



Sun, 30 Oct 2005 22:58:59 GMT  
 ISO: sample code to dynamically alter Tk's widget label language

Quote:

> Does anyone have some sample code that displays some Tk toolkit widgets,
> then provides for the ability to change the language in which those labels
> are displayed?

> In other words, some sort of radio button, etc. that would allow one to
> toggle between english, french, german, etc. and the tk native widget
> labels (on things like dialogs, menus, etc. - non-user provided stuff)
> would change language?

> It's non-obvious to me how this would be written.

I agree, it's non trivial.

I had some thoughts a while back about how to do this on a general
level. Nothing has come to fruition yet. I had thought about making the
widget name the string to be translated, and somehow incorporating the
application namespace into the widget path, so that msgcat knows where
to look for translated strings. Each widget that could be dynamically
translated should have some way of being recognized, such as starting

looking for matching names, look in the relevant namespace, and
translated with widget text.

Something like this (quickly knocked-up):

# ----------
   namespace eval myapp {}

proc myapp::gui {} {

   set ns [namespace current]
   regsub -all "::" $ns "-" name
   set W .$name:1
   toplevel $W


   variable Language
   foreach lang {en fr de} Language {English French German} {

     radiobutton $r \
       -text "$Language ($lang)" \
       -variable ${ns}::Language \
       -value $Language \
       -command [list trans $W $lang]
     pack $r -side top -anchor w
   }
   set Language English

Quote:
}

proc myapp::main {} {

   package require msgcat
   namespace import ::msgcat::*

   mcset fr "English" "Anglais"
   mcset fr "French" "Fran?ais"
   mcset fr "German" "Allemand"

   mcset de "English" "Englischer"
   mcset de "French" "Franz?sischer"
   mcset de "German" "Deutscher"

   mcset fr "Select Language" "Choisissez La Langue"
   mcset de "Select Language" "W?hlen Sie Sprache Vor"

   gui

Quote:
}

proc trans {W lang} {

   puts "proc trans \[$W\] \[$lang\]"

   puts "  current locale = \[[msgcat::mclocale]\]"
   msgcat::mclocale $lang

   trans:tree $W

Quote:
}

proc trans:tree {W} {

   puts "proc trans:tree \[$W\]"

   set name [winfo name $W]

     set from [string range $name 1 end]
     puts "  from  = \[$from\]"
     set top [winfo toplevel $W]
     regexp {\.(.*):(.*)} $top -> space index
     regsub -all -- - $space :: ns
     puts "  space = \[$space\] ns = \[$ns\]"
     set to [namespace eval $ns [list mc $from]]
     puts "  to    = \[$to\]"
     switch -- [winfo class $W] {
       Label -
       Radiobutton {
         $W configure -text $to
       }
     }
   }

   foreach c [winfo children $W] {
     trans:tree $c
   }

Quote:
}

# ----------

Whatcha think?

Personally I'd rather not load all message catalogs into memory at the
same time. Having a large application translated into several languages
would not be very efficient. Maybe have a 'lang' command in each
namespace to load the required language message catalog?

I like the msgcat system, but I think it has a few deficiencies. I'll
try and make some notes for improvement.

--
Mark G. Saye



Mon, 31 Oct 2005 01:51:58 GMT  
 ISO: sample code to dynamically alter Tk's widget label language
I stand on my method, but I wrote it over a year ago. I still say that
it's not a good idea to try to make some sort of magical transformation.
That will make it unclear as to what exactly is happening. The only part
of the program that is effected is the currently open windows. Changing
the language with msgcat-set, will then use that language when anything
new is rendered. To apply it to an open window, refresh/repaint the
widgets in question, or as I did in my example, collect the
text-settings of the widgets into one procedure that is used to set
the language.
To make it perhaps not trivial but congruent with msgcat you need to
think like msgcat works, not try to find a workaround, try to work
with msgcat.

IMO that is,
Veronica

Quote:


>> Does anyone have some sample code that displays some Tk toolkit widgets,
>> then provides for the ability to change the language in which those
>> labels
>> are displayed?

>> In other words, some sort of radio button, etc. that would allow one to
>> toggle between english, french, german, etc. and the tk native widget
>> labels (on things like dialogs, menus, etc. - non-user provided stuff)
>> would change language?

>> It's non-obvious to me how this would be written.

> I agree, it's non trivial.

> I had some thoughts a while back about how to do this on a general
> level. Nothing has come to fruition yet. I had thought about making the
> widget name the string to be translated, and somehow incorporating the
> application namespace into the widget path, so that msgcat knows where
> to look for translated strings. Each widget that could be dynamically
> translated should have some way of being recognized, such as starting

> looking for matching names, look in the relevant namespace, and
> translated with widget text.

> Something like this (quickly knocked-up):

> # ----------
>   namespace eval myapp {}

> proc myapp::gui {} {

>   set ns [namespace current]
>   regsub -all "::" $ns "-" name
>   set W .$name:1
>   toplevel $W


>   variable Language
>   foreach lang {en fr de} Language {English French German} {

>     radiobutton $r \
>       -text "$Language ($lang)" \
>       -variable ${ns}::Language \
>       -value $Language \
>       -command [list trans $W $lang]
>     pack $r -side top -anchor w
>   }
>   set Language English

> }

> proc myapp::main {} {

>   package require msgcat
>   namespace import ::msgcat::*

>   mcset fr "English" "Anglais"
>   mcset fr "French" "Fran?ais"
>   mcset fr "German" "Allemand"

>   mcset de "English" "Englischer"
>   mcset de "French" "Franz?sischer"
>   mcset de "German" "Deutscher"

>   mcset fr "Select Language" "Choisissez La Langue"
>   mcset de "Select Language" "W?hlen Sie Sprache Vor"

>   gui

> }

> proc trans {W lang} {

>   puts "proc trans \[$W\] \[$lang\]"

>   puts "  current locale = \[[msgcat::mclocale]\]"
>   msgcat::mclocale $lang

>   trans:tree $W

> }

> proc trans:tree {W} {

>   puts "proc trans:tree \[$W\]"

>   set name [winfo name $W]

>     set from [string range $name 1 end]
>     puts "  from  = \[$from\]"
>     set top [winfo toplevel $W]
>     regexp {\.(.*):(.*)} $top -> space index
>     regsub -all -- - $space :: ns
>     puts "  space = \[$space\] ns = \[$ns\]"
>     set to [namespace eval $ns [list mc $from]]
>     puts "  to    = \[$to\]"
>     switch -- [winfo class $W] {
>       Label -
>       Radiobutton {
>         $W configure -text $to
>       }
>     }
>   }

>   foreach c [winfo children $W] {
>     trans:tree $c
>   }

> }
> # ----------

> Whatcha think?

> Personally I'd rather not load all message catalogs into memory at the
> same time. Having a large application translated into several languages
> would not be very efficient. Maybe have a 'lang' command in each
> namespace to load the required language message catalog?

> I like the msgcat system, but I think it has a few deficiencies. I'll
> try and make some notes for improvement.



Mon, 31 Oct 2005 02:14:28 GMT  
 ISO: sample code to dynamically alter Tk's widget label language


:And by the way. We really need to do something about that number of
:message-files. I was going to suggest that we consider making use of the
:Free Translation Project. http://www.iro.umontreal.ca/contrib/po/HTML/
:
:I would be happy to be the TP coordinator if noone else is interested.
:In about a month or so. We could at least start a discussion about this.
:
:We should also consider starting in on the Tcl-core.

I was surprised on this topic several times today.  First that the
tk work had begun.  Then that the tcl work had not yet begun ...

Anyone taken a look at what in Tk remains non-msgcat'aloged?

--
The Tenth Annual Tcl/Tk Conference <URL: http://www.tcl.tk/community/tcl2003 >
Even if explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.



Mon, 31 Oct 2005 02:36:30 GMT  
 
 [ 42 post ]  Go to page: [1] [2] [3]

 Relevant Pages 

1. HELP! REFRESHING LABEL WIDGET IN TCL/TK

2. Does tk support scrollbar for label/scale widgets ?

3. Code sample for C Tk lib usage

4. Need small sample C code calling Tk script

5. Sample Tcl/TK code segmen

6. Sample Tcl/TK code segments

7. Looking for sample TK code

8. Tcl/Tk sample code?

9. sample code for Matt Neuburg's book?

10. MSSQL 'alter table' from Clarion

11. Can Text widget autosize like Label widget?

12. Altering 'print'/mod_python

 

 
Powered by phpBB® Forum Software