Debian Codegraph @

doc/msgcat.n

raw
1 '\"
2 '\" Copyright (c) 1998 Mark Harrison.
3 '\"
4 '\" See the file "license.terms" for information on usage and redistribution
5 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
6 '\"
7 .TH "msgcat" n 1.5 msgcat "Tcl Bundled Packages"
8 .so man.macros
9 .BS
10 '\" Note: do not modify the .SH NAME line immediately below!
11 .SH NAME
12 msgcat \- Tcl message catalog
13 .SH SYNOPSIS
14 .nf
15 \fBpackage require Tcl 8.5\fR
16 \fBpackage require msgcat 1.6\fR
17
18 \fB::msgcat::mc \fIsrc-string\fR ?\fIarg arg ...\fR?
19 \fB::msgcat::mcmax ?\fIsrc-string src-string ...\fR?
20 .VS "TIP 412"
21 \fB::msgcat::mcexists\fR ?\fB-exactnamespace\fR? ?\fB-exactlocale\fR? \fIsrc-string\fR
22 .VE "TIP 412"
23 .sp
24 \fB::msgcat::mclocale \fR?\fInewLocale\fR?
25 .sp
26 \fB::msgcat::mcpreferences\fR
27 .sp
28 .VS "TIP 412"
29 \fB::msgcat::mcloadedlocales subcommand\fR ?\fIlocale\fR?
30 .VE "TIP 412"
31 \fB::msgcat::mcload \fIdirname\fR
32 \fB::msgcat::mcset \fIlocale src-string \fR?\fItranslate-string\fR?
33 \fB::msgcat::mcmset \fIlocale src-trans-list\fR
34 \fB::msgcat::mcflset \fIsrc-string \fR?\fItranslate-string\fR?
35 \fB::msgcat::mcflmset \fIsrc-trans-list\fR
36 \fB::msgcat::mcunknown \fIlocale src-string\fR ?\fIarg arg ...\fR?
37 .VS "TIP 412"
38 \fB::msgcat::mcpackagelocale subcommand\fR ?\fIlocale\fR?
39 \fB::msgcat::mcpackageconfig subcommand\fR \fIoption\fR ?\fIvalue\fR?
40 \fB::msgcat::mcforgetpackage\fR
41 .VE "TIP 412"
42 .BE
43 .SH DESCRIPTION
44 .PP
45 The \fBmsgcat\fR package provides a set of functions
46 that can be used to manage multi-lingual user interfaces.
47 Text strings are defined in a
48 .QW "message catalog"
49 which is independent from the application, and
50 which can be edited or localized without modifying
51 the application source code. New languages
52 or locales may be provided by adding a new file to
53 the message catalog.
54 .PP
55 \fBmsgcat\fR distinguishes packages by its namespace. Each package has
56 its own message catalog and configuration settings in \fBmsgcat\fR.
57 .PP
58 A \fIlocale\fR is a specification string describing a user language like
59 \fBde_ch\fR for Swiss German. In \fBmsgcat\fR, there is a global locale
60 initialized by the system locale of the current system. Each package may
61 decide to use the global locale or to use a package specific locale.
62 .PP
63 The global locale may be changed on demand, for example by a user initiated
64 language change or within a multi user application like a web server.
65 .SH COMMANDS
66 .\" COMMAND: mc
67 .TP
68 \fB::msgcat::mc \fIsrc-string\fR ?\fIarg arg ...\fR?
69 .
70 Returns a translation of \fIsrc-string\fR according to the
71 current locale. If additional arguments past \fIsrc-string\fR
72 are given, the \fBformat\fR command is used to substitute the
73 additional arguments in the translation of \fIsrc-string\fR.
74 .RS
75 .PP
76 \fB::msgcat::mc\fR will search the messages defined
77 in the current namespace for a translation of \fIsrc-string\fR; if
78 none is found, it will search in the parent of the current namespace,
79 and so on until it reaches the global namespace. If no translation
80 string exists, \fB::msgcat::mcunknown\fR is called and the string
81 returned from \fB::msgcat::mcunknown\fR is returned.
82 .PP
83 \fB::msgcat::mc\fR is the main function used to localize an
84 application. Instead of using an English string directly, an
85 application can pass the English string through \fB::msgcat::mc\fR and
86 use the result. If an application is written for a single language in
87 this fashion, then it is easy to add support for additional languages
88 later simply by defining new message catalog entries.
89 .RE
90 .\" COMMAND: mcmax
91 .TP
92 \fB::msgcat::mcmax ?\fIsrc-string src-string ...\fR?
93 .
94 Given several source strings, \fB::msgcat::mcmax\fR returns the length
95 of the longest translated string. This is useful when designing
96 localized GUIs, which may require that all buttons, for example, be a
97 fixed width (which will be the width of the widest button).
98 .\" COMMAND: mcexists
99 .TP
100 \fB::msgcat::mcexists\fR ?\fB-exactnamespace\fR? ?\fB-exactlocale\fR? \fIsrc-string\fR
101 .VS "TIP 412"
102 Return true, if there is a translation for the given \fIsrc-string\fR.
103 .PP
104 .RS
105 The search may be limited by the option \fB\-exactnamespace\fR to only check
106 the current namespace and not any parent namespaces.
107 .PP
108 It may also be limited by the option \fB\-exactlocale\fR to only check the
109 first prefered locale (e.g. first element returned by
110 \fB::msgcat::mcpreferences\fR if global locale is used).
111 .RE
112 .VE "TIP 412"
113 .\" COMMAND: mclocale
114 .TP
115 \fB::msgcat::mclocale \fR?\fInewLocale\fR?
116 .
117 This function sets the locale to \fInewLocale\fR. If \fInewLocale\fR
118 is omitted, the current locale is returned, otherwise the current locale
119 is set to \fInewLocale\fR. msgcat stores and compares the locale in a
120 case-insensitive manner, and returns locales in lowercase.
121 The initial locale is determined by the locale specified in
122 the user's environment. See \fBLOCALE SPECIFICATION\fR
123 below for a description of the locale string format.
124 .RS
125 .PP
126 .VS "TIP 412"
127 If the locale is set, the preference list of locales is evaluated.
128 Locales in this list are loaded now, if not jet loaded.
129 .VE "TIP 412"
130 .RE
131 .\" COMMAND: mcpreferences
132 .TP
133 \fB::msgcat::mcpreferences\fR
134 .
135 Returns an ordered list of the locales preferred by
136 the user, based on the user's language specification.
137 The list is ordered from most specific to least
138 preference. The list is derived from the current
139 locale set in msgcat by \fB::msgcat::mclocale\fR, and
140 cannot be set independently. For example, if the
141 current locale is en_US_funky, then \fB::msgcat::mcpreferences\fR
142 returns \fB{en_us_funky en_us en {}}\fR.
143 .\" COMMAND: mcloadedlocales
144 .TP
145 \fB::msgcat::mcloadedlocales subcommand\fR ?\fIlocale\fR?
146 .
147 This group of commands manage the list of loaded locales for packages not setting a package locale.
148 .PP
149 .RS
150 The subcommand \fBget\fR returns the list of currently loaded locales.
151 .PP
152 The subcommand \fBpresent\fR requires the argument \fIlocale\fR and returns true, if this locale is loaded.
153 .PP
154 The subcommand \fBclear\fR removes all locales and their data, which are not in the current preference list.
155 .RE
156 .\" COMMAND: mcload
157 .TP
158 \fB::msgcat::mcload \fIdirname\fR
159 .VS "TIP 412"
160 Searches the specified directory for files that match
161 the language specifications returned by \fB::msgcat::mcloadedlocales get\fR
162 (or \fBmsgcat::mcpackagelocale preferences\fR if a package locale is set)
163 (note that these are all lowercase), extended by the file extension
164 .QW .msg .
165 Each matching file is
166 read in order, assuming a UTF-8 encoding. The file contents are
167 then evaluated as a Tcl script. This means that Unicode characters
168 may be present in the message file either directly in their UTF-8
169 encoded form, or by use of the backslash-u quoting recognized by Tcl
170 evaluation. The number of message files which matched the specification
171 and were loaded is returned.
172 .RS
173 .PP
174 In addition, the given folder is stored in the \fBmsgcat\fR package
175 configuration option \fImcfolder\fR to eventually load message catalog
176 files required by a locale change.
177 .VE "TIP 412"
178 .RE
179 .\" COMMAND: mcset
180 .TP
181 \fB::msgcat::mcset \fIlocale src-string \fR?\fItranslate-string\fR?
182 .
183 Sets the translation for \fIsrc-string\fR to \fItranslate-string\fR
184 in the specified \fIlocale\fR and the current namespace. If
185 \fItranslate-string\fR is not specified, \fIsrc-string\fR is used
186 for both. The function returns \fItranslate-string\fR.
187 .\" COMMAND: mcmset
188 .TP
189 \fB::msgcat::mcmset \fIlocale src-trans-list\fR
190 .
191 Sets the translation for multiple source strings in
192 \fIsrc-trans-list\fR in the specified \fIlocale\fR and the current
193 namespace.
194 \fIsrc-trans-list\fR must have an even number of elements and is in
195 the form {\fIsrc-string translate-string\fR ?\fIsrc-string
196 translate-string ...\fR?} \fB::msgcat::mcmset\fR can be significantly
197 faster than multiple invocations of \fB::msgcat::mcset\fR. The function
198 returns the number of translations set.
199 .\" COMMAND: mcflset
200 .TP
201 \fB::msgcat::mcflset \fIsrc-string \fR?\fItranslate-string\fR?
202 .
203 Sets the translation for \fIsrc-string\fR to \fItranslate-string\fR in the
204 current namespace for the locale implied by the name of the message catalog
205 being loaded via \fB::msgcat::mcload\fR. If \fItranslate-string\fR is not
206 specified, \fIsrc-string\fR is used for both. The function returns
207 \fItranslate-string\fR.
208 .\" COMMAND: mcflmset
209 .TP
210 \fB::msgcat::mcflmset \fIsrc-trans-list\fR
211 .
212 Sets the translation for multiple source strings in \fIsrc-trans-list\fR in
213 the current namespace for the locale implied by the name of the message
214 catalog being loaded via \fB::msgcat::mcload\fR. \fIsrc-trans-list\fR must
215 have an even number of elements and is in the form {\fIsrc-string
216 translate-string\fR ?\fIsrc-string translate-string ...\fR?}
217 \fB::msgcat::mcflmset\fR can be significantly faster than multiple invocations
218 of \fB::msgcat::mcflset\fR. The function returns the number of translations set.
219 .\" COMMAND: mcunknown
220 .TP
221 \fB::msgcat::mcunknown \fIlocale src-string\fR ?\fIarg arg ...\fR?
222 .
223 This routine is called by \fB::msgcat::mc\fR in the case when
224 a translation for \fIsrc-string\fR is not defined in the
225 current locale. The default action is to return
226 \fIsrc-string\fR passed by format if there are any arguments. This
227 procedure can be redefined by the
228 application, for example to log error messages for each unknown
229 string. The \fB::msgcat::mcunknown\fR procedure is invoked at the
230 same stack context as the call to \fB::msgcat::mc\fR. The return value
231 of \fB::msgcat::mcunknown\fR is used as the return value for the call
232 to \fB::msgcat::mc\fR.
233 .VS "TIP 412"
234 .RS
235 .PP
236 Note that this routine is only called if the concerned package did not set a
237 package locale unknown command name.
238 .RE
239 .\" COMMAND: mcforgetpackage
240 .TP
241 \fB::msgcat::mcforgetpackage\fR
242 .
243 The calling package clears all its state within the \fBmsgcat\fR package
244 including all settings and translations.
245 .VE "TIP 412"
246 .PP
247 .SH "LOCALE SPECIFICATION"
248 .PP
249 The locale is specified to \fBmsgcat\fR by a locale string
250 passed to \fB::msgcat::mclocale\fR.
251 The locale string consists of
252 a language code, an optional country code, and an optional
253 system-specific code, each separated by
254 .QW _ .
255 The country and language
256 codes are specified in standards ISO-639 and ISO-3166.
257 For example, the locale
258 .QW en
259 specifies English and
260 .QW en_US
261 specifies U.S. English.
262 .PP
263 When the msgcat package is first loaded, the locale is initialized
264 according to the user's environment. The variables \fBenv(LC_ALL)\fR,
265 \fBenv(LC_MESSAGES)\fR, and \fBenv(LANG)\fR are examined in order.
266 The first of them to have a non-empty value is used to determine the
267 initial locale. The value is parsed according to the XPG4 pattern
268 .PP
269 .CS
270 language[_country][.codeset][@modifier]
271 .CE
272 .PP
273 to extract its parts. The initial locale is then set by calling
274 \fB::msgcat::mclocale\fR with the argument
275 .PP
276 .CS
277 language[_country][_modifier]
278 .CE
279 .PP
280 On Windows and Cygwin, if none of those environment variables is set,
281 msgcat will attempt to extract locale information from the registry.
282 From Windows Vista on, the RFC4747 locale name "lang-script-country-options"
283 is transformed to the locale as "lang_country_script" (Example:
284 sr-Latn-CS -> sr_cs_latin). For Windows XP, the language id is
285 transformed analoguously (Example: 0c1a -> sr_yu_cyrillic).
286 If all these attempts to discover an initial locale from the user's
287 environment fail, msgcat defaults to an initial locale of
288 .QW C .
289 .PP
290 When a locale is specified by the user, a
291 .QW "best match"
292 search is performed during string translation. For example, if a user
293 specifies
294 en_GB_Funky, the locales
295 .QW en_gb_funky ,
296 .QW en_gb ,
297 .QW en
298 and
299 .MT
300 (the empty string)
301 are searched in order until a matching translation
302 string is found. If no translation string is available, then
303 the unknown handler is called.
304 .SH "NAMESPACES AND MESSAGE CATALOGS"
305 .PP
306 Strings stored in the message catalog are stored relative
307 to the namespace from which they were added. This allows
308 multiple packages to use the same strings without fear
309 of collisions with other packages. It also allows the
310 source string to be shorter and less prone to typographical
311 error.
312 .PP
313 For example, executing the code
314 .PP
315 .CS
316 \fB::msgcat::mcset\fR en hello "hello from ::"
317 namespace eval foo {
318 \fB::msgcat::mcset\fR en hello "hello from ::foo"
319 }
320 puts [\fB::msgcat::mc\fR hello]
321 namespace eval foo {puts [\fB::msgcat::mc\fR hello]}
322 .CE
323 .PP
324 will print
325 .PP
326 .CS
327 hello from ::
328 hello from ::foo
329 .CE
330 .PP
331 When searching for a translation of a message, the
332 message catalog will search first the current namespace,
333 then the parent of the current namespace, and so on until
334 the global namespace is reached. This allows child namespaces to
335 .QW inherit
336 messages from their parent namespace.
337 .PP
338 For example, executing (in the
339 .QW en
340 locale) the code
341 .PP
342 .CS
343 \fB::msgcat::mcset\fR en m1 ":: message1"
344 \fB::msgcat::mcset\fR en m2 ":: message2"
345 \fB::msgcat::mcset\fR en m3 ":: message3"
346 namespace eval ::foo {
347 \fB::msgcat::mcset\fR en m2 "::foo message2"
348 \fB::msgcat::mcset\fR en m3 "::foo message3"
349 }
350 namespace eval ::foo::bar {
351 \fB::msgcat::mcset\fR en m3 "::foo::bar message3"
352 }
353 namespace import \fB::msgcat::mc\fR
354 puts "[\fBmc\fR m1]; [\fBmc\fR m2]; [\fBmc\fR m3]"
355 namespace eval ::foo {puts "[\fBmc\fR m1]; [\fBmc\fR m2]; [\fBmc\fR m3]"}
356 namespace eval ::foo::bar {puts "[\fBmc\fR m1]; [\fBmc\fR m2]; [\fBmc\fR m3]"}
357 .CE
358 .PP
359 will print
360 .PP
361 .CS
362 :: message1; :: message2; :: message3
363 :: message1; ::foo message2; ::foo message3
364 :: message1; ::foo message2; ::foo::bar message3
365 .CE
366 .SH "LOCATION AND FORMAT OF MESSAGE FILES"
367 .PP
368 Message files can be located in any directory, subject
369 to the following conditions:
370 .IP [1]
371 All message files for a package are in the same directory.
372 .IP [2]
373 The message file name is a msgcat locale specifier (all lowercase) followed by
374 .QW .msg .
375 For example:
376 .PP
377 .CS
378 es.msg \(em spanish
379 en_gb.msg \(em United Kingdom English
380 .CE
381 .PP
382 \fIException:\fR The message file for the root locale
383 .MT
384 is called
385 .QW \fBROOT.msg\fR .
386 This exception is made so as not to
387 cause peculiar behavior, such as marking the message file as
388 .QW hidden
389 on Unix file systems.
390 .IP [3]
391 The file contains a series of calls to \fBmcflset\fR and
392 \fBmcflmset\fR, setting the necessary translation strings
393 for the language, likely enclosed in a \fBnamespace eval\fR
394 so that all source strings are tied to the namespace of
395 the package. For example, a short \fBes.msg\fR might contain:
396 .PP
397 .CS
398 namespace eval ::mypackage {
399 \fB::msgcat::mcflset\fR "Free Beer" "Cerveza Gratis"
400 }
401 .CE
402 .SH "RECOMMENDED MESSAGE SETUP FOR PACKAGES"
403 .PP
404 If a package is installed into a subdirectory of the
405 \fBtcl_pkgPath\fR and loaded via \fBpackage require\fR, the
406 following procedure is recommended.
407 .IP [1]
408 During package installation, create a subdirectory
409 \fBmsgs\fR under your package directory.
410 .IP [2]
411 Copy your *.msg files into that directory.
412 .IP [3]
413 Add the following command to your package initialization script:
414 .PP
415 .CS
416 # load language files, stored in msgs subdirectory
417 \fB::msgcat::mcload\fR [file join [file dirname [info script]] msgs]
418 .CE
419 .SH "POSITIONAL CODES FOR FORMAT AND SCAN COMMANDS"
420 .PP
421 It is possible that a message string used as an argument
422 to \fBformat\fR might have positionally dependent parameters that
423 might need to be repositioned. For example, it might be
424 syntactically desirable to rearrange the sentence structure
425 while translating.
426 .PP
427 .CS
428 format "We produced %d units in location %s" $num $city
429 format "In location %s we produced %d units" $city $num
430 .CE
431 .PP
432 This can be handled by using the positional
433 parameters:
434 .PP
435 .CS
436 format "We produced %1\e$d units in location %2\e$s" $num $city
437 format "In location %2\e$s we produced %1\e$d units" $num $city
438 .CE
439 .PP
440 Similarly, positional parameters can be used with \fBscan\fR to
441 extract values from internationalized strings. Note that it is not
442 necessary to pass the output of \fB::msgcat::mc\fR to \fBformat\fR
443 directly; by passing the values to substitute in as arguments, the
444 formatting substitution is done directly.
445 .PP
446 .CS
447 \fBmsgcat::mc\fR {Produced %1$d at %2$s} $num $city
448 # ... where that key is mapped to one of the
449 # human-oriented versions by \fBmsgcat::mcset\fR
450 .CE
451 .VS "TIP 412"
452 .SH "PACKAGE PRIVATE LOCALE"
453 .PP
454 A package using \fBmsgcat\fR may choose to use its own package private
455 locale and its own set of loaded locales, independent to the global
456 locale set by \fB::msgcat::mclocale\fR.
457 .PP
458 This allows a package to change its locale without causing any locales load or
459 removal in other packages and not to invoke the global locale change callback
460 (see below).
461 .PP
462 This action is controled by the following ensemble:
463 .\" COMMAND: mcpackagelocale
464 .\" METHOD: set
465 .TP
466 \fB::msgcat::mcpackagelocale set\fR ?\fIlocale\fR?
467 .
468 Set or change a package private locale.
469 The package private locale is set to the given \fIlocale\fR if the \fIlocale\fR
470 is given. If the option \fIlocale\fR is not given, the package is set to package
471 private locale mode, but no locale is changed (e.g. if the global locale was
472 valid for the package before, it is copied to the package private locale).
473 .PP
474 .RS
475 This command may cause the load of locales.
476 .RE
477 .\" METHOD: get
478 .TP
479 \fB::msgcat::mcpackagelocale get\fR
480 .
481 Return the package private locale or the global locale, if no package private
482 locale is set.
483 .\" METHOD: preferences
484 .TP
485 \fB::msgcat::mcpackagelocale preferences\fR
486 .
487 Return the package private preferences or the global preferences,
488 if no package private locale is set.
489 .\" METHOD: loaded
490 .TP
491 \fB::msgcat::mcpackagelocale loaded\fR
492 .
493 Return the list of locales loaded for this package.
494 .\" METHOD: isset
495 .TP
496 \fB::msgcat::mcpackagelocale isset\fR
497 .
498 Returns true, if a package private locale is set.
499 .\" METHOD: unset
500 .TP
501 \fB::msgcat::mcpackagelocale unset\fR
502 .
503 Unset the package private locale and use the global locale.
504 Load and remove locales to adjust the list of loaded locales for the
505 package to the global loaded locales list.
506 .\" METHOD: present
507 .TP
508 \fB::msgcat::mcpackagelocale present\fI locale\fR
509 .
510 Returns true, if the given locale is loaded for the package.
511 .\" METHOD: clear
512 .TP
513 \fB::msgcat::mcpackagelocale clear\fR
514 .
515 Clear any loaded locales of the package not present in the package preferences.
516 .PP
517 .SH "CHANGING PACKAGE OPTIONS"
518 .PP
519 Each package using msgcat has a set of options within \fBmsgcat\fR.
520 The package options are described in the next sectionPackage options.
521 Each package option may be set or unset individually using the following
522 ensemble:
523 .\" COMMAND: mcpackageconfig
524 .\" METHOD: get
525 .TP
526 \fB::msgcat::mcpackageconfig get\fI option\fR
527 .
528 Return the current value of the given \fIoption\fR.
529 This call returns an error if the option is not set for the package.
530 .\" METHOD: isset
531 .TP
532 \fB::msgcat::mcpackageconfig isset\fI option\fR
533 .
534 Returns 1, if the given \fIoption\fR is set for the package, 0 otherwise.
535 .\" METHOD: set
536 .TP
537 \fB::msgcat::mcpackageconfig set\fI option value\fR
538 .
539 Set the given \fIoption\fR to the given \fIvalue\fR.
540 This may invoke additional actions in dependency of the \fIoption\fR.
541 The return value is 0 or the number of loaded packages for the option
542 \fBmcfolder\fR.
543 .\" METHOD: unset
544 .TP
545 \fB::msgcat::mcpackageconfig unset\fI option\fR
546 .
547 Unsets the given \fIoption\fR for the package.
548 No action is taken if the \fIoption\fR is not set for the package.
549 The empty string is returned.
550 .SS Package options
551 .PP
552 The following package options are available for each package:
553 .TP
554 \fBmcfolder\fR
555 .
556 This is the message folder of the package. This option is set by mcload and by
557 the subcommand set. Both are identical and both return the number of loaded
558 message catalog files.
559 .RS
560 .PP
561 Setting or changing this value will load all locales contained in the
562 preferences valid for the package. This implies also to invoke any set
563 loadcmd (see below).
564 .PP
565 Unsetting this value will disable message file load for the package.
566 .RE
567 .TP
568 \fBloadcmd\fR
569 .
570 This callback is invoked before a set of message catalog files are loaded for
571 the package which has this property set.
572 .PP
573 .RS
574 This callback may be used to do any preparation work for message file load or
575 to get the message data from another source like a data base. In this case, no
576 message files are used (mcfolder is unset).
577 .PP
578 See section \fBcallback invocation\fR below.
579 The parameter list appended to this callback is the list of locales to load.
580 .PP
581 If this callback is changed, it is called with the preferences valid for the
582 package.
583 .RE
584 .TP
585 \fBchangecmd\fR
586 .
587 This callback is invoked when a default local change was performed. Its
588 purpose is to allow a package to update any dependency on the default locale
589 like showing the GUI in another language.
590 .PP
591 .RS
592 See the callback invocation section below.
593 The parameter list appended to this callback is \fBmcpreferences\fR.
594 The registered callbacks are invoked in no particular order.
595 .RE
596 .TP
597 \fBunknowncmd\fR
598 .
599 Use a package locale mcunknown procedure instead of the standard version
600 supplied by the msgcat package (\fBmsgcat::mcunknown\fR).
601 .PP
602 .RS
603 The called procedure must return the formatted message which will finally be
604 returned by \fBmsgcat::mc\fR.
605 .PP
606 A generic unknown handler is used if set to the empty string. This consists of
607 returning the key if no arguments are given. With given arguments, the
608 \fBformat\fR command is used to process the arguments.
609 .PP
610 See section \fBcallback invocation\fR below.
611 The appended arguments are identical to \fBmsgcat::mcunknown\fR.
612 .RE
613 .SH "Callback invocation"
614 A package may decide to register one or multiple callbacks, as described above.
615 .PP
616 Callbacks are invoked, if:
617 .PP
618 1. the callback command is set,
619 .PP
620 2. the command is not the empty string,
621 .PP
622 3. the registering namespace exists.
623 .PP
624 If a called routine fails with an error, the \fBbgerror\fR routine for the
625 interpreter is invoked after command completion.
626 Only exception is the callback \fBunknowncmd\fR, where an error causes the
627 invoking \fBmc\fR-command to fail with that error.
628 .PP
629 .SH EXAMPLES
630 Packages which display a GUI may update their widgets when the global locale
631 changes. To register to a callback, use:
632 .CS
633 namespace eval gui {
634 msgcat::mcpackageconfig changecmd updateGUI
635
636 proc updateGUI args {
637 puts "New locale is '[lindex $args 0]'."
638 }
639 }
640 % msgcat::mclocale fr
641 fr
642 % New locale is 'fr'.
643 .CE
644 .PP
645 If locales (or additional locales) are contained in another source like a
646 database, a package may use the load callback and not \fBmcload\fR:
647 .CS
648 namespace eval db {
649 msgcat::mcpackageconfig loadcmd loadMessages
650
651 proc loadMessages args {
652 foreach locale $args {
653 if {[LocaleInDB $locale]} {
654 msgcat::mcmset $locale [GetLocaleList $locale]
655 }
656 }
657 }
658 }
659 .CE
660 .PP
661 The \fBclock\fR command implementation uses \fBmsgcat\fR with a package
662 locale to implement the command line parameter \fB\-locale\fR.
663 Here are some sketches of the implementation:
664 .PP
665 First, a package locale is initialized and the generic unknown function is
666 deactivated:
667 .CS
668 msgcat::mcpackagelocale set
669 msgcat::mcpackageconfig unknowncmd ""
670 .CE
671 As an example, the user requires the week day in a certain locale as follows:
672 .CS
673 clock format [clock seconds] -format %A -locale fr
674 .CE
675 \fBclock\fR sets the package locale to \fBfr\fR and looks for the day name as
676 follows:
677 .CS
678 msgcat::mcpackagelocale set $locale
679 return [lindex [msgcat::mc DAYS_OF_WEEK_FULL] $day]
680 ### Returns "mercredi"
681 .CE
682 Within \fBclock\fR, some message-catalog items are heavy in computation and
683 thus are dynamically cached using:
684 .CS
685 proc ::tcl::clock::LocalizeFormat { locale format } {
686 set key FORMAT_$format
687 if { [::msgcat::mcexists -exactlocale -exactnamespace $key] } {
688 return [mc $key]
689 }
690 #...expensive computation of format clipped...
691 mcset $locale $key $format
692 return $format
693 }
694 .CE
695 .VE "TIP 412"
696 .SH CREDITS
697 .PP
698 The message catalog code was developed by Mark Harrison.
699 .SH "SEE ALSO"
700 format(n), scan(n), namespace(n), package(n)
701 .SH KEYWORDS
702 internationalization, i18n, localization, l10n, message, text, translation
703 .\" Local Variables:
704 .\" mode: nroff
705 .\" End: