Subject: Commit of Harald Oehlmann's TIP 404 patch without TIP 399 pieces and with some added documentation. No tests for new functionality yet.

From: dkf <[email-masked]>
Date: Mon, 27 Aug 2012 17:24:55 +0000

---
 doc/msgcat.n                | 28 +++++++++++++++-
 library/msgcat/msgcat.tcl   | 78 +++++++++++++++++++++++++++++++++++++++++++--
 library/msgcat/pkgIndex.tcl |  2 +-
 3 files changed, 103 insertions(+), 5 deletions(-)

diff --git a/doc/msgcat.n b/doc/msgcat.n
index 595c85f..d65563a 100644
--- a/doc/msgcat.n
+++ b/doc/msgcat.n
@@ -13,7 +13,7 @@ msgcat \- Tcl message catalog
 .SH SYNOPSIS
 \fBpackage require Tcl 8.5\fR
 .sp
-\fBpackage require msgcat 1.4.5\fR
+\fBpackage require msgcat 1.5.0\fR
 .sp
 \fB::msgcat::mc \fIsrc-string\fR ?\fIarg arg ...\fR?
 .sp
@@ -29,6 +29,12 @@ msgcat \- Tcl message catalog
 .sp
 \fB::msgcat::mcmset \fIlocale src-trans-list\fR
 .sp
+.VS "TIP 404"
+\fB::msgcat::mcflset \fIsrc-string \fR?\fItranslate-string\fR?
+.sp
+\fB::msgcat::mcflmset \fIsrc-trans-list\fR
+.VE "TIP 404"
+.sp
 \fB::msgcat::mcunknown \fIlocale src-string\fR
 .BE
 .SH DESCRIPTION
@@ -131,6 +137,26 @@ translate-string ...\fR?} \fB::msgcat::mcmset\fR can be significantly
 faster than multiple invocations of \fB::msgcat::mcset\fR. The function
 returns the number of translations set.
 .TP
+\fB::msgcat::mcflset \fIsrc-string \fR?\fItranslate-string\fR?
+.VS "TIP 404"
+Sets the translation for \fIsrc-string\fR to \fItranslate-string\fR in the the
+current namespace for the locale implied by the name of the message catalog
+being loaded via \fB::msgcat::mcload\fR.  If \fItranslate-string\fR is not
+specified, \fIsrc-string\fR is used for both.  The function returns
+\fItranslate-string\fR.
+.VE "TIP 404"
+.TP
+\fB::msgcat::mcflmset \fIsrc-trans-list\fR
+.VS "TIP 404"
+Sets the translation for multiple source strings in \fIsrc-trans-list\fR in
+the current namespace for the locale implied by the name of the message
+catalog being loaded via \fB::msgcat::mcload\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?}
+\fB::msgcat::mcmset\fR can be significantly faster than multiple invocations
+of \fB::msgcat::mcset\fR. The function returns the number of translations set.
+.VE "TIP 404"
+.TP
 \fB::msgcat::mcunknown \fIlocale src-string\fR
 .
 This routine is called by \fB::msgcat::mc\fR in the case when
diff --git a/library/msgcat/msgcat.tcl b/library/msgcat/msgcat.tcl
index 3377b47..6dd44d2 100644
--- a/library/msgcat/msgcat.tcl
+++ b/library/msgcat/msgcat.tcl
@@ -13,11 +13,11 @@
 package require Tcl 8.5
 # When the version number changes, be sure to update the pkgIndex.tcl file,
 # and the installation directory in the Makefiles.
-package provide msgcat 1.4.5
+package provide msgcat 1.5.0
 
 namespace eval msgcat {
     namespace export mc mcload mclocale mcmax mcmset mcpreferences mcset \
-	    mcunknown
+	    mcunknown mcflset mcflmset
 
     # Records the current locale as passed to mclocale
     variable Locale ""
@@ -25,6 +25,12 @@ namespace eval msgcat {
     # Records the list of locales to search
     variable Loclist {}
 
+    # Records the locale of the currently sourced message catalogue file; this
+    # would be problematic if anyone were to recursively load a message
+    # catalog for a different locale from inside a catalog, but that's not a
+    # case that we really need to worry about.
+    variable FileLocale
+
     # Records the mapping between source strings and translated strings.  The
     # dict key is of the form "<locale> <namespace> <src>", where locale and
     # namespace should be themselves dict values and the value is
@@ -277,6 +283,7 @@ proc msgcat::mcpreferences {} {
 #	Returns the number of message catalogs that were loaded.
 
 proc msgcat::mcload {langdir} {
+    variable FileLocale
     set x 0
     foreach p [mcpreferences] {
 	if { $p eq {} } {
@@ -285,7 +292,12 @@ proc msgcat::mcload {langdir} {
 	set langfile [file join $langdir $p.msg]
 	if {[file exists $langfile]} {
 	    incr x
+	    set FileLocale [string tolower [file tail [file rootname $langfile]]]
+	    if {"root" eq $FileLocale} {
+		set FileLocale ""
+	    }
 	    uplevel 1 [list ::source -encoding utf-8 $langfile]
+	    unset FileLocale
 	}
     }
     return $x
@@ -318,6 +330,35 @@ proc msgcat::mcset {locale src {dest ""}} {
     return $dest
 }
 
+# msgcat::mcflset --
+#
+#	Set the translation for a given string in the current file locale.
+#
+# Arguments:
+#	src		The source string.
+#	dest		(Optional) The translated string.  If omitted,
+#			the source string is used.
+#
+# Results:
+#	Returns the new locale.
+
+proc msgcat::mcflset {src {dest ""}} {
+    variable FileLocale
+    variable Msgs
+
+    if {![info exists FileLocale]} {
+	return -code error \
+	    "must only be used inside a message catalog loaded with ::msgcat::mcload"
+    }
+    if {[llength [info level 0]] == 2} { ;# dest not specified
+	set dest $src
+    }
+
+    set ns [uplevel 1 [list ::namespace current]]
+    dict set Msgs $FileLocale $ns $src $dest
+    return $dest
+}
+
 # msgcat::mcmset --
 #
 #	Set the translation for multiple strings in a specified locale.
@@ -345,7 +386,38 @@ proc msgcat::mcmset {locale pairs } {
 	dict set Msgs $locale $ns $src $dest
     }
 
-    return $length
+    return [expr {$length / 2}]
+}
+
+# msgcat::mcflmset --
+#
+#	Set the translation for multiple strings in the mc file locale.
+#
+# Arguments:
+#	pairs		One or more src/dest pairs (must be even length)
+#
+# Results:
+#	Returns the number of pairs processed
+
+proc msgcat::mcflmset {pairs} {
+    variable FileLocale
+    variable Msgs
+
+    if {![info exists FileLocale]} {
+	return -code error \
+	    "must only be used inside a message catalog loaded with ::msgcat::mcload"
+    }
+    set length [llength $pairs]
+    if {$length % 2} {
+	return -code error "bad translation list:\
+		should be \"[lindex [info level 0] 0] locale {src dest ...}\""
+    }
+
+    set ns [uplevel 1 [list ::namespace current]]
+    foreach {src dest} $pairs {
+	dict set Msgs $FileLocale $ns $src $dest
+    }
+    return [expr {$length / 2}]
 }
 
 # msgcat::mcunknown --
diff --git a/library/msgcat/pkgIndex.tcl b/library/msgcat/pkgIndex.tcl
index 60c2d3c..832bf81 100644
--- a/library/msgcat/pkgIndex.tcl
+++ b/library/msgcat/pkgIndex.tcl
@@ -1,2 +1,2 @@
 if {![package vsatisfies [package provide Tcl] 8.5]} {return}
-package ifneeded msgcat 1.4.5 [list source [file join $dir msgcat.tcl]]
+package ifneeded msgcat 1.5.0 [list source [file join $dir msgcat.tcl]]
-- 
cgit v0.12