
README for language encodings
-----------------------------

If you write your language specific message catalog saving it
using the system encodings, which is likely unless you have an
editor producing utf-8 text, you need to translate it using
the utf-8 encoding. If you don't you'll se a number of '?'
for your national characters.

Instructions:

1.   Copy the en.msg file to a file name describing your language code
and platform. Say, "fr.win" for French on Windows. As an alternative
you can take the Swedish message catalog, "sv.msg", as a template.

2.   Open it in your text editor. This file should be readable.
Now you edit it in your system encoding. Change all occurences of
"::msgcat::mcset en" to "::msgcat::mcset fr" if on a French system,
for instance. The syntax for each entry in a message catalog is:
::msgcat::mcset langCode key ?translatedString?
Add the 'translatedString' if missing, and do translate if exists.
Save your file as 'fr.sys', for instance.

3.  Utf-8 encode the file using the script below. Now you pick a file
name 'fr.msg' if French, for instance.

4.  You new language catalog should now be loaded automatically.

5.  As a check that all entries in your message catalog exists you
may use the small script at the end of this file.

Now try it...


Use the following code to do this:

........................................................................

# Translate a file from system encoding to utf-8

set src [tk_getOpenFile -title {Pick your language catalog}]
if {$src == ""} return
set new "[file rootname [file tail $src]].msg"
set fdsrc [open $src "r"]
set txt [read $fdsrc];
close $fdsrc
set dst [tk_getSaveFile -title {Pick new catalog}  \
	-initialdir [file dirname $src] -initialfile $new]
if {$dst == ""} return
set fddst [open $dst "w"]
fconfigure $fddst -encoding utf-8
puts $fddst $txt
close $fddst
if {$::tcl_platform(platform) == "macintosh"} {
    set creator [file attributes $src -creator]
    file attributes $dst -creator $creator
}

........................................................................


# If you for some reason want to translate any of the message catalogs
# that are utf-8 encoded to your system encoding do:

set src [tk_getOpenFile -title {Pick your utf-8 language catalog} \
	-filetypes {{{Message Catalogs}       {.msg}}}]
set suff [string range $::tcl_platform(platform) 0 2]
set new "[file rootname [file tail $src]].${suff}"
set fdsrc [open $src "r"]
fconfigure $fdsrc -encoding utf-8
set txt [read $fdsrc];
close $fdsrc
set dst [tk_getSaveFile -title {Save system encoded}  \
	-initialdir [file dirname $src] -initialfile $new]
set fddst [open $dst "w"]
puts $fddst $txt
close $fddst
if {$::tcl_platform(platform) == "macintosh"} {
    set creator [file attributes $src -creator]
    file attributes $dst -creator $creator
}

........................................................................

# If you want to compare an existing message catalog with the original
# en.msg to see if any parts missing use the script below.
# Quit Wish when finished!

namespace eval ::msgcat:: {}
proc ::msgcat::mcset {lang key {str ""}} {
    global arr keylist table
    if {$lang == "en"} {
        lappend keylist $key
    }
    set table($lang,$key) $str
    set arr($lang,$key) [uplevel 1 {namespace current}]
}
set keylist {}
set src [tk_getOpenFile -title {Pick en.msg File} \
	-filetypes {{{Message Catalog}       {.msg}}}]
if {$src == ""} return
if {[file tail $src] != "en.msg"} {
    tk_messageBox -message "Wrong file!"
    return
}
set dst [tk_getOpenFile -title {Pick Your Message Catalog} \
	-filetypes {{{Message Catalog}       {.msg}}}]
if {$dst == ""} return
set rootdst [file rootname [file tail $dst]]
set lang $rootdst
source $src
source $dst
foreach name [lsort [array names arr en,*]] {
    regexp {en,(.*)} $name match key
    if {![info exists arr($lang,$key)]} {
         puts "Missing: $key"
    } elseif {$arr(en,$key) != $arr($lang,$key)} {
        puts "Wrong namespace: $key, \t$arr($lang,$key) -> $arr(en,$key)"
    }
}

if {0} {
# Create 'new' catalog. Encoding wrong!!!
set dir [file dirname $dst]
set new [file join $dir ${lang}_new.msg]
set fd [open $new w]
fconfigure $fd -encoding utf-8
foreach key $keylist {
    if {[info exists table($lang,$key)]} {
        puts $fd [list ::msgcat::mcset $lang $key $table($lang,$key)]
    } else {
        puts $fd [list #::msgcat::mcset $lang $key $table(en,$key)]
    }
}
close $fd
unset keylist
}
unset arr
puts "\n::msgcat::mcset has been redefined; do not reuse this shell"

........................................................................

