From 23f6fa6d86cce6e3841ecdaf19957d0313407f28 Mon Sep 17 00:00:00 2001 From: oehhar Date: Fri, 26 Jun 2015 13:41:31 +0000 Subject: Manpage, callback fail calls bgerror, clean catalog on packagelocale unset --- changes | 4 + doc/msgcat.n | 304 +++++++++++++++++++++++++++++++++++++++++++--- library/msgcat/msgcat.tcl | 35 ++++-- 3 files changed, 317 insertions(+), 26 deletions(-) mode change 100644 => 100755 doc/msgcat.n diff --git a/changes b/changes index 16609e7..c4177ca 100755 --- a/changes +++ b/changes @@ -8530,3 +8530,7 @@ include ::oo::class (fellows) *** 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 + diff --git a/doc/msgcat.n b/doc/msgcat.n old mode 100644 new mode 100755 index bae6dbe..7e46528 --- a/doc/msgcat.n +++ b/doc/msgcat.n @@ -13,29 +13,43 @@ 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 @@ -46,18 +60,23 @@ Text strings are defined in a 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 @@ -84,6 +103,18 @@ 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 @@ -93,6 +124,13 @@ 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 . @@ -103,13 +141,26 @@ 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 @@ -118,6 +169,11 @@ 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? . @@ -138,16 +194,13 @@ 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 @@ -155,7 +208,6 @@ 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? . @@ -168,7 +220,18 @@ 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 @@ -217,15 +280,15 @@ When a locale is specified by the user, a 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 @@ -373,6 +436,209 @@ formatting substitution is done directly. # ... 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. diff --git a/library/msgcat/msgcat.tcl b/library/msgcat/msgcat.tcl index 228ac97..8bef081 100755 --- a/library/msgcat/msgcat.tcl +++ b/library/msgcat/msgcat.tcl @@ -4,6 +4,7 @@ # 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. # @@ -217,7 +218,7 @@ proc msgcat::mc {src args} { } # call package local or default unknown command set args [linsert $args 0 [lindex $loclist 0] $src] - switch -exact -- [Invoke unknowncmd $args $ns result] { + switch -exact -- [Invoke unknowncmd $args $ns result 1] { 0 { return [uplevel 1 [linsert $args 0 [namespace origin mcunknown]]] } 1 { return [DefaultUnknown {*}$args] } default { return $result } @@ -469,7 +470,7 @@ proc msgcat::mcpackagelocale {subcommand {locale ""}} { loaded { return [PackageLocales $ns] } isset { return [dict exists $PackageConfig loclist $ns] } set { # set a package locale or add a package locale - + # Copy the default locale if no package locale set so far if {![dict exists $PackageConfig loclist $ns]} { dict set PackageConfig loclist $ns $Loclist @@ -511,15 +512,24 @@ proc msgcat::mcpackagelocale {subcommand {locale ""}} { } } unset { # unset package locale and restore default locales - + if { ![dict exists $PackageConfig loclist $ns] } { return } - + # unset package locale set loadLocales [ListComplement\ [dict get $PackageConfig loadedlocales $ns] $LoadedLocales] dict unset PackageConfig loadedlocales $ns dict unset PackageConfig loclist $ns + # unset keys not in global loaded locales + if {[dict exists $Msgs $ns]} { + foreach locale [dict keys [dict get $Msgs $ns]] { + if {$locale ni $LoadedLocales} { + dict unset Msgs $ns $locale + } + } + } + # Add missing locales Load $ns $loadLocales } @@ -848,15 +858,17 @@ proc msgcat::Load {ns locales {callbackonly 0}} { # If not given or empty, check all registered packages. # resultname Variable to save the callback result of the last called # callback to. May be set to "" to discard the result. +# failerror (0) Fail on error if true. Otherwise call bgerror. # # Results: # Possible values: # - 0: no valid command registered # - 1: registered command was the empty string # - 2: registered command called, resultname is set +# - 3: registered command failed # If multiple commands are called, the maximum of all results is returned. -proc msgcat::Invoke {index arglist {ns ""} {resultname ""}} { +proc msgcat::Invoke {index arglist {ns ""} {resultname ""} {failerror 0}} { variable PackageConfig variable Config if {"" ne $resultname} { @@ -874,8 +886,17 @@ proc msgcat::Invoke {index arglist {ns ""} {resultname ""}} { if {"" eq $cmd} { if {$ret == 0} {set ret 1} } else { - set result [namespace inscope $ns $cmd {*}$arglist] - set ret 2 + if {$failerror} { + set result [namespace inscope $ns $cmd {*}$arglist] + set ret 2 + } elseif {1 == [catch { + set result [namespace inscope $ns $cmd {*}$arglist] + if {$ret < 2} {set ret 2} + } err derr]} { + after idle [concat [::interp bgerror ""]\ + [list $err $derr]] + set ret 3 + } } } } -- cgit v0.12