Index: ChangeLog ================================================================== --- ChangeLog +++ ChangeLog @@ -910,10 +910,18 @@ * generic/tcl.h: Make Tcl_Interp a fully opaque structure if TCL_NO_DEPRECATED is set (TIP 330 and 336). * win/nmakehlp.c: Let "nmakehlp -V" start searching digits after the found match (suggested by Harald Oehlmann). + +2012-09-19 Harald Oehlmann + + IMPLEMENTATION OF TIP#412. + + * library/msgcat/msgcat.tcl: dynamic locale change with mc file + * library/clock.tcl: load on locale change. + clock uses new msgcat features. 2012-09-07 Harald Oehlmann *** 8.6b3 TAGGED FOR RELEASE *** Index: changes ================================================================== --- changes +++ changes @@ -8115,10 +8115,12 @@ Dropped support for OS X versions less than 10.4 (Tiger) (fellows) --- Released 8.6b3, September 18, 2012 --- See ChangeLog for details --- +2012-09-19 (tip412) msgcat dynamic locale change (oehlmann) + 2012-09-20 (enhancement) full Unicode support (nijtmans) => dde 1.4.0 2012-09-20 (enhancement) update bundled zlib to 1.2.7 (nijtmans) @@ -8526,5 +8528,9 @@ 2015-03-10 (enhancement) Revise OS X notifier for better Cocoa (walzer) *** POTENTIAL INCOMPATIBILITY *** --- Released 8.6.4, March 12, 2015 --- http://core.tcl.tk/tcl/ for details + +2015-06-25 (TIP 412) msgcat dynamic locale change and package private locale +=> msgcat 1.6.0 + Index: doc/msgcat.n ================================================================== --- doc/msgcat.n +++ doc/msgcat.n @@ -11,33 +11,47 @@ .SH NAME msgcat \- Tcl message catalog .SH SYNOPSIS \fBpackage require Tcl 8.5\fR .sp -\fBpackage require msgcat 1.5\fR +\fBpackage require msgcat 1.6\fR .sp \fB::msgcat::mc \fIsrc-string\fR ?\fIarg arg ...\fR? .sp \fB::msgcat::mcmax ?\fIsrc-string src-string ...\fR? +.sp +.VS "TIP 412" +\fB::msgcat::mcexists\fR ?\fB-exactnamespace\fR? ?\fB-exactlocale\fR? \fIsrc-string\fR +.VE "TIP 412" .sp \fB::msgcat::mclocale \fR?\fInewLocale\fR? .sp \fB::msgcat::mcpreferences\fR +.sp +.VS "TIP 412" +\fB::msgcat::mcloadedlocales subcommand\fR ?\fIlocale\fR? +.VE "TIP 412" .sp \fB::msgcat::mcload \fIdirname\fR .sp \fB::msgcat::mcset \fIlocale src-string \fR?\fItranslate-string\fR? .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 ?\fIarg arg ...\fR? +.sp +.VS "TIP 412" +\fB::msgcat::mcpackagelocale subcommand\fR ?\fIlocale\fR? +.sp +\fB::msgcat::mcpackageconfig subcommand\fR \fIoption\fR ?\fIvalue\fR? +.sp +\fB::msgcat::mcforgetpackage\fR +.VE "TIP 412" .BE .SH DESCRIPTION .PP The \fBmsgcat\fR package provides a set of functions that can be used to manage multi-lingual user interfaces. @@ -44,22 +58,27 @@ Text strings are defined in a .QW "message catalog" which is independent from the application, and which can be edited or localized without modifying the application source code. New languages -or locales are provided by adding a new file to +or locales may be provided by adding a new file to the message catalog. .PP -Use of the message catalog is optional by any application -or package, but is encouraged if the application or package -wishes to be enabled for multi-lingual applications. +\fBmsgcat\fR distinguises packages by its namespace. +Each package has its own message catalog and configuration settings in \fBmsgcat\fR. +.PP +A \fIlocale\fR is a specification string describing a user language like \fBde_ch\fR for Swiss German. +In \fBmsgcat\fR, there is a global locale initialized by the system locale of the current system. +Each package may decide to use the global locale or to use a package specific locale. +.PP +The global locale may be changed on demand, for example by a user initiated language change or within a multi user application like a web server. .SH COMMANDS .TP \fB::msgcat::mc \fIsrc-string\fR ?\fIarg arg ...\fR? . Returns a translation of \fIsrc-string\fR according to the -user's current locale. If additional arguments past \fIsrc-string\fR +current locale. If additional arguments past \fIsrc-string\fR are given, the \fBformat\fR command is used to substitute the additional arguments in the translation of \fIsrc-string\fR. .RS .PP \fB::msgcat::mc\fR will search the messages defined @@ -81,10 +100,22 @@ . Given several source strings, \fB::msgcat::mcmax\fR returns the length of the longest translated string. This is useful when designing localized GUIs, which may require that all buttons, for example, be a fixed width (which will be the width of the widest button). +.TP +\fB::msgcat::mcexists\fR ?\fB-exactnamespace\fR? ?\fB-exactlocale\fR? \fIsrc-string\fR +. +.VS "TIP 412" +Return true, if there is a translation for the given \fIsrc-string\fR. +.PP +.RS +The search may be limited by the option \fB\-exactnamespace\fR to only check the current namespace and not any parent namespaces. +.PP +It may also be limited by the option \fB\-exactlocale\fR to only check the first prefered locale (e.g. first element returned by \fB::msgcat::mcpreferences\fR if global locale is used). +.RE +.VE "TIP 412" .TP \fB::msgcat::mclocale \fR?\fInewLocale\fR? . This function sets the locale to \fInewLocale\fR. If \fInewLocale\fR is omitted, the current locale is returned, otherwise the current locale @@ -91,10 +122,17 @@ is set to \fInewLocale\fR. msgcat stores and compares the locale in a case-insensitive manner, and returns locales in lowercase. The initial locale is determined by the locale specified in the user's environment. See \fBLOCALE SPECIFICATION\fR below for a description of the locale string format. +.RS +.PP +.VS "TIP 412" +If the locale is set, the preference list of locales is evaluated. +Locales in this list are loaded now, if not jet loaded. +.VE "TIP 412" +.RE .TP \fB::msgcat::mcpreferences\fR . Returns an ordered list of the locales preferred by the user, based on the user's language specification. @@ -101,25 +139,43 @@ The list is ordered from most specific to least preference. The list is derived from the current locale set in msgcat by \fB::msgcat::mclocale\fR, and cannot be set independently. For example, if the current locale is en_US_funky, then \fB::msgcat::mcpreferences\fR -returns \fB{en_US_funky en_US en {}}\fR. +returns \fB{en_us_funky en_us en {}}\fR. +.TP +\fB::msgcat:mcloadedlocales subcommand\fR ?\fIlocale\fR? +. +This group of commands manage the list of loaded locales for packages not setting a package locale. +.PP +.RS +The subcommand \fBget\fR returns the list of currently loaded locales. +.PP +The subcommand \fBpresent\fR requires the argument \fIlocale\fR and returns true, if this locale is loaded. +.PP +The subcommand \fBclear\fR removes all locales and their data, which are not in the current preference list. +.RE .TP \fB::msgcat::mcload \fIdirname\fR . +.VS "TIP 412" Searches the specified directory for files that match -the language specifications returned by \fB::msgcat::mcpreferences\fR -(note that these are all lowercase), extended by the file extension +the language specifications returned by \fB::msgcat::mcloadedlocales get\fR +(or \fBmsgcat::mcpackagelocale preferences\fR if a package locale is set) (note that these are all lowercase), extended by the file extension .QW .msg . Each matching file is read in order, assuming a UTF-8 encoding. The file contents are then evaluated as a Tcl script. This means that Unicode characters may be present in the message file either directly in their UTF-8 encoded form, or by use of the backslash-u quoting recognized by Tcl evaluation. The number of message files which matched the specification and were loaded is returned. +.RS +.PP +In addition, the given folder is stored in the \fBmsgcat\fR package configuration option \fImcfolder\fR to eventually load message catalog files required by a locale change. +.VE "TIP 412" +.RE .TP \fB::msgcat::mcset \fIlocale src-string \fR?\fItranslate-string\fR? . Sets the translation for \fIsrc-string\fR to \fItranslate-string\fR in the specified \fIlocale\fR and the current namespace. If @@ -136,28 +192,24 @@ 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 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::mcflmset\fR can be significantly faster than multiple invocations of \fB::msgcat::mcflset\fR. The function returns the number of translations set. -.VE "TIP 404" .TP \fB::msgcat::mcunknown \fIlocale src-string\fR ?\fIarg arg ...\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 @@ -166,11 +218,22 @@ procedure can be redefined by the application, for example to log error messages for each unknown string. The \fB::msgcat::mcunknown\fR procedure is invoked at the same stack context as the call to \fB::msgcat::mc\fR. The return value of \fB::msgcat::mcunknown\fR is used as the return value for the call -to \fB::msgcat::mc\fR. +to \fB::msgcat::mc\fR. +.VS "TIP 412" +.RS +.PP +Note that this routine is only called if the concerned package did not set a package locale unknown command name. +.RE +.TP +\fB::msgcat::mcforgetpackage\fR +. +The calling package clears all its state within the \fBmsgcat\fR package including all settings and translations. +.VE "TIP 412" +.PP .SH "LOCALE SPECIFICATION" .PP The locale is specified to \fBmsgcat\fR by a locale string passed to \fB::msgcat::mclocale\fR. The locale string consists of @@ -215,19 +278,19 @@ When a locale is specified by the user, a .QW "best match" search is performed during string translation. For example, if a user specifies en_GB_Funky, the locales -.QW en_GB_Funky , -.QW en_GB , +.QW en_gb_funky , +.QW en_gb , .QW en and .MT (the empty string) are searched in order until a matching translation string is found. If no translation string is available, then -\fB::msgcat::mcunknown\fR is called. +the unknown handler is called. .SH "NAMESPACES AND MESSAGE CATALOGS" .PP Strings stored in the message catalog are stored relative to the namespace from which they were added. This allows multiple packages to use the same strings without fear @@ -371,10 +434,213 @@ .CS \fBmsgcat::mc\fR {Produced %1$d at %2$s} $num $city # ... where that key is mapped to one of the # human-oriented versions by \fBmsgcat::mcset\fR .CE +.VS "TIP 412" +.SH Package private locale +.PP +A package using \fBmsgcat\fR may choose to use its own package private +locale and its own set of loaded locales, independent to the global +locale set by \fB::msgcat::mclocale\fR. +.PP +This allows a package to change its locale without causing any locales load or removal in other packages and not to invoke the global locale change callback (see below). +.PP +This action is controled by the following ensemble: +.TP +\fB::msgcat::mcpackagelocale set\fR ?\fIlocale\fR? +. +Set or change a package private locale. +The package private locale is set to the given \fIlocale\fR if the \fIlocale\fR is given. +If the option \fIlocale\fR is not given, the package is set to package private locale mode, but no locale is changed (e.g. if the global locale was valid for the package before, it is copied to the package private locale). +.PP +.RS +This command may cause the load of locales. +.RE +.TP +\fB::msgcat::mcpackagelocale get\fR +. +Return the package private locale or the global locale, if no package private locale is set. +.TP +\fB::msgcat::mcpackagelocale preferences\fR +. +Return the package private preferences or the global preferences, +if no package private locale is set. +.TP +\fB::msgcat::mcpackagelocale loaded\fR +. +Return the list of locales loaded for this package. +.TP +\fB::msgcat::mcpackagelocale isset\fR +. +Returns true, if a package private locale is set. +.TP +\fB::msgcat::mcpackagelocale unset\fR +. +Unset the package private locale and use the globale locale. +Load and remove locales to adjust the list of loaded locales for the +package to the global loaded locales list. +.TP +\fB::msgcat::mcpackagelocale present\fR \fIlocale\fR +. +Returns true, if the given locale is loaded for the package. +.TP +\fB::msgcat::mcpackagelocale clear\fR +. +Clear any loaded locales of the package not present in the package preferences. +.PP +.SH Changing package options +.PP +Each package using msgcat has a set of options within \fBmsgcat\fR. +The package options are described in the next sectionPackage options. +Each package option may be set or unset individually using the following ensemble: +.TP +\fB::msgcat::mcpackageconfig get\fR \fIoption\fR +. +Return the current value of the given \fIoption\fR. +This call returns an error if the option is not set for the package. +.TP +\fB::msgcat::mcpackageconfig isset\fR \fIoption\fR +. +Returns 1, if the given \fIoption\fR is set for the package, 0 otherwise. +.TP +\fB::msgcat::mcpackageconfig set\fR \fIoption\fR \fIvalue\fR +. +Set the given \fIoption\fR to the given \fIvalue\fR. +This may invoke additional actions in dependency of the \fIoption\fR. +The return value is 0 or the number of loaded packages for the option \fBmcfolder\fR. +.TP +\fB::msgcat::mcpackageconfig unset\fR \fIoption\fR +. +Unsets the given \fIoption\fR for the package. +No action is taken if the \fIoption\fR is not set for the package. +The empty string is returned. +.SS Package options +.PP +The following package options are available for each package: +.TP +\fBmcfolder\fR +. +This is the message folder of the package. This option is set by mcload and by the subcommand set. Both are identical and both return the number of loaded message catalog files. +.RS +.PP +Setting or changing this value will load all locales contained in the preferences valid for the package. This implies also to invoke any set loadcmd (see below). +.PP +Unsetting this value will disable message file load for the package. +.RE +.TP +\fBloadcmd\fR +. +This callback is invoked before a set of message catalog files are loaded for the package which has this property set. +.PP +.RS +This callback may be used to do any preparation work for message file load or to get the message data from another source like a data base. In this case, no message files are used (mcfolder is unset). +.PP +See section \fBcallback invocation\fR below. +The parameter list appended to this callback is the list of locales to load. +.PP +If this callback is changed, it is called with the preferences valid for the package. +.RE +.TP +\fBchangecmd\fR +. +This callback is invoked when a default local change was performed. Its purpose is to allow a package to update any dependency on the default locale like showing the GUI in another language. +.PP +.RS +See the callback invocation section below. +The parameter list appended to this callback is \fBmcpreferences\fR. +The registered callbacks are invoked in no particular order. +.RE +.TP +\fBunknowncmd\fR +. +Use a package locale mcunknown procedure instead of the standard version supplied by the msgcat package (msgcat::mcunknown). +.PP +.RS +The called procedure must return the formatted message which will finally be returned by msgcat::mc. +.PP +A generic unknown handler is used if set to the empty string. This consists in returning the key if no arguments are given. With given arguments, format is used to process the arguments. +.PP +See section \fBcallback invocation\fR below. +The appended arguments are identical to \fB::msgcat::mcunknown\fR. +.RE +.SS Callback invocation +A package may decide to register one or multiple callbacks, as described above. +.PP +Callbacks are invoked, if: +.PP +1. the callback command is set, +.PP +2. the command is not the empty string, +.PP +3. the registering namespace exists. +.PP +If a called routine fails with an error, the \fBbgerror\fR routine for the interpreter is invoked after command completion. +Only exception is the callback \fBunknowncmd\fR, where an error causes the invoking \fBmc\fR-command to fail with that error. +.PP +.SS Examples +Packages which display a GUI may update their widgets when the global locale changes. +To register to a callback, use: +.CS +namespace eval gui { + msgcat::mcpackageconfig changecmd updateGUI + + proc updateGui args { + puts "New locale is '[lindex $args 0]'." + } +} +% msgcat::mclocale fr +fr +% New locale is 'fr'. +.CE +.PP +If locales (or additional locales) are contained in another source like a data base, a package may use the load callback and not mcload: +.CS +namespace eval db { + msgcat::mcpackageconfig loadcmd loadMessages + + proc loadMessages args { + foreach locale $args { + if {[LocaleInDB $locale]} { + msgcat::mcmset $locale [GetLocaleList $locale] + } + } + } +} +.CE +.PP +The \fBclock\fR command implementation uses \fBmsgcat\fR with a package locale to implement the command line parameter \fB-locale\fR. +Here are some sketches of the implementation: +.PP +First, a package locale is initialized and the generic unknown function is desactivated: +.CS +msgcat::mcpackagelocale set +msgcat::mcpackageconfig unknowncmd "" +.CE +As an example, the user requires the week day in a certain locale as follows: +.CS +clock format clock seconds -format %A -locale fr +.CE +\fBclock\fR sets the package locale to \fBfr\fR and looks for the day name as follows: +.CS +msgcat::mcpackagelocale set $locale +return [lindex [msgcat::mc DAYS_OF_WEEK_FULL] $day] +### Returns "mercredi" +.CE +Within \fBclock\fR, some message-catalog items are heavy in computation and thus are dynamically cached using: +.CS +proc ::tcl::clock::LocalizeFormat { locale format } { + set key FORMAT_$format + if { [::msgcat::mcexists -exactlocale -exactnamespace $key] } { + return [mc $key] + } + #...expensive computation of format clipped... + mcset $locale $key $format + return $format +} +.CE +.VE "TIP 412" .SH CREDITS .PP The message catalog code was developed by Mark Harrison. .SH "SEE ALSO" format(n), scan(n), namespace(n), package(n) Index: library/clock.tcl ================================================================== --- library/clock.tcl +++ library/clock.tcl @@ -17,11 +17,11 @@ # We must have message catalogs that support the root locale, and we need # access to the Registry on Windows systems. uplevel \#0 { - package require msgcat 1.4 + package require msgcat 1.6 if { $::tcl_platform(platform) eq {windows} } { if { [catch { package require registry 1.1 }] } { namespace eval ::tcl::clock [list variable NoRegistry {}] } } @@ -58,10 +58,12 @@ # Import the message catalog commands that we use. namespace import ::msgcat::mcload namespace import ::msgcat::mclocale + namespace import ::msgcat::mc + namespace import ::msgcat::mcpackagelocale } #---------------------------------------------------------------------- # @@ -104,10 +106,14 @@ set TZData(:UTC) $TZData(:Etc/UTC) set TZData(:localtime) {} } InitTZData + mcpackagelocale set {} + ::msgcat::mcpackageconfig set mcfolder [file join $LibDir msgs] + ::msgcat::mcpackageconfig set unknowncmd "" + # Define the message catalog for the root locale. ::msgcat::mcmset {} { AM {am} BCE {B.C.E.} @@ -247,11 +253,10 @@ } # Define the directories for time zone data and message catalogs. variable DataDir [file join $LibDir tzdata] - variable MsgDir [file join $LibDir msgs] # Number of days in the months, in common years and leap years. variable DaysInRomanMonthInCommonYear \ { 31 28 31 30 31 30 31 31 30 31 30 31 } @@ -621,15 +626,10 @@ # names and whose values are pairs # comprising regexes matching numerals # in the given locales and dictionaries # mapping the numerals to their numeric # values. - variable McLoaded {}; # Dictionary whose keys are locales - # in which [mcload] has been executed - # and whose values are second-level - # dictionaries indexed by message - # name and giving message text. # variable CachedSystemTimeZone; # If 'CachedSystemTimeZone' exists, # it contains the value of the # system time zone, as determined from # the environment. variable TimeZoneBad {}; # Dictionary whose keys are time zone @@ -657,10 +657,11 @@ # user documentation to see what it does. # #---------------------------------------------------------------------- proc ::tcl::clock::format { args } { + variable FormatProc variable TZData lassign [ParseFormatArgs {*}$args] format locale timezone set locale [string tolower $locale] @@ -690,10 +691,11 @@ set FormatProc($procName) \ [ParseClockFormatFormat $procName $format $locale] } return [$procName $clockval $timezone] + } #---------------------------------------------------------------------- # # ParseClockFormatFormat -- @@ -708,31 +710,26 @@ # Returns the name of the newly-built procedure. # #---------------------------------------------------------------------- proc ::tcl::clock::ParseClockFormatFormat {procName format locale} { + if {[namespace which $procName] ne {}} { return $procName } # Map away the locale-dependent composite format groups - EnterLocale $locale oldLocale + EnterLocale $locale # Change locale if a fresh locale has been given on the command line. try { return [ParseClockFormatFormat2 $format $locale $procName] } trap CLOCK {result opts} { dict unset opts -errorinfo return -options $opts $result - } finally { - # Restore the locale - - if { [info exists oldLocale] } { - mclocale $oldLocale - } } } proc ::tcl::clock::ParseClockFormatFormat2 {format locale procName} { set didLocaleEra 0 @@ -1179,10 +1176,11 @@ # documentation to see what it does. # #---------------------------------------------------------------------- proc ::tcl::clock::scan { args } { + set format {} # Check the count of args if { [llength $args] < 1 || [llength $args] % 2 != 1 } { @@ -1262,28 +1260,21 @@ return [FreeScan $string $base $timezone $locale] } # Change locale if a fresh locale has been given on the command line. - EnterLocale $locale oldLocale + EnterLocale $locale try { # Map away the locale-dependent composite format groups set scanner [ParseClockScanFormat $format $locale] return [$scanner $string $base $timezone] } trap CLOCK {result opts} { # Conceal location of generation of expected errors - dict unset opts -errorinfo return -options $opts $result - } finally { - # Restore the locale - - if { [info exists oldLocale] } { - mclocale $oldLocale - } } } #---------------------------------------------------------------------- # @@ -1302,10 +1293,11 @@ # the epoch # #---------------------------------------------------------------------- proc ::tcl::clock::FreeScan { string base timezone locale } { + variable TZData # Get the data for time changes in the given zone try { @@ -1429,10 +1421,11 @@ }] dict set date2 tzName $timezone set date2 [ConvertLocalToUTC $date2[set date2 {}] $TZData($timezone) \ 2361222] set seconds [dict get $date2 seconds] + } # Do relative month if { [llength $parseOrdinalMonth] > 0 } { @@ -2153,10 +2146,11 @@ #---------------------------------------------------------------------- proc ::tcl::clock::MakeUniquePrefixRegexp { successors uniquePrefixMapping prefixString } { + # Get the characters that may follow the current prefix string set schars [lsort -ascii [dict keys [dict get $successors $prefixString]]] if { [llength $schars] == 0 } { return {} @@ -2222,10 +2216,11 @@ # None. # #---------------------------------------------------------------------- proc ::tcl::clock::MakeParseCodeFromFields { dateFields parseActions } { + set currPrio 999 set currFieldPos [list] set currCodeBurst { error "in ::tcl::clock::MakeParseCodeFromFields: can't happen" } @@ -2295,30 +2290,20 @@ # # Switch [mclocale] to a given locale if necessary # # Parameters: # locale -- Desired locale -# oldLocaleVar -- Name of a variable in caller's scope that -# tracks the previous locale name. # # Results: # Returns the locale that was previously current. # # Side effects: -# Does [mclocale]. If necessary, uses [mcload] to load the designated -# locale's files, and tracks that it has done so in the 'McLoaded' -# variable. +# Does [mclocale]. If necessary, loades the designated locale's files. # #---------------------------------------------------------------------- -proc ::tcl::clock::EnterLocale { locale oldLocaleVar } { - upvar 1 $oldLocaleVar oldLocale - - variable MsgDir - variable McLoaded - - set oldLocale [mclocale] +proc ::tcl::clock::EnterLocale { locale } { if { $locale eq {system} } { if { $::tcl_platform(platform) ne {windows} } { # On a non-windows platform, the 'system' locale is the same as # the 'current' locale @@ -2327,37 +2312,26 @@ # On a windows platform, the 'system' locale is adapted from the # 'current' locale by applying the date and time formats from the # Control Panel. First, load the 'current' locale if it's not yet # loaded - if {![dict exists $McLoaded $oldLocale] } { - mcload $MsgDir - dict set McLoaded $oldLocale {} - } + mcpackagelocale set [mclocale] # Make a new locale string for the system locale, and get the # Control Panel information - set locale ${oldLocale}_windows - if { ![dict exists $McLoaded $locale] } { + set locale [mclocale]_windows + if { $locale ni [::msgcat::mcpackagelocale loaded] } { LoadWindowsDateTimeFormats $locale - dict set McLoaded $locale {} } } } if { $locale eq {current}} { - set locale $oldLocale - unset oldLocale - } elseif { $locale eq $oldLocale } { - unset oldLocale - } else { - mclocale $locale - } - if { ![dict exists $McLoaded $locale] } { - mcload $MsgDir - dict set McLoaded $locale {} - } + set locale [mclocale] + } + # Eventually load the locale + mcpackagelocale set $locale } #---------------------------------------------------------------------- # # LoadWindowsDateTimeFormats -- @@ -2480,10 +2454,11 @@ catch { ::msgcat::mcset $locale LOCALE_DATE_TIME_FORMAT "$ldatefmt $timefmt" } return + } #---------------------------------------------------------------------- # # LocalizeFormat -- @@ -2503,17 +2478,17 @@ # None. # #---------------------------------------------------------------------- proc ::tcl::clock::LocalizeFormat { locale format } { - variable McLoaded - if { [dict exists $McLoaded $locale FORMAT $format] } { - return [dict get $McLoaded $locale FORMAT $format] + # message catalog key to cache this format + set key FORMAT_$format + + if { [::msgcat::mcexists -exactlocale -exactnamespace $key] } { + return [mc $key] } - set inFormat $format - # Handle locale-dependent format groups by mapping them out of the format # string. Note that the order of the [string map] operations is # significant because later formats can refer to later ones; for example # %c can refer to %X, which in turn can refer to %T. @@ -2532,11 +2507,11 @@ lappend list %Ex [string map $list [mc LOCALE_DATE_FORMAT]] lappend list %c [string map $list [mc DATE_TIME_FORMAT]] lappend list %Ec [string map $list [mc LOCALE_DATE_TIME_FORMAT]] set format [string map $list $format] - dict set McLoaded $locale FORMAT $inFormat $format + ::msgcat::mcset $locale $key $format return $format } #---------------------------------------------------------------------- # @@ -3131,11 +3106,10 @@ } then { return -code error \ -errorcode [list CLOCK badTimeZone $timezone] \ "time zone \"$timezone\" not found" } - } elseif { ![catch {ParsePosixTimeZone $timezone} tzfields] } { # This looks like a POSIX time zone - try to process it if { [catch {ProcessPosixTimeZone $tzfields} data opts] } { if { [lindex [dict get $opts -errorcode] 0] eq {CLOCK} } { @@ -3899,17 +3873,19 @@ # time is relative to the wall clock, not UTC. # #---------------------------------------------------------------------- proc ::tcl::clock::DeterminePosixDSTTime { z bound y } { + variable FEB_28 # Determine the start or end day of DST set date [dict create era CE year $y] set doy [dict get $z ${bound}DayOfYear] if { $doy ne {} } { + # Time was specified as a day of the year if { [dict get $z ${bound}J] ne {} && [IsGregorianLeapYear $y] && ( $doy > $FEB_28 ) } { @@ -4317,11 +4293,11 @@ return -code error "expected boolean value but got \"$gmt\"" } elseif { $gmt } { set timezone :GMT } - EnterLocale $locale oldLocale + EnterLocale $locale set changeover [mc GREGORIAN_CHANGE_DATE] if {[catch {SetupTimeZone $timezone} retval opts]} { dict unset opts -errorinfo @@ -4369,16 +4345,10 @@ return $clockval } trap CLOCK {result opts} { # Conceal the innards of [clock] when it's an expected error dict unset opts -errorinfo return -options $opts $result - } finally { - # Restore the locale - - if { [info exists oldLocale] } { - mclocale $oldLocale - } } } #---------------------------------------------------------------------- # @@ -4447,10 +4417,11 @@ }] set date [ConvertLocalToUTC $date[set date {}] $TZData($timezone) \ $changeover] return [dict get $date seconds] + } #---------------------------------------------------------------------- # # AddDays -- @@ -4497,42 +4468,11 @@ }] set date [ConvertLocalToUTC $date[set date {}] $TZData($timezone) \ $changeover] return [dict get $date seconds] -} - -#---------------------------------------------------------------------- -# -# mc -- -# -# Wrapper around ::msgcat::mc that caches the result according to the -# locale. -# -# Parameters: -# Accepts the name of the message to retrieve. -# -# Results: -# Returns the message text. -# -# Side effects: -# Caches the message text. -# -# Notes: -# Only the single-argument version of [mc] is supported. -# -#---------------------------------------------------------------------- - -proc ::tcl::clock::mc { name } { - variable McLoaded - set Locale [mclocale] - if { [dict exists $McLoaded $Locale $name] } { - return [dict get $McLoaded $Locale $name] - } - set val [::msgcat::mc $name] - dict set McLoaded $Locale $name $val - return $val + } #---------------------------------------------------------------------- # # ClearCaches -- @@ -4551,11 +4491,10 @@ #---------------------------------------------------------------------- proc ::tcl::clock::ClearCaches {} { variable FormatProc variable LocaleNumeralCache - variable McLoaded variable CachedSystemTimeZone variable TimeZoneBad foreach p [info procs [namespace current]::scanproc'*] { rename $p {} @@ -4564,10 +4503,9 @@ rename $p {} } catch {unset FormatProc} set LocaleNumeralCache {} - set McLoaded {} catch {unset CachedSystemTimeZone} set TimeZoneBad {} InitTZData } Index: library/msgcat/msgcat.tcl ================================================================== --- library/msgcat/msgcat.tcl +++ library/msgcat/msgcat.tcl @@ -2,36 +2,44 @@ # # This file defines various procedures which implement a # message catalog facility for Tcl programs. It should be # loaded with the command "package require msgcat". # +# Copyright (c) 2010-2015 by Harald Oehlmann. # Copyright (c) 1998-2000 by Ajuba Solutions. # Copyright (c) 1998 by Mark Harrison. # # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. 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.5.2 +package provide msgcat 1.6.0 namespace eval msgcat { - namespace export mc mcload mclocale mcmax mcmset mcpreferences mcset \ - mcunknown mcflset mcflmset - - # Records the current locale as passed to mclocale - variable Locale "" + namespace export mc mcexists mcload mclocale mcmax mcmset mcpreferences mcset\ + mcunknown mcflset mcflmset mcloadedlocales mcforgetpackage\ + mcpackageconfig mcpackagelocale # Records the list of locales to search variable Loclist {} + + # List of currently loaded locales + variable LoadedLocales {} # Records the locale of the currently sourced message catalogue file variable FileLocale + + # Configuration values per Package (e.g. client namespace). + # The dict key is of the form "