1msgcat(n) Tcl Bundled Packages msgcat(n)
2
3
4
5______________________________________________________________________________
6
8 msgcat - Tcl message catalog
9
11 package require Tcl 8.2
12
13 package require msgcat 1.3
14
15 ::msgcat::mc src-string ?arg arg ...?
16
17 ::msgcat::mcmax ?src-string src-string ...?
18
19 ::msgcat::mclocale ?newLocale?
20
21 ::msgcat::mcpreferences
22
23 ::msgcat::mcload dirname
24
25 ::msgcat::mcset locale src-string ?translate-string?
26
27 ::msgcat::mcmset locale src-trans-list
28
29 ::msgcat::mcunknown locale src-string
30_________________________________________________________________
31
32
34 The msgcat package provides a set of functions that can be used to man‐
35 age multi-lingual user interfaces. Text strings are defined in a
36 ``message catalog'' which is independent from the application, and
37 which can be edited or localized without modifying the application
38 source code. New languages or locales are provided by adding a new
39 file to the message catalog.
40
41 Use of the message catalog is optional by any application or package,
42 but is encouraged if the application or package wishes to be enabled
43 for multi-lingual applications.
44
46 ::msgcat::mc src-string ?arg arg ...?
47 Returns a translation of src-string according to the user's cur‐
48 rent locale. If additional arguments past src-string are given,
49 the format command is used to substitute the additional argu‐
50 ments in the translation of src-string.
51
52 ::msgcat::mc will search the messages defined in the current namespace
53 for a translation of src-string; if none is found, it will search in
54 the parent of the current namespace, and so on until it reaches the
55 global namespace. If no translation string exists, ::msgcat::mcunknown
56 is called and the string returned from ::msgcat::mcunknown is returned.
57
58 ::msgcat::mc is the main function used to localize an application.
59 Instead of using an English string directly, an application can pass
60 the English string through ::msgcat::mc and use the result. If an
61 application is written for a single language in this fashion, then it
62 is easy to add support for additional languages later simply by defin‐
63 ing new message catalog entries.
64
65 ::msgcat::mcmax ?src-string src-string ...?
66 Given several source strings, ::msgcat::mcmax returns the length
67 of the longest translated string. This is useful when designing
68 localized GUIs, which may require that all buttons, for example,
69 be a fixed width (which will be the width of the widest button).
70
71 ::msgcat::mclocale ?newLocale?
72 This function sets the locale to newLocale. If newLocale is
73 omitted, the current locale is returned, otherwise the current
74 locale is set to newLocale. msgcat stores and compares the
75 locale in a case-insensitive manner, and returns locales in low‐
76 ercase. The initial locale is determined by the locale speci‐
77 fied in the user's environment. See LOCALE SPECIFICATION below
78 for a description of the locale string format.
79
80 ::msgcat::mcpreferences
81 Returns an ordered list of the locales preferred by the user,
82 based on the user's language specification. The list is ordered
83 from most specific to least preference. The list is derived
84 from the current locale set in msgcat by ::msgcat::mclocale, and
85 cannot be set independently. For example, if the current locale
86 is en_US_funky, then ::msgcat::mcpreferences returns
87 {en_US_funky en_US en}.
88
89 ::msgcat::mcload dirname
90 Searches the specified directory for files that match the lan‐
91 guage specifications returned by ::msgcat::mcpreferences (note
92 that these are all lowercase), extended by the file extension
93 ``.msg''. Each matching file is read in order, assuming a UTF-8
94 encoding. The file contents are then evaluated as a Tcl script.
95 This means that Unicode characters may be present in the message
96 file either directly in their UTF-8 encoded form, or by use of
97 the backslash-u quoting recognized by Tcl evaluation. The num‐
98 ber of message files which matched the specification and were
99 loaded is returned.
100
101 ::msgcat::mcset locale src-string ?translate-string?
102 Sets the translation for src-string to translate-string in the
103 specified locale and the current namespace. If translate-string
104 is not specified, src-string is used for both. The function
105 returns translate-string.
106
107 ::msgcat::mcmset locale src-trans-list
108 Sets the translation for multiple source strings in src-trans-
109 list in the specified locale and the current namespace. src-
110 trans-list must have an even number of elements and is in the
111 form {src-string translate-string ?src-string translate-string
112 ...?} ::msgcat::mcmset can be significantly faster than multiple
113 invocations of ::msgcat::mcset. The function returns the number
114 of translations set.
115
116 ::msgcat::mcunknown locale src-string
117 This routine is called by ::msgcat::mc in the case when a trans‐
118 lation for src-string is not defined in the current locale. The
119 default action is to return src-string. This procedure can be
120 redefined by the application, for example to log error messages
121 for each unknown string. The ::msgcat::mcunknown procedure is
122 invoked at the same stack context as the call to ::msgcat::mc.
123 The return value of ::msgcat::mcunknown is used as the return
124 value for the call to ::msgcat::mc.
125
127 The locale is specified to msgcat by a locale string passed to ::msg‐
128 cat::mclocale. The locale string consists of a language code, an
129 optional country code, and an optional system-specific code, each sepa‐
130 rated by ``_''. The country and language codes are specified in stan‐
131 dards ISO-639 and ISO-3166. For example, the locale ``en'' specifies
132 English and ``en_US'' specifies U.S. English.
133
134 When the msgcat package is first loaded, the locale is initialized
135 according to the user's environment. The variables env(LC_ALL),
136 env(LC_MESSAGES), and env(LANG) are examined in order. The first of
137 them to have a non-empty value is used to determine the initial locale.
138 The value is parsed according to the XPG4 pattern
139 language[_country][.codeset][@modifier]
140 to extract its parts. The initial locale is then set by calling ::msg‐
141 cat::mclocale with the argument
142 language[_country][_modifier]
143 On Windows, if none of those environment variables is set, msgcat will
144 attempt to extract locale information from the registry. If all these
145 attempts to discover an initial locale from the user's environment
146 fail, msgcat defaults to an initial locale of ``C''.
147
148 When a locale is specified by the user, a ``best match'' search is per‐
149 formed during string translation. For example, if a user specifies
150 en_GB_Funky, the locales ``en_GB_Funky'', ``en_GB'', and ``en'' are
151 searched in order until a matching translation string is found. If no
152 translation string is available, then ::msgcat::unknown is called.
153
155 Strings stored in the message catalog are stored relative to the names‐
156 pace from which they were added. This allows multiple packages to use
157 the same strings without fear of collisions with other packages. It
158 also allows the source string to be shorter and less prone to typo‐
159 graphical error.
160
161 For example, executing the code
162 ::msgcat::mcset en hello "hello from ::"
163 namespace eval foo {
164 ::msgcat::mcset en hello "hello from ::foo"
165 }
166 puts [::msgcat::mc hello]
167 namespace eval foo {puts [::msgcat::mc hello]}
168 will print
169 hello from ::
170 hello from ::foo
171
172 When searching for a translation of a message, the message catalog will
173 search first the current namespace, then the parent of the current
174 namespace, and so on until the global namespace is reached. This
175 allows child namespaces to "inherit" messages from their parent names‐
176 pace.
177
178 For example, executing (in the ``en'' locale) the code
179 ::msgcat::mcset en m1 ":: message1"
180 ::msgcat::mcset en m2 ":: message2"
181 ::msgcat::mcset en m3 ":: message3"
182 namespace eval ::foo {
183 ::msgcat::mcset en m2 "::foo message2"
184 ::msgcat::mcset en m3 "::foo message3"
185 }
186 namespace eval ::foo::bar {
187 ::msgcat::mcset en m3 "::foo::bar message3"
188 }
189 namespace import ::msgcat::mc
190 puts "[mc m1]; [mc m2]; [mc m3]"
191 namespace eval ::foo {puts "[mc m1]; [mc m2]; [mc m3]"}
192 namespace eval ::foo::bar {puts "[mc m1]; [mc m2]; [mc m3]"}
193 will print
194 :: message1; :: message2; :: message3
195 :: message1; ::foo message2; ::foo message3
196 :: message1; ::foo message2; ::foo::bar message3
197
199 Message files can be located in any directory, subject to the following
200 conditions:
201
202 [1] All message files for a package are in the same directory.
203
204 [2] The message file name is a msgcat locale specifier (all lower‐
205 case) followed by ``.msg''. For example:
206 es.msg -- spanish
207 en_gb.msg -- United Kingdom English
208
209 [3] The file contains a series of calls to mcset and mcmset, setting
210 the necessary translation strings for the language, likely
211 enclosed in a namespace eval so that all source strings are tied
212 to the namespace of the package. For example, a short es.msg
213 might contain:
214 namespace eval ::mypackage {
215 ::msgcat::mcset es "Free Beer!" "Cerveza Gracias!"
216 }
217
219 If a package is installed into a subdirectory of the tcl_pkgPath and
220 loaded via package require, the following procedure is recommended.
221
222 [1] During package installation, create a subdirectory msgs under
223 your package directory.
224
225 [2] Copy your *.msg files into that directory.
226
227 [3]
228 Add the following command to your package initialization
229 script:
230 # load language files, stored in msgs subdirectory
231 ::msgcat::mcload [file join [file dirname [info script]] msgs]
232
234 It is possible that a message string used as an argument to format
235 might have positionally dependent parameters that might need to be
236 repositioned. For example, it might be syntactically desirable to
237 rearrange the sentence structure while translating.
238 format "We produced %d units in location %s" $num $city
239 format "In location %s we produced %d units" $city $num
240
241 This can be handled by using the positional parameters:
242 format "We produced %1\$d units in location %2\$s" $num $city
243 format "In location %2\$s we produced %1\$d units" $num $city
244
245 Similarly, positional parameters can be used with scan to extract val‐
246 ues from internationalized strings.
247
249 The message catalog code was developed by Mark Harrison.
250
251
253 format(n), scan(n), namespace(n), package(n)
254
255
257 internationalization, i18n, localization, l10n, message, text, transla‐
258 tion
259
260
261
262msgcat 1.3 msgcat(n)