Seth Woolley's Man Viewer

Manual for msgcat - man n msgcat

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

msgcat(1,n)(n)                    Tcl Bundled Packages                    msgcat(1,n)(n)



NAME
       msgcat(1,n) - Tcl message catalog

SYNOPSIS
       package require Tcl 8.2

       package require msgcat(1,n) 1.3

       ::msgcat::mc src-string ?arg arg ...?

       ::msgcat::mcmax ?src-string src-string ...?

       ::msgcat::mclocale ?newLocale?

       ::msgcat::mcpreferences

       ::msgcat::mcload dirname

       ::msgcat::mcset locale(3,5,7) src-string ?translate-string?

       ::msgcat::mcmset locale(3,5,7) src-trans-list

       ::msgcat::mcunknown locale(3,5,7) src-string


DESCRIPTION
       The msgcat(1,n) package provides a set(7,n,1 builtins) of functions that can be used to man-
       age multi-lingual user interfaces.   Text  strings  are  defined  in(1,8)  a
       ``message  catalog''  which  is  independent  from the application, and
       which can be edited or  localized  without  modifying  the  application
       source  code.   New  languages  or locales are provided by adding a new
       file(1,n) to the message catalog.

       Use of the message catalog is optional by any application  or  package,
       but  is  encouraged  if(3,n) the application or package wishes to be enabled
       for multi-lingual applications.


COMMANDS
       ::msgcat::mc src-string ?arg arg ...?
              Returns a translation of src-string according to the user's cur-
              rent locale.  If additional arguments past src-string are given,
              the format command is used to substitute  the  additional  argu-
              ments in(1,8) the translation of src-string.

              ::msgcat::mc  will  search  the  messages defined in(1,8) the current
              namespace for a translation of src-string; if(3,n) none is found,  it
              will  search  in(1,8)  the parent of the current namespace, and so on
              until it reaches the global namespace.  If no translation string(3,n)
              exists,  ::msgcat::mcunknown  is  called and the string(3,n) returned
              from ::msgcat::mcunknown is returned.

       ::msgcat::mc is the main function  used  to  localize  an  application.
       Instead  of  using  an English string(3,n) directly, an application can pass
       the English string(3,n) through ::msgcat::mc and  use  the  result.   If  an
       application  is  written for a single language in(1,8) this fashion, then it
       is easy to add support for additional languages later simply by  defin-
       ing new message catalog entries.

       ::msgcat::mcmax ?src-string src-string ...?
              Given several source strings, ::msgcat::mcmax returns the length
              of the longest translated string.  This is useful when designing
              localized GUIs, which may require that all buttons, for example,
              be a fixed width (which will be the width of the widest button).

       ::msgcat::mclocale ?newLocale?
              This  function  sets  the  locale(3,5,7) to newLocale.  If newLocale is
              omitted, the current locale(3,5,7) is returned, otherwise  the  current
              locale(3,5,7)  is  set(7,n,1 builtins)  to  newLocale.   msgcat(1,n) stores and compares the
              locale(3,5,7) in(1,8) a case-insensitive manner, and returns locales in(1,8) low-
              ercase.   The  initial locale(3,5,7) is determined by the locale(3,5,7) speci-
              fied in(1,8) the user's environment.  See LOCALE SPECIFICATION  below
              for a description of the locale(3,5,7) string(3,n) format.

       ::msgcat::mcpreferences
              Returns  an  ordered  list of the locales preferred by the user,
              based on the user's language specification.  The list is ordered
              from  most  specific  to  least preference.  The list is derived
              from the current locale(3,5,7) set(7,n,1 builtins) in(1,8) msgcat(1,n) by  msgcat::mclocale,  and
              cannot be set(7,n,1 builtins) independently.  For example, if(3,n) the current locale(3,5,7)
              is en_US_funky, then msgcat::mcpreferences returns  {en_US_funky
              en_US en}.

       ::msgcat::mcload dirname
              Searches  the  specified directory for files that match the lan-
              guage specifications returned by  ::msgcat::mcpreferences  (note
              that  these  are  all lowercase), extended by the file(1,n) extension
              ``.msg''.  Each matching file(1,n) is read(2,n,1 builtins) in(1,8) order, assuming a UTF-8
              encoding.  The file(1,n) contents are then evaluated as a Tcl script.
              This means that non-Latin characters may be present in(1,8) the  mes-
              sage file(1,n) either directly in(1,8) their UTF-8 encoded form, or by use
              of the backslash-u quoting recognized by  Tcl  evaluation.   The
              number of message files which matched the specification and were
              loaded is returned.

       ::msgcat::mcset locale(3,5,7) src-string ?translate-string?
              Sets the translation for src-string to translate-string  in(1,8)  the
              specified locale(3,5,7) and the current namespace.  If translate-string
              is not specified, src-string is used  for  both.   The  function
              returns translate-string.

       ::msgcat::mcmset locale(3,5,7) src-trans-list
              Sets  the  translation for multiple source strings in(1,8) src-trans-
              list in(1,8) the specified locale(3,5,7) and the  current  namespace.   src-
              trans-list  must  have  an even number of elements and is in(1,8) the
              form {src-string translate-string  ?src-string  translate-string
              ...?}  msgcat::mcmset  can be significantly faster than multiple
              invocations of msgcat::mcset. The function returns the number of
              translations set.

       ::msgcat::mcunknown locale(3,5,7) src-string
              This routine is called by ::msgcat::mc in(1,8) the case when a trans-
              lation for src-string is not defined in(1,8) the current locale.  The
              default  action  is to return src-string.  This procedure can be
              redefined by the application, for example to log error(8,n)  messages
              for  each  unknown string.  The ::msgcat::mcunknown procedure is
              invoked at the same stack context as the call  to  ::msgcat::mc.
              The  return  value  of ::msgcat::mcunknown is used as the return
              value for the call to ::msgcat::mc.


LOCALE SPECIFICATION
       The locale(3,5,7) is specified to msgcat(1,n) by a locale(3,5,7) string(3,n) passed  to  ::msg-
       cat::mclocale.   The  locale(3,5,7)  string(3,n)  consists  of  a language code, an
       optional country code, and an optional system-specific code, each sepa-
       rated  by ``_''.  The country and language codes are specified in(1,8) stan-
       dards ISO-639 and ISO-3166.  For example, the locale(3,5,7)  ``en''  specifies
       English and ``en_US'' specifies U.S. English.

       When  the  msgcat(1,n)  package  is  first loaded, the locale(3,5,7) is initialized
       according  to  the  user's  environment.   The  variables  env(1,3)(LC_ALL),
       env(1,3)(LC_MESSAGES),  and  env(1,3)(LANG)  are examined in(1,8) order.  The first of
       them to have a non-empty value is used to determine the initial locale.
       The  value  is  parsed  according  to  the XPG4 pattern language[_coun-
       try][.codeset][@modifier] to extract its parts.  The initial locale(3,5,7)  is
       then  set(7,n,1 builtins) by calling msgcat::mclocale with the argument language[_coun-
       try][_modifier] On Windows, if(3,n) none of those environment  variables  is
       set(7,n,1 builtins),  msgcat(1,n)  will  attempt to extract locale(3,5,7) information from the reg-
       istry.  If all these attempts to discover(1,3,5) an initial  locale(3,5,7)  from  the
       user's environment fail, msgcat(1,n) defaults to an initial locale(3,5,7) of ``C''.

       When a locale(3,5,7) is specified by the user, a ``best match'' search is per-
       formed  during  string(3,n)  translation.   For example, if(3,n) a user specifies
       en_GB_Funky, the locales ``en_GB_Funky'',  ``en_GB'',  and  ``en''  are
       searched  in(1,8) order until a matching translation string(3,n) is found.  If no
       translation string(3,n) is available, then ::msgcat::unknown is called.


NAMESPACES AND MESSAGE CATALOGS
       Strings stored in(1,8) the message catalog are stored relative to the names-
       pace  from which they were added.  This allows multiple packages to use
       the same strings without fear of collisions with  other  packages.   It
       also  allows  the  source  string(3,n) to be shorter and less(1,3) prone to typo-
       graphical error.

       For example, executing the code mcset en hello "hello from  ::"  names-
       pace  eval  foo  {mcset  en  hello  "hello from ::foo"} puts(3,n) [mc hello]
       namespace eval foo {puts(3,n) [mc hello]} will print  hello  from  ::  hello
       from ::foo

       When searching for a translation of a message, the message catalog will
       search first the current namespace, then  the  parent  of  the  current
       namespace,  and  so  on  until  the  global namespace is reached.  This
       allows child namespaces to "inherit" messages from their parent  names-
       pace.

       For  example, executing (in(1,8) the ``en'' locale(3,5,7)) the code mcset en m1 "::
       message1" mcset en m2 ":: message2" mcset en m3 ":: message3" namespace
       eval ::foo {
           mcset en m2 "::foo message2"
           mcset en m3 "::foo message3" } namespace eval ::foo::bar {
           mcset  en  m3  "::foo::bar  message3" } puts(3,n) "[mc m1]; [mc m2]; [mc
       m3]" namespace eval ::foo {puts(3,n) "[mc m1]; [mc m2]; [mc m3]"}  namespace
       eval  ::foo::bar  {puts(3,n) "[mc m1]; [mc m2]; [mc m3]"} will print :: mes-
       sage1; :: message2; :: message3 :: message1; ::foo message2; ::foo mes-
       sage3 :: message1; ::foo message2; ::foo::bar message3


LOCATION AND FORMAT OF MESSAGE FILES
       Message files can be located in(1,8) any directory, subject to the following
       conditions:

       [1]    All message files for a package are in(1,8) the same directory.

       [2]    The message file(1,n) name is a msgcat(1,n) locale(3,5,7) specifier  (all  lower-
              case)  followed  by ``.msg''.  For example: es.msg    -- spanish
              en_gb.msg -- United Kingdom English

       [3]    The file(1,n) contains a series of calls to mcset and mcmset, setting
              the  necessary  translation  strings  for  the  language, likely
              enclosed in(1,8) a namespace eval so that all source strings are tied
              to  the  namespace  of  the package. For example, a short es.msg
              might contain: namespace eval ::mypackage {
                  ::msgcat::mcset es "Free Beer!" "Cerveza Gracias!"  }


RECOMMENDED MESSAGE SETUP FOR PACKAGES
       If a package is installed into a subdirectory of  the  tcl_pkgPath  and
       loaded via package require, the following procedure is recommended.

       [1]    During  package  installation,  create a subdirectory msgs under
              your package directory.

       [2]    Copy your *.msg files into that directory.

       [3]     Add  the  following  command  to  your  package  initialization
              script:  #  load(7,n)  language  files,  stored  in(1,8) msgs subdirectory
              ::msgcat::mcload [file(1,n) join(1,n) [file(1,n) dirname [info(1,5,n) script]] msgs]


POSITIONAL CODES FOR FORMAT AND SCAN COMMANDS
       It is possible that a message string(3,n) used  as  an  argument  to  format
       might  have  positionally  dependent  parameters  that might need to be
       repositioned.  For example, it  might  be  syntactically  desirable  to
       rearrange  the  sentence  structure while translating.  format "We pro-
       duced %d units(1,7) in(1,8) location %s" $num $city format  "In  location  %s  we
       produced %d units(1,7)" $city $num

       This can be handled by using the positional parameters: format "We pro-
       duced %1\$d units(1,7) in(1,8) location %2\$s" $num  $city  format  "In  location
       %2\$s we produced %1\$d units(1,7)" $num $city

       Similarly,  positional parameters can be used with scan to extract val-
       ues from internationalized strings.


CREDITS
       The message catalog code was developed by Mark Harrison.


SEE ALSO
       format(n), scan(n), namespace(n), package(n)


KEYWORDS
       internationalization, i18n, localization, l10n, message, text, transla-
       tion



msgcat(1,n)                                1.3                            msgcat(1,n)(n)

References for this manual (incoming links)