diff options
author | ericm <ericm> | 2000-07-17 22:25:25 (GMT) |
---|---|---|
committer | ericm <ericm> | 2000-07-17 22:25:25 (GMT) |
commit | e58ac604378b855c137c05576fd796df952cb349 (patch) | |
tree | 4aaf12e0f958eefbadbedaafe08b0bd0d6340cbe | |
parent | 247225ff4ff6cee54489ee55dc9911d459e1003d (diff) | |
download | tcl-e58ac604378b855c137c05576fd796df952cb349.zip tcl-e58ac604378b855c137c05576fd796df952cb349.tar.gz tcl-e58ac604378b855c137c05576fd796df952cb349.tar.bz2 |
* library/msgcat1.0/msgcat.tcl:
* doc/msgcat.n:
* tests/msgcat.test: Applied patches from Chris Nelson, to provide
the mcmset function, which allows the translator to set multiple
string translations in a single function call, rather than
requiring many calls to mcset. [RFE: 6000, 5993]. In addition,
these patches correct mcload to use utf-8 encoding on when reading
message catalog files, and provides for better default behavior
for determining the locale on a Windows system.
-rw-r--r-- | ChangeLog | 12 | ||||
-rw-r--r-- | doc/msgcat.n | 11 | ||||
-rw-r--r-- | library/msgcat/msgcat.tcl | 88 | ||||
-rw-r--r-- | library/msgcat1.0/msgcat.tcl | 88 | ||||
-rw-r--r-- | tests/msgcat.test | 22 |
5 files changed, 210 insertions, 11 deletions
@@ -1,3 +1,15 @@ +2000-07-17 Eric Melski <ericm@scriptics.com> + + * library/msgcat1.0/msgcat.tcl: + * doc/msgcat.n: + * tests/msgcat.test: Applied patches from Chris Nelson, to provide + the mcmset function, which allows the translator to set multiple + string translations in a single function call, rather than + requiring many calls to mcset. [RFE: 6000, 5993]. In addition, + these patches correct mcload to use utf-8 encoding on when reading + message catalog files, and provides for better default behavior + for determining the locale on a Windows system. + 2000-07-17 Mo DeJong <mdejong@redhat.com> * unix/tcl.m4 (SC_ENABLE_GCC): Don't set CC=gcc diff --git a/doc/msgcat.n b/doc/msgcat.n index e271839..4d604c9 100644 --- a/doc/msgcat.n +++ b/doc/msgcat.n @@ -25,6 +25,8 @@ msgcat \- Tcl message catalog .sp \fB::msgcat::mcset \fIlocale src-string \fR?\fItranslate-string\fR? .sp +\fB::msgcat::mcmset \fIlocale src-trans-list\fR +.sp \fB::msgcat::mcunknown \fIlocale src-string\fR .BE @@ -99,6 +101,15 @@ in the specified \fIlocale\fR. If \fItranslate-string\fR is not specified, \fIsrc-string\fR is used for both. The function returns \fItranslate-string\fR. .TP +\fB::msgcat::mcmset \fIlocale src-trans-list\fR +Sets the translation for multiple source strings in +\fIsrc-trans-list\fR in the specified \fIlocale\fR. +\fIsrc-trans-list\fR must have an even number of elements and is in +the form {\fIsrc-string translate-string\fR ?\fIsrc-string +translate-string ...\fR?} \fBmcsetcat::mcmset\fR can be significantly +faster than multiple invocations of \fBmsgcat::mcset\fR. The function +returns the number of translations set. +.TP \fB::msgcat::mcunknown \fIlocale src-string\fR This routine is called by \fB::msgcat::mc\fR in the case when a translation for \fIsrc-string\fR is not defined in the diff --git a/library/msgcat/msgcat.tcl b/library/msgcat/msgcat.tcl index fb1aeb3..fa3350c 100644 --- a/library/msgcat/msgcat.tcl +++ b/library/msgcat/msgcat.tcl @@ -10,12 +10,12 @@ # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # -# RCS: @(#) $Id: msgcat.tcl,v 1.6 2000/07/06 21:05:02 ericm Exp $ +# RCS: @(#) $Id: msgcat.tcl,v 1.7 2000/07/17 22:25:26 ericm Exp $ package provide msgcat 1.1 namespace eval msgcat { - namespace export mc mcset mclocale mcpreferences mcunknown mcmax + namespace export mc mcset mcmset mclocale mcpreferences mcunknown mcmax # Records the current locale as passed to mclocale variable locale "" @@ -134,7 +134,10 @@ proc msgcat::mcload {langdir} { set langfile [file join $langdir $p.msg] if {[file exists $langfile]} { incr x - uplevel [list source $langfile] + set fid [open $langfile "r"] + fconfigure $fid -encoding utf-8 + uplevel [list eval [read $fid]] + close $fid } } return $x @@ -164,6 +167,34 @@ proc msgcat::mcset {locale src {dest ""}} { return $dest } +# msgcat::mcmset -- +# +# Set the translation for multiple strings in a specified locale. +# +# Arguments: +# locale The locale to use. +# pairs One or more src/dest pairs (must be even length) +# +# Results: +# Returns the number of pairs processed + +proc msgcat::mcmset {locale pairs } { + + set length [llength $pairs] + if {$length % 2} { + error {bad translation list: should be "mcmset locale {src dest ...}"} + } + + set locale [string tolower $locale] + set ns [uplevel {namespace current}] + + foreach {src dest} $pairs { + set ::msgcat::msgs($locale,$ns,$src) $dest + } + + return $length +} + # msgcat::mcunknown -- # # This routine is called by msgcat::mc if a translation cannot @@ -218,6 +249,55 @@ namespace eval msgcat { if {[info exists ::env(LANG)]} { mclocale $::env(LANG) } else { - mclocale "C" + if { $tcl_platform(platform) == "windows" } { + # try to set locale depending on registry settings + # + set key {HKEY_CURRENT_USER\Control Panel\International} + if {[catch {package require registry}] || \ + [catch {registry get $key "locale"} locale]} { + mclocale "C" + } else { + + package forget registry + # + # Clean up registry value for translating LCID value + # by using only the last 2 digits, since first + # 2 digits appear to be the country... For example + # 0409 - English - United States + # 0809 - English - United Kingdom + # + set locale [string trimleft $locale "0"] + set locale [string range $locale end-1 end] + set locale [string tolower $locale] + switch -- $locale { + 01 { mclocale "ar" } + 02 { mclocale "bg" } + 03 { mclocale "ca" } + 04 { mclocale "zh" } + 05 { mclocale "cs" } + 06 { mclocale "da" } + 07 { mclocale "de" } + 08 { mclocale "el" } + 09 { mclocale "en" } + 0a { mclocale "es" } + 0b { mclocale "fi" } + 0c { mclocale "fr" } + 0d { mclocale "he" } + 0e { mclocale "hu" } + 0f { mclocale "is" } + 10 { mclocale "it" } + 11 { mclocale "ja" } + 12 { mclocale "ko" } + 13 { mclocale "da" } + 14 { mclocale "no" } + 15 { mclocale "pl" } + 16 { mclocale "pt" } + + default { mclocale "C" } + } + } + } else { + mclocale "C" + } } } diff --git a/library/msgcat1.0/msgcat.tcl b/library/msgcat1.0/msgcat.tcl index fb1aeb3..fa3350c 100644 --- a/library/msgcat1.0/msgcat.tcl +++ b/library/msgcat1.0/msgcat.tcl @@ -10,12 +10,12 @@ # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # -# RCS: @(#) $Id: msgcat.tcl,v 1.6 2000/07/06 21:05:02 ericm Exp $ +# RCS: @(#) $Id: msgcat.tcl,v 1.7 2000/07/17 22:25:26 ericm Exp $ package provide msgcat 1.1 namespace eval msgcat { - namespace export mc mcset mclocale mcpreferences mcunknown mcmax + namespace export mc mcset mcmset mclocale mcpreferences mcunknown mcmax # Records the current locale as passed to mclocale variable locale "" @@ -134,7 +134,10 @@ proc msgcat::mcload {langdir} { set langfile [file join $langdir $p.msg] if {[file exists $langfile]} { incr x - uplevel [list source $langfile] + set fid [open $langfile "r"] + fconfigure $fid -encoding utf-8 + uplevel [list eval [read $fid]] + close $fid } } return $x @@ -164,6 +167,34 @@ proc msgcat::mcset {locale src {dest ""}} { return $dest } +# msgcat::mcmset -- +# +# Set the translation for multiple strings in a specified locale. +# +# Arguments: +# locale The locale to use. +# pairs One or more src/dest pairs (must be even length) +# +# Results: +# Returns the number of pairs processed + +proc msgcat::mcmset {locale pairs } { + + set length [llength $pairs] + if {$length % 2} { + error {bad translation list: should be "mcmset locale {src dest ...}"} + } + + set locale [string tolower $locale] + set ns [uplevel {namespace current}] + + foreach {src dest} $pairs { + set ::msgcat::msgs($locale,$ns,$src) $dest + } + + return $length +} + # msgcat::mcunknown -- # # This routine is called by msgcat::mc if a translation cannot @@ -218,6 +249,55 @@ namespace eval msgcat { if {[info exists ::env(LANG)]} { mclocale $::env(LANG) } else { - mclocale "C" + if { $tcl_platform(platform) == "windows" } { + # try to set locale depending on registry settings + # + set key {HKEY_CURRENT_USER\Control Panel\International} + if {[catch {package require registry}] || \ + [catch {registry get $key "locale"} locale]} { + mclocale "C" + } else { + + package forget registry + # + # Clean up registry value for translating LCID value + # by using only the last 2 digits, since first + # 2 digits appear to be the country... For example + # 0409 - English - United States + # 0809 - English - United Kingdom + # + set locale [string trimleft $locale "0"] + set locale [string range $locale end-1 end] + set locale [string tolower $locale] + switch -- $locale { + 01 { mclocale "ar" } + 02 { mclocale "bg" } + 03 { mclocale "ca" } + 04 { mclocale "zh" } + 05 { mclocale "cs" } + 06 { mclocale "da" } + 07 { mclocale "de" } + 08 { mclocale "el" } + 09 { mclocale "en" } + 0a { mclocale "es" } + 0b { mclocale "fi" } + 0c { mclocale "fr" } + 0d { mclocale "he" } + 0e { mclocale "hu" } + 0f { mclocale "is" } + 10 { mclocale "it" } + 11 { mclocale "ja" } + 12 { mclocale "ko" } + 13 { mclocale "da" } + 14 { mclocale "no" } + 15 { mclocale "pl" } + 16 { mclocale "pt" } + + default { mclocale "C" } + } + } + } else { + mclocale "C" + } } } diff --git a/tests/msgcat.test b/tests/msgcat.test index cdb42ac..633c869 100644 --- a/tests/msgcat.test +++ b/tests/msgcat.test @@ -1,6 +1,6 @@ # Commands covered: ::msgcat::mc ::msgcat::mclocale # ::msgcat::mcpreferences ::msgcat::mcload -# ::msgcat::mcset ::msgcat::mcunknown +# ::msgcat::mcset ::msgcat::mcmset ::msgcat::mcunknown # # This file contains a collection of tests for the msgcat script library. # Sourcing this file into Tcl runs the tests and @@ -12,7 +12,7 @@ # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # -# RCS: @(#) $Id: msgcat.test,v 1.9 2000/07/06 21:05:02 ericm Exp $ +# RCS: @(#) $Id: msgcat.test,v 1.10 2000/07/17 22:25:26 ericm Exp $ if {[lsearch [namespace children] ::tcltest] == -1} { package require tcltest @@ -84,7 +84,7 @@ test msgcat-1.11 {::msgcat::mcpreferences, three elements} { } {en_us_funky en_us en} # -# Test mcset and mcc, ensuring that namespace partitioning +# Test mcset and mc, ensuring that namespace partitioning # is working. # @@ -106,6 +106,22 @@ test msgcat-2.4 {::msgcat::mcset, namespace overlap} { ::msgcat::mclocale foo_BAR namespace eval baz {::msgcat::mc con1} } {con1baz} +test msgcat-2.5 {::msgcat::mcmset, global scope} { + ::msgcat::mcmset foo_BAR { + src1 trans1 + src2 trans2 + } + ::msgcat::mc src1 +} {trans1} +test msgcat-2.6 {::msgcat::mcmset, namespace overlap} { + namespace eval bar {::msgcat::mcmset foo_BAR {con2 con2bar}} + namespace eval baz {::msgcat::mcmset foo_BAR {con2 con2baz}} +} {2} +test msgcat-2.7 {::msgcat::mcmset, namespace overlap} { + ::msgcat::mclocale foo_BAR + namespace eval baz {::msgcat::mc con2} +} {con2baz} + # # Test mcset and mc, ensuring that more specific locales |