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.4.2
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

DESCRIPTION

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

COMMANDS

45       ::msgcat::mc src-string ?arg arg ...?
46              Returns a translation of src-string according to the user's cur‐
47              rent locale.  If additional arguments past src-string are given,
48              the format command is used to substitute  the  additional  argu‐
49              ments in the translation of src-string.
50
51              ::msgcat::mc  will  search  the  messages defined in the current
52              namespace for a translation of src-string; if none is found,  it
53              will  search  in  the parent of the current namespace, and so on
54              until it reaches the global namespace.  If no translation string
55              exists,  ::msgcat::mcunknown  is  called and the string returned
56              from ::msgcat::mcunknown is returned.
57
58              ::msgcat::mc is the main function used to localize  an  applica‐
59              tion.   Instead of using an English string directly, an applica‐
60              tion can pass the English string through  ::msgcat::mc  and  use
61              the  result.  If an application is written for a single language
62              in this fashion, then it is easy to add support  for  additional
63              languages later simply by defining 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

LOCALE SPECIFICATION

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”, “en”  and  (the  empty │
151       string)  are  searched  in order until a matching translation string is
152       found.  If no translation string is available, then ::msgcat::mcunknown
153       is called.
154

NAMESPACES AND MESSAGE CATALOGS

156       Strings  stored  in  the  message  catalog  are  stored relative to the
157       namespace from which they were added.  This allows multiple packages to
158       use  the  same  strings without fear of collisions with other packages.
159       It also allows the source string  to  be  shorter  and  less  prone  to
160       typographical error.
161
162       For example, executing the code
163              ::msgcat::mcset en hello "hello from ::"
164              namespace eval foo {
165                 ::msgcat::mcset en hello "hello from ::foo"
166              }
167              puts [::msgcat::mc hello]
168              namespace eval foo {puts [::msgcat::mc hello]}
169       will print
170              hello from ::
171              hello from ::foo
172
173       When searching for a translation of a message, the message catalog will
174       search first the current namespace, then  the  parent  of  the  current
175       namespace,  and  so  on  until  the  global namespace is reached.  This
176       allows  child  namespaces  to  “inherit”  messages  from  their  parent
177       namespace.
178
179       For example, executing (in the “en” locale) the code
180              ::msgcat::mcset en m1 ":: message1"
181              ::msgcat::mcset en m2 ":: message2"
182              ::msgcat::mcset en m3 ":: message3"
183              namespace eval ::foo {
184                 ::msgcat::mcset en m2 "::foo message2"
185                 ::msgcat::mcset en m3 "::foo message3"
186              }
187              namespace eval ::foo::bar {
188                 ::msgcat::mcset en m3 "::foo::bar message3"
189              }
190              namespace import ::msgcat::mc
191              puts "[mc m1]; [mc m2]; [mc m3]"
192              namespace eval ::foo {puts "[mc m1]; [mc m2]; [mc m3]"}
193              namespace eval ::foo::bar {puts "[mc m1]; [mc m2]; [mc m3]"}
194       will print
195              :: message1; :: message2; :: message3
196              :: message1; ::foo message2; ::foo message3
197              :: message1; ::foo message2; ::foo::bar message3
198

LOCATION AND FORMAT OF MESSAGE FILES

200       Message files can be located in any directory, subject to the following
201       conditions:
202
203       [1]    All message files for a package are in the same directory.
204
205       [2]    The  message  file  name  is  a  msgcat  locale  specifier  (all
206              lowercase) followed by “.msg”.  For example:
207              es.msg    — spanish
208              en_gb.msg — United Kingdom English
209       Exception:  The  message file for the root locale is called “ROOT.msg”. │
210       This exception is made so as not to cause peculiar  behavior,  such  as │
211       marking the message file as “hidden” on Unix file systems.
212
213       [3]    The file contains a series of calls to mcset and mcmset, setting
214              the necessary  translation  strings  for  the  language,  likely
215              enclosed in a namespace eval so that all source strings are tied
216              to the namespace of the package. For  example,  a  short  es.msg
217              might contain:
218              namespace eval ::mypackage {
219                 ::msgcat::mcset es "Free Beer!" "Cerveza Gracias!"
220              }
221
223       If  a  package  is installed into a subdirectory of the tcl_pkgPath and
224       loaded via package require, the following procedure is recommended.
225
226       [1]    During package installation, create a  subdirectory  msgs  under
227              your package directory.
228
229       [2]    Copy your *.msg files into that directory.
230
231       [3]
232               Add  the  following  command  to  your  package  initialization
233              script:
234              # load language files, stored in msgs subdirectory
235              ::msgcat::mcload [file join [file dirname [info script]] msgs]
236

POSITIONAL CODES FOR FORMAT AND SCAN COMMANDS

238       It is possible that a message string used  as  an  argument  to  format
239       might  have  positionally  dependent  parameters  that might need to be
240       repositioned.  For example, it  might  be  syntactically  desirable  to
241       rearrange the sentence structure while translating.
242              format "We produced %d units in location %s" $num $city
243              format "In location %s we produced %d units" $city $num
244
245       This can be handled by using the positional parameters:
246              format "We produced %1\$d units in location %2\$s" $num $city
247              format "In location %2\$s we produced %1\$d units" $num $city
248
249       Similarly,  positional  parameters  can  be  used  with scan to extract
250       values from internationalized strings.
251

CREDITS

253       The message catalog code was developed by Mark Harrison.
254

SEE ALSO

256       format(n), scan(n), namespace(n), package(n)
257

KEYWORDS

259       internationalization,  i18n,   localization,   l10n,   message,   text,
260       translation
261
262
263
264msgcat                                1.4                            msgcat(n)
Impressum