1msgcat(n)                    Tcl Bundled Packages                    msgcat(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       msgcat - Tcl message catalog
9

SYNOPSIS

11       package require Tcl 8.5
12
13       package require msgcat 1.5.0
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::mcflset src-string ?translate-string?                         │
30
31       ::msgcat::mcflmset src-trans-list
32
33       ::msgcat::mcunknown locale src-string
34_________________________________________________________________
35

DESCRIPTION

37       The msgcat package provides a set of functions that can be used to man‐
38       age multi-lingual user interfaces.  Text strings are defined in a “mes‐
39       sage  catalog” which is independent from the application, and which can
40       be edited or localized without modifying the application  source  code.
41       New  languages or locales are provided by adding a new file to the mes‐
42       sage catalog.
43
44       Use of the message catalog is optional by any application  or  package,
45       but  is  encouraged  if the application or package wishes to be enabled
46       for multi-lingual applications.
47

COMMANDS

49       ::msgcat::mc src-string ?arg arg ...?
50              Returns a translation of src-string according to the user's cur‐
51              rent locale.  If additional arguments past src-string are given,
52              the format command is used to substitute  the  additional  argu‐
53              ments in the translation of src-string.
54
55              ::msgcat::mc  will  search  the  messages defined in the current
56              namespace for a translation of src-string; if none is found,  it
57              will  search  in  the parent of the current namespace, and so on
58              until it reaches the global namespace.  If no translation string
59              exists,  ::msgcat::mcunknown  is  called and the string returned
60              from ::msgcat::mcunknown is returned.
61
62              ::msgcat::mc is the main function used to localize  an  applica‐
63              tion.   Instead of using an English string directly, an applica‐
64              tion can pass the English string through  ::msgcat::mc  and  use
65              the  result.  If an application is written for a single language
66              in this fashion, then it is easy to add support  for  additional
67              languages later simply by defining new message catalog entries.
68
69       ::msgcat::mcmax ?src-string src-string ...?
70              Given several source strings, ::msgcat::mcmax returns the length
71              of the longest translated string.  This is useful when designing
72              localized GUIs, which may require that all buttons, for example,
73              be a fixed width (which will be the width of the widest button).
74
75       ::msgcat::mclocale ?newLocale?
76              This function sets the locale to  newLocale.   If  newLocale  is
77              omitted,  the  current locale is returned, otherwise the current
78              locale is set to newLocale.   msgcat  stores  and  compares  the
79              locale in a case-insensitive manner, and returns locales in low‐
80              ercase.  The initial locale is determined by the  locale  speci‐
81              fied  in the user's environment.  See LOCALE SPECIFICATION below
82              for a description of the locale string format.
83
84       ::msgcat::mcpreferences
85              Returns an ordered list of the locales preferred  by  the  user,
86              based on the user's language specification.  The list is ordered
87              from most specific to least preference.   The  list  is  derived
88              from the current locale set in msgcat by ::msgcat::mclocale, and
89              cannot be set independently.  For example, if the current locale
90              is    en_US_funky,    then    ::msgcat::mcpreferences    returns
91              {en_US_funky en_US en {}}.
92
93       ::msgcat::mcload dirname
94              Searches the specified directory for files that match  the  lan‐
95              guage  specifications  returned by ::msgcat::mcpreferences (note
96              that these are all lowercase), extended by  the  file  extension
97              “.msg”.   Each  matching file is read in order, assuming a UTF-8
98              encoding.  The file contents are then evaluated as a Tcl script.
99              This means that Unicode characters may be present in the message
100              file either directly in their UTF-8 encoded form, or by  use  of
101              the  backslash-u quoting recognized by Tcl evaluation.  The num‐
102              ber of message files which matched the  specification  and  were
103              loaded is returned.
104
105       ::msgcat::mcset locale src-string ?translate-string?
106              Sets  the  translation for src-string to translate-string in the
107              specified locale and the current namespace.  If translate-string
108              is  not  specified,  src-string  is used for both.  The function
109              returns translate-string.
110
111       ::msgcat::mcmset locale src-trans-list
112              Sets the translation for multiple source strings  in  src-trans-
113              list  in  the  specified locale and the current namespace.  src-
114              trans-list must have an even number of elements and  is  in  the
115              form  {src-string  translate-string ?src-string translate-string
116              ...?} ::msgcat::mcmset can be significantly faster than multiple
117              invocations  of ::msgcat::mcset. The function returns the number
118              of translations set.
119
120       ::msgcat::mcflset src-string ?translate-string?
121              Sets the translation for src-string to translate-string  in  the │
122              current namespace for the locale implied by the name of the mes‐ │
123              sage catalog being loaded via ::msgcat::mcload.   If  translate-
124              string is not specified, src-string is used for both.  The func‐ │
125              tion returns translate-string.
126
127       ::msgcat::mcflmset src-trans-list
128              Sets the translation for multiple source strings  in  src-trans-
129              list in the current namespace for the locale implied by the name │
130              of the message catalog being loaded via  ::msgcat::mcload.  src-
131              trans-list  must  have  an even number of elements and is in the │
132              form {src-string translate-string  ?src-string  translate-string
133              ...?} ::msgcat::mcflmset can be significantly faster than multi‐ │
134              ple invocations of ::msgcat::mcflset. The function  returns  the │
135              number of translations set.
136
137       ::msgcat::mcunknown locale src-string
138              This routine is called by ::msgcat::mc in the case when a trans‐
139              lation for src-string is not defined in the current locale.  The
140              default  action  is to return src-string.  This procedure can be
141              redefined by the application, for example to log error  messages
142              for  each  unknown string.  The ::msgcat::mcunknown procedure is
143              invoked at the same stack context as the call  to  ::msgcat::mc.
144              The  return  value  of ::msgcat::mcunknown is used as the return
145              value for the call to ::msgcat::mc.
146

LOCALE SPECIFICATION

148       The locale is specified to msgcat by a locale string passed  to  ::msg‐
149       cat::mclocale.   The  locale  string  consists  of  a language code, an
150       optional country code, and an optional system-specific code, each sepa‐
151       rated  by  “_”.   The country and language codes are specified in stan‐
152       dards ISO-639 and ISO-3166.  For example,  the  locale  “en”  specifies
153       English and “en_US” specifies U.S. English.
154
155       When  the  msgcat  package  is  first loaded, the locale is initialized
156       according  to  the  user's  environment.   The  variables  env(LC_ALL),
157       env(LC_MESSAGES),  and  env(LANG)  are examined in order.  The first of
158       them to have a non-empty value is used to determine the initial locale.
159       The value is parsed according to the XPG4 pattern
160
161              language[_country][.codeset][@modifier]
162
163       to extract its parts.  The initial locale is then set by calling ::msg‐
164       cat::mclocale with the argument
165
166              language[_country][_modifier]
167
168       On Windows and Cygwin, if none of those environment variables  is  set,
169       msgcat  will  attempt  to extract locale information from the registry.
170       From Windows Vista on, the RFC4747  locale  name  "lang-script-country-
171       options"  is  transformed to the locale as "lang_country_script" (Exam‐
172       ple: sr-Latn-CS -> sr_cs_latin). For Windows XP,  the  language  id  is
173       transformed  analoguously  (Example:  0c1a  -> sr_yu_cyrillic).  If all
174       these attempts to discover an initial locale from the  user's  environ‐
175       ment fail, msgcat defaults to an initial locale of “C”.
176
177       When  a  locale is specified by the user, a “best match” search is per‐
178       formed during string translation.  For example,  if  a  user  specifies
179       en_GB_Funky,  the  locales  “en_GB_Funky”, “en_GB”, “en” and (the empty
180       string) are searched in order until a matching  translation  string  is
181       found.  If no translation string is available, then ::msgcat::mcunknown
182       is called.
183

NAMESPACES AND MESSAGE CATALOGS

185       Strings stored in the  message  catalog  are  stored  relative  to  the
186       namespace from which they were added.  This allows multiple packages to
187       use the same strings without fear of collisions  with  other  packages.
188       It  also  allows  the  source  string  to  be shorter and less prone to
189       typographical error.
190
191       For example, executing the code
192
193              ::msgcat::mcset en hello "hello from ::"
194              namespace eval foo {
195                  ::msgcat::mcset en hello "hello from ::foo"
196              }
197              puts [::msgcat::mc hello]
198              namespace eval foo {puts [::msgcat::mc hello]}
199
200       will print
201
202              hello from ::
203              hello from ::foo
204
205       When searching for a translation of a message, the message catalog will
206       search  first  the  current  namespace,  then the parent of the current
207       namespace, and so on until  the  global  namespace  is  reached.   This
208       allows  child  namespaces  to  “inherit”  messages  from  their  parent
209       namespace.
210
211       For example, executing (in the “en” locale) the code
212
213              ::msgcat::mcset en m1 ":: message1"
214              ::msgcat::mcset en m2 ":: message2"
215              ::msgcat::mcset en m3 ":: message3"
216              namespace eval ::foo {
217                  ::msgcat::mcset en m2 "::foo message2"
218                  ::msgcat::mcset en m3 "::foo message3"
219              }
220              namespace eval ::foo::bar {
221                  ::msgcat::mcset en m3 "::foo::bar message3"
222              }
223              namespace import ::msgcat::mc
224              puts "[mc m1]; [mc m2]; [mc m3]"
225              namespace eval ::foo {puts "[mc m1]; [mc m2]; [mc m3]"}
226              namespace eval ::foo::bar {puts "[mc m1]; [mc m2]; [mc m3]"}
227
228       will print
229
230              :: message1; :: message2; :: message3
231              :: message1; ::foo message2; ::foo message3
232              :: message1; ::foo message2; ::foo::bar message3
233

LOCATION AND FORMAT OF MESSAGE FILES

235       Message files can be located in any directory, subject to the following
236       conditions:
237
238       [1]    All message files for a package are in the same directory.
239
240       [2]    The  message  file  name  is  a  msgcat  locale  specifier  (all
241              lowercase) followed by “.msg”.  For example:
242
243              es.msg    — spanish
244              en_gb.msg — United Kingdom English
245
246       Exception: The message file for the root locale is  called  “ROOT.msg”.
247       This  exception  is  made so as not to cause peculiar behavior, such as
248       marking the message file as “hidden” on Unix file systems.
249
250       [3]    The file contains a series of calls  to  mcflset  and  mcflmset,
251              setting  the  necessary  translation  strings  for the language,
252              likely enclosed in a namespace eval so that all  source  strings
253              are  tied  to the namespace of the package. For example, a short
254              es.msg might contain:
255
256              namespace eval ::mypackage {
257                  ::msgcat::mcflset "Free Beer!" "Cerveza Gracias!"
258              }
259
261       If a package is installed into a subdirectory of  the  tcl_pkgPath  and
262       loaded via package require, the following procedure is recommended.
263
264       [1]    During  package  installation,  create a subdirectory msgs under
265              your package directory.
266
267       [2]    Copy your *.msg files into that directory.
268
269       [3]    Add the following command to your package initialization script:
270
271              # load language files, stored in msgs subdirectory
272              ::msgcat::mcload [file join [file dirname [info script]] msgs]
273

POSITIONAL CODES FOR FORMAT AND SCAN COMMANDS

275       It is possible that a message string used  as  an  argument  to  format
276       might  have  positionally  dependent  parameters  that might need to be
277       repositioned.  For example, it  might  be  syntactically  desirable  to
278       rearrange the sentence structure while translating.
279
280              format "We produced %d units in location %s" $num $city
281              format "In location %s we produced %d units" $city $num
282
283       This can be handled by using the positional parameters:
284
285              format "We produced %1\$d units in location %2\$s" $num $city
286              format "In location %2\$s we produced %1\$d units" $num $city
287
288       Similarly,  positional  parameters  can  be  used  with scan to extract
289       values from internationalized strings. Note that it is not necessary to
290       pass  the  output  of  ::msgcat::mc  to format directly; by passing the
291       values to substitute in as arguments, the  formatting  substitution  is
292       done directly.
293
294              msgcat::mc {Produced %1$d at %2$s} $num $city
295              # ... where that key is mapped to one of the
296              # human-oriented versions by msgcat::mcset
297

CREDITS

299       The message catalog code was developed by Mark Harrison.
300

SEE ALSO

302       format(n), scan(n), namespace(n), package(n)
303

KEYWORDS

305       internationalization,   i18n,   localization,   l10n,   message,  text,
306       translation
307
308
309
310msgcat                                1.5                            msgcat(n)
Impressum