package require msgcat 1.6
::msgcat::mc src-string ?arg arg ...?
::msgcat::mcmax ?src-string src-string ...?
::msgcat::mcexists ?-exactnamespace? ?-exactlocale? src-string
::msgcat::mclocale ?newLocale?
::msgcat::mcpreferences
::msgcat::mcloadedlocales subcommand ?locale?
::msgcat::mcload dirname
::msgcat::mcset locale src-string ?translate-string?
::msgcat::mcmset locale src-trans-list
::msgcat::mcflset src-string ?translate-string?
::msgcat::mcflmset src-trans-list
::msgcat::mcunknown locale src-string ?arg arg ...?
::msgcat::mcpackagelocale subcommand ?locale?
::msgcat::mcpackageconfig subcommand option ?value?
::msgcat::mcforgetpackage
The msgcat package provides a set of functions that can be used to manage multi-lingual user interfaces. Text strings are defined in a “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 may be provided by adding a new file to the message catalog.
msgcat distinguises packages by its namespace. Each package has its own message catalog and configuration settings in msgcat.
A locale is a specification string describing a user language like de_ch for Swiss German. In msgcat, 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.
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.
::msgcat::mc will search the messages defined in the current namespace for a translation of src-string; if none is found, it will search in the parent of the current namespace, and so on until it reaches the global namespace. If no translation string exists, ::msgcat::mcunknown is called and the string returned from ::msgcat::mcunknown is returned.
::msgcat::mc is the main function used to localize an application. Instead of using an English string directly, an application can pass the English string through ::msgcat::mc and use the result. If an application is written for a single language in this fashion, then it is easy to add support for additional languages later simply by defining new message catalog entries.
It may also be limited by the option -exactlocale to only check the first prefered locale (e.g. first element returned by ::msgcat::mcpreferences if global locale is used).
If the locale is set, the preference list of locales is evaluated. Locales in this list are loaded now, if not jet loaded.
The subcommand present requires the argument locale and returns true, if this locale is loaded.
The subcommand clear removes all locales and their data, which are not in the current preference list.
In addition, the given folder is stored in the msgcat package configuration option mcfolder to eventually load message catalog files required by a locale change.
Note that this routine is only called if the concerned package did not set a package locale unknown command name.
The locale is specified to msgcat by a locale string passed to ::msgcat::mclocale. The locale string consists of a language code, an optional country code, and an optional system-specific code, each separated by “_” The country and language codes are specified in standards ISO-639 and ISO-3166. For example, the locale “en” specifies English and “en_US” specifies U.S. English.
When the msgcat package is first loaded, the locale is initialized according to the user's environment. The variables env(LC_ALL), env(LC_MESSAGES), and env(LANG) are examined in order. The first of them to have a non-empty value is used to determine the initial locale. The value is parsed according to the XPG4 pattern
language[_country][.codeset][@modifier]
to extract its parts. The initial locale is then set by calling ::msgcat::mclocale with the argument
language[_country][_modifier]
On Windows and Cygwin, if none of those environment variables is set, msgcat will attempt to extract locale information from the registry. From Windows Vista on, the RFC4747 locale name "lang-script-country-options" is transformed to the locale as "lang_country_script" (Example: sr-Latn-CS -> sr_cs_latin). For Windows XP, the language id is transformed analoguously (Example: 0c1a -> sr_yu_cyrillic). If all these attempts to discover an initial locale from the user's environment fail, msgcat defaults to an initial locale of “C”
When a locale is specified by the user, a “best match” search is performed during string translation. For example, if a user specifies en_GB_Funky, the locales “en_gb_funky” “en_gb” “en” and .MT (the empty string) are searched in order until a matching translation string is found. If no translation string is available, then the unknown handler is called.
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 of collisions with other packages. It also allows the source string to be shorter and less prone to typographical error.
For example, executing the code
::msgcat::mcset en hello "hello from ::" namespace eval foo { ::msgcat::mcset en hello "hello from ::foo" } puts [::msgcat::mc hello] namespace eval foo {puts [::msgcat::mc hello]}
will print
hello from :: hello from ::foo
When searching for a translation of a message, the message catalog will search first the current namespace, then the parent of the current namespace, and so on until the global namespace is reached. This allows child namespaces to “inherit” messages from their parent namespace.
For example, executing (in the “en” locale) the code
::msgcat::mcset en m1 ":: message1" ::msgcat::mcset en m2 ":: message2" ::msgcat::mcset en m3 ":: message3" namespace eval ::foo { ::msgcat::mcset en m2 "::foo message2" ::msgcat::mcset en m3 "::foo message3" } namespace eval ::foo::bar { ::msgcat::mcset en m3 "::foo::bar message3" } namespace import ::msgcat::mc puts "[mc m1]; [mc m2]; [mc m3]" namespace eval ::foo {puts "[mc m1]; [mc m2]; [mc m3]"} namespace eval ::foo::bar {puts "[mc m1]; [mc m2]; [mc m3]"}
will print
:: message1; :: message2; :: message3 :: message1; ::foo message2; ::foo message3 :: message1; ::foo message2; ::foo::bar message3
Message files can be located in any directory, subject to the following conditions:
es.msg \(em spanish en_gb.msg \(em United Kingdom English
Exception: The message file for the root locale .MT is called “ROOT.msg” This exception is made so as not to cause peculiar behavior, such as marking the message file as “hidden” on Unix file systems.
namespace eval ::mypackage { ::msgcat::mcflset "Free Beer" "Cerveza Gratis" }
If a package is installed into a subdirectory of the tcl_pkgPath and loaded via package require, the following procedure is recommended.
# load language files, stored in msgs subdirectory ::msgcat::mcload [file join [file dirname [info script]] msgs]
It is possible that a message string used as an argument to format might have positionally dependent parameters that might need to be repositioned. For example, it might be syntactically desirable to rearrange the sentence structure while translating.
format "We produced %d units in location %s" $num $city format "In location %s we produced %d units" $city $num
This can be handled by using the positional parameters:
format "We produced %1\$d units in location %2\$s" $num $city format "In location %2\$s we produced %1\$d units" $num $city
Similarly, positional parameters can be used with scan to extract values from internationalized strings. Note that it is not necessary to pass the output of ::msgcat::mc to format directly; by passing the values to substitute in as arguments, the formatting substitution is done directly.
msgcat::mc {Produced %1$d at %2$s} $num $city # ... where that key is mapped to one of the # human-oriented versions by msgcat::mcset
A package using msgcat may choose to use its own package private locale and its own set of loaded locales, independent to the global locale set by ::msgcat::mclocale.
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).
This action is controled by the following ensemble:
Each package using msgcat has a set of options within msgcat. The package options are described in the next sectionPackage options. Each package option may be set or unset individually using the following ensemble:
The following package options are available for each package:
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).
Unsetting this value will disable message file load for the package.
See section callback invocation below. The parameter list appended to this callback is the list of locales to load.
If this callback is changed, it is called with the preferences valid for the package.
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.
See section callback invocation below. The appended arguments are identical to ::msgcat::mcunknown.
Callbacks are invoked, if:
1. the callback command is set,
2. the command is not the empty string,
3. the registering namespace exists.
If a called routine fails with an error, the bgerror routine for the interpreter is invoked after command completion. Only exception is the callback unknowncmd, where an error causes the invoking mc-command to fail with that error.
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'.
If locales (or additional locales) are contained in another source like a data base, a package may use the load callback and not mcload:
namespace eval db { msgcat::mcpackageconfig loadcmd loadMessages proc loadMessages args { foreach locale $args { if {[LocaleInDB $locale]} { msgcat::mcmset $locale [GetLocaleList $locale] } } } }
The clock command implementation uses msgcat with a package locale to implement the command line parameter -locale. Here are some sketches of the implementation:
First, a package locale is initialized and the generic unknown function is desactivated:
msgcat::mcpackagelocale set msgcat::mcpackageconfig unknowncmd ""As an example, the user requires the week day in a certain locale as follows:
clock format clock seconds -format %A -locale frclock sets the package locale to fr and looks for the day name as follows:
msgcat::mcpackagelocale set $locale return [lindex [msgcat::mc DAYS_OF_WEEK_FULL] $day] ### Returns "mercredi"Within clock, some message-catalog items are heavy in computation and thus are dynamically cached using:
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 }
The message catalog code was developed by Mark Harrison.