Multilingual menu

Difference between version 12 and 13 - Previous - Next
[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 4 LOC more, you also get the window title localized ;-)


======package require Tk
package require msgcat
namespace import msgcat::*

mcset de Language Sprache
mcset de English  Englisch
mcset de German   Deutsch
mcset de French   Franz\u00f6sisch
mcset de Test     Versuch

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

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


proc makeMenu {} {
    . config -menu [menu .m]
    menu .m.m2 -tearoff 0
    .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]
    }
    wm title . [mc Test]
}


proc reset code {
    mclocale $code
    .m entryconfig 1 -label [mc Language]
    foreach i {0 1 2} label {English French German} {
        .m.m2 entryconfig $i -label [mc $label]
    }
    wm title . [mc Test]
}
makeMenu
======

----
[[name redacted]]: I changed the 'ö' to a Unicode code.

[AMG]: What made this edit necessary?  Are you having problems with [[[source]]] using the wrong encoding?

[[name redacted]]: If I had `source`d the code I would have made sure to use the `-encoding` option because, as a Swedish-language programmer, I'm used to using it and have a lot of experience with accented letters. Most (I guess?) users on this wiki, however, use the basic latin/[ASCII] alphabet and possibly wouldn't know what to do with such a letter (which sounds like I'm calling those users ignorant, but I'm certainly not: it's more a question of domain knowledge in a minority domain). And even if they did, the most likely way for them to use the code would probably be to copy it into a script file and, e.g., double-click on the icon to launch it in `[wish]` (which, of course, also has an `-encoding` option, but it's a lot more awkward to use it: on Windows you basically need to create a [DOS BAT magic%|%batchfile%|%] file to start `wish` with the correct invocation). So, it's not so much a ''necessary'' edit as a ''courtesy'' edit to make it easier for everyone to use the code. For us accented-letters-users, it's all the same -- there is no significant difference between an 'ö' and a `\u00f6` when we run the code. For another programmer it might mean the difference between text and garbage. Förstår du? `:)`
[HE] 2022-04-25: I added "package require Tk" to assure that the code can be executed on Linux in tclsh.

<<categories>> Local | i18n - Writing for the world | Arts and crafts of Tcl-Tk programming