Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/msgcat.n @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 11.4 KB
Line 
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'\" SCCS: @(#) msgcat.n
8'\"
9.so man.macros
10.TH "msgcat" n 1.4 msgcat "Tcl Bundled Packages"
11.BS
12'\" Note:  do not modify the .SH NAME line immediately below!
13.SH NAME
14msgcat \- Tcl message catalog
15.SH SYNOPSIS
16\fBpackage require Tcl 8.5\fR
17.sp
18\fBpackage require msgcat 1.4.2\fR
19.sp
20\fB::msgcat::mc \fIsrc-string\fR ?\fIarg arg ...\fR?
21.sp
22\fB::msgcat::mcmax ?\fIsrc-string src-string ...\fR?
23.sp
24\fB::msgcat::mclocale \fR?\fInewLocale\fR?
25.sp
26\fB::msgcat::mcpreferences\fR
27.sp
28\fB::msgcat::mcload \fIdirname\fR
29.sp
30\fB::msgcat::mcset \fIlocale src-string \fR?\fItranslate-string\fR?
31.sp
32\fB::msgcat::mcmset \fIlocale src-trans-list\fR
33.sp
34\fB::msgcat::mcunknown \fIlocale src-string\fR
35.BE
36.SH DESCRIPTION
37.PP
38The \fBmsgcat\fR package provides a set of functions
39that can be used to manage multi-lingual user interfaces.
40Text strings are defined in a
41.QW "message catalog"
42which is independent from the application, and
43which can be edited or localized without modifying
44the application source code.  New languages
45or locales are provided by adding a new file to
46the message catalog.
47.PP
48Use of the message catalog is optional by any application
49or package, but is encouraged if the application or package
50wishes to be enabled for multi-lingual applications.
51.SH COMMANDS
52.TP
53\fB::msgcat::mc \fIsrc-string\fR ?\fIarg arg ...\fR?
54Returns a translation of \fIsrc-string\fR according to the
55user's current locale.  If additional arguments past \fIsrc-string\fR
56are given, the \fBformat\fR command is used to substitute the
57additional arguments in the translation of \fIsrc-string\fR.
58.RS
59.PP
60\fB::msgcat::mc\fR will search the messages defined
61in the current namespace for a translation of \fIsrc-string\fR; if
62none is found, it will search in the parent of the current namespace,
63and so on until it reaches the global namespace.  If no translation
64string exists, \fB::msgcat::mcunknown\fR is called and the string
65returned from \fB::msgcat::mcunknown\fR is returned.
66.PP
67\fB::msgcat::mc\fR is the main function used to localize an
68application.  Instead of using an English string directly, an
69application can pass the English string through \fB::msgcat::mc\fR and
70use the result.  If an application is written for a single language in
71this fashion, then it is easy to add support for additional languages
72later simply by defining new message catalog entries.
73.RE
74.TP
75\fB::msgcat::mcmax ?\fIsrc-string src-string ...\fR?
76Given several source strings, \fB::msgcat::mcmax\fR returns the length
77of the longest translated string.  This is useful when designing
78localized GUIs, which may require that all buttons, for example, be a
79fixed width (which will be the width of the widest button).
80.TP
81\fB::msgcat::mclocale \fR?\fInewLocale\fR? 
82This function sets the locale to \fInewLocale\fR.  If \fInewLocale\fR
83is omitted, the current locale is returned, otherwise the current locale
84is set to \fInewLocale\fR.  msgcat stores and compares the locale in a
85case-insensitive manner, and returns locales in lowercase.
86The initial locale is determined by the locale specified in
87the user's environment.  See \fBLOCALE SPECIFICATION\fR
88below for a description of the locale string format.
89.TP
90\fB::msgcat::mcpreferences\fR
91Returns an ordered list of the locales preferred by
92the user, based on the user's language specification.
93The list is ordered from most specific to least
94preference.  The list is derived from the current
95locale set in msgcat by \fB::msgcat::mclocale\fR, and
96cannot be set independently.  For example, if the
97current locale is en_US_funky, then \fB::msgcat::mcpreferences\fR
98.VS 1.4
99returns \fB{en_US_funky en_US en {}}\fR.
100.VE 1.4
101.TP
102\fB::msgcat::mcload \fIdirname\fR
103Searches the specified directory for files that match
104the language specifications returned by \fB::msgcat::mcpreferences\fR
105(note that these are all lowercase), extended by the file extension
106.QW .msg .
107Each matching file is
108read in order, assuming a UTF-8 encoding.  The file contents are
109then evaluated as a Tcl script.  This means that Unicode characters
110may be present in the message file either directly in their UTF-8
111encoded form, or by use of the backslash-u quoting recognized by Tcl
112evaluation.  The number of message files which matched the specification
113and were loaded is returned.
114.TP
115\fB::msgcat::mcset \fIlocale src-string \fR?\fItranslate-string\fR?
116Sets the translation for \fIsrc-string\fR to \fItranslate-string\fR
117in the specified \fIlocale\fR and the current namespace.  If
118\fItranslate-string\fR is not specified, \fIsrc-string\fR is used
119for both.  The function returns \fItranslate-string\fR.
120.TP
121\fB::msgcat::mcmset \fIlocale src-trans-list\fR
122Sets the translation for multiple source strings in
123\fIsrc-trans-list\fR in the specified \fIlocale\fR and the current
124namespace.
125\fIsrc-trans-list\fR must have an even number of elements and is in
126the form {\fIsrc-string translate-string\fR ?\fIsrc-string
127translate-string ...\fR?} \fB::msgcat::mcmset\fR can be significantly
128faster than multiple invocations of \fB::msgcat::mcset\fR. The function
129returns the number of translations set.
130.TP
131\fB::msgcat::mcunknown \fIlocale src-string\fR
132This routine is called by \fB::msgcat::mc\fR in the case when
133a translation for \fIsrc-string\fR is not defined in the
134current locale.  The default action is to return
135\fIsrc-string\fR.  This procedure can be redefined by the
136application, for example to log error messages for each unknown
137string.  The \fB::msgcat::mcunknown\fR procedure is invoked at the
138same stack context as the call to \fB::msgcat::mc\fR.  The return value
139of \fB::msgcat::mcunknown\fR is used as the return value for the call
140to \fB::msgcat::mc\fR. 
141.SH "LOCALE SPECIFICATION"
142.PP
143The locale is specified to \fBmsgcat\fR by a locale string
144passed to \fB::msgcat::mclocale\fR.
145The locale string consists of
146a language code, an optional country code, and an optional
147system-specific code, each separated by
148.QW _ .
149The country and language
150codes are specified in standards ISO-639 and ISO-3166.
151For example, the locale
152.QW en
153specifies English and
154.QW en_US
155specifies U.S. English.
156.PP
157When the msgcat package is first loaded, the locale is initialized
158according to the user's environment.  The variables \fBenv(LC_ALL)\fR,
159\fBenv(LC_MESSAGES)\fR, and \fBenv(LANG)\fR are examined in order.
160The first of them to have a non-empty value is used to determine the
161initial locale.  The value is parsed according to the XPG4 pattern
162.CS
163language[_country][.codeset][@modifier]
164.CE
165to extract its parts.  The initial locale is then set by calling
166\fB::msgcat::mclocale\fR with the argument
167.CS
168language[_country][_modifier]
169.CE
170On Windows, if none of those environment variables is set, msgcat will
171attempt to extract locale information from the
172registry.  If all these attempts to discover an initial locale
173from the user's environment fail, msgcat defaults to an initial
174locale of
175.QW C .
176.PP
177When a locale is specified by the user, a
178.QW "best match"
179search is performed during string translation.  For example, if a user
180specifies
181.VS 1.4
182en_GB_Funky, the locales
183.QW en_GB_Funky ,
184.QW en_GB ,
185.QW en
186and
187.MT
188(the empty string)
189.VE 1.4
190are searched in order until a matching translation
191string is found.  If no translation string is available, then
192\fB::msgcat::mcunknown\fR is called.
193.SH "NAMESPACES AND MESSAGE CATALOGS"
194.PP
195Strings stored in the message catalog are stored relative
196to the namespace from which they were added.  This allows
197multiple packages to use the same strings without fear
198of collisions with other packages.  It also allows the
199source string to be shorter and less prone to typographical
200error.
201.PP
202For example, executing the code
203.CS
204\fB::msgcat::mcset\fR en hello "hello from ::"
205namespace eval foo {
206   \fB::msgcat::mcset\fR en hello "hello from ::foo"
207}
208puts [\fB::msgcat::mc\fR hello]
209namespace eval foo {puts [\fB::msgcat::mc\fR hello]}
210.CE
211will print
212.CS
213hello from ::
214hello from ::foo
215.CE
216.PP
217When searching for a translation of a message, the
218message catalog will search first the current namespace,
219then the parent of the current namespace, and so on until
220the global namespace is reached.  This allows child namespaces to
221.QW inherit
222messages from their parent namespace.
223.PP
224For example, executing (in the
225.QW en
226locale) the code
227.CS
228\fB::msgcat::mcset\fR en m1 ":: message1"
229\fB::msgcat::mcset\fR en m2 ":: message2"
230\fB::msgcat::mcset\fR en m3 ":: message3"
231namespace eval ::foo {
232   \fB::msgcat::mcset\fR en m2 "::foo message2"
233   \fB::msgcat::mcset\fR en m3 "::foo message3"
234}
235namespace eval ::foo::bar {
236   \fB::msgcat::mcset\fR en m3 "::foo::bar message3"
237}
238namespace import \fB::msgcat::mc\fR
239puts "[\fBmc\fR m1]; [\fBmc\fR m2]; [\fBmc\fR m3]"
240namespace eval ::foo {puts "[\fBmc\fR m1]; [\fBmc\fR m2]; [\fBmc\fR m3]"}
241namespace eval ::foo::bar {puts "[\fBmc\fR m1]; [\fBmc\fR m2]; [\fBmc\fR m3]"}
242.CE
243will print
244.CS
245:: message1; :: message2; :: message3
246:: message1; ::foo message2; ::foo message3
247:: message1; ::foo message2; ::foo::bar message3
248.CE
249.SH "LOCATION AND FORMAT OF MESSAGE FILES"
250.PP
251Message files can be located in any directory, subject
252to the following conditions:
253.IP [1]
254All message files for a package are in the same directory.
255.IP [2]
256The message file name is a msgcat locale specifier (all lowercase) followed by
257.QW .msg .
258For example:
259.CS
260es.msg    \(em spanish
261en_gb.msg \(em United Kingdom English
262.CE
263.VS 1.4
264\fIException:\fR The message file for the root locale
265.MT
266is called
267.QW \fBROOT.msg\fR .
268This exception is made so as not to
269cause peculiar behavior, such as marking the message file as
270.QW hidden
271on Unix file systems.
272.VE 1.4
273.IP [3]
274The file contains a series of calls to \fBmcset\fR and
275\fBmcmset\fR, setting the necessary translation strings
276for the language, likely enclosed in a \fBnamespace eval\fR
277so that all source strings are tied to the namespace of
278the package. For example, a short \fBes.msg\fR might contain:
279.CS
280namespace eval ::mypackage {
281   \fB::msgcat::mcset\fR es "Free Beer!" "Cerveza Gracias!"
282}
283.CE
284.SH "RECOMMENDED MESSAGE SETUP FOR PACKAGES"
285.PP
286If a package is installed into a subdirectory of the
287\fBtcl_pkgPath\fR and loaded via \fBpackage require\fR, the
288following procedure is recommended.
289.IP [1]
290During package installation, create a subdirectory
291\fBmsgs\fR under your package directory.
292.IP [2]
293Copy your *.msg files into that directory.
294.IP [3]
295 Add the following command to your package
296initialization script:
297.CS
298# load language files, stored in msgs subdirectory
299\fB::msgcat::mcload\fR [file join [file dirname [info script]] msgs]
300.CE
301.SH "POSITIONAL CODES FOR FORMAT AND SCAN COMMANDS"
302.PP
303It is possible that a message string used as an argument
304to \fBformat\fR might have positionally dependent parameters that
305might need to be repositioned.  For example, it might be
306syntactically desirable to rearrange the sentence structure
307while translating.
308.CS
309format "We produced %d units in location %s" $num $city
310format "In location %s we produced %d units" $city $num
311.CE
312.PP
313This can be handled by using the positional
314parameters:
315.CS
316format "We produced %1\e$d units in location %2\e$s" $num $city
317format "In location %2\e$s we produced %1\e$d units" $num $city
318.CE
319.PP
320Similarly, positional parameters can be used with \fBscan\fR to
321extract values from internationalized strings.
322.SH CREDITS
323.PP
324The message catalog code was developed by Mark Harrison.
325.SH "SEE ALSO"
326format(n), scan(n), namespace(n), package(n)
327.SH KEYWORDS
328internationalization, i18n, localization, l10n, message, text, translation
Note: See TracBrowser for help on using the repository browser.