Msgcat and Mac OS X

The Tcl core library includes msgcat, a package which facilities program localization. msgcat provides means to define a catalog of messages in multiple languages and a procedure to retrieve messages appropriate to a specific locale, typically determined by cues from the operating system.

In general, to localize puts "Hello, world!", you provide equivalent strings for each supported locale and select the appropriate one at runtime by stating puts [mc "Hello, world!"]. For example, this command might print ┬íHola, mundo! if the locale is es. Default strings in the programmer’s native language can usually be used as keys to the message catalog.

Anyway, msgcat looks at a couple environment variables (or the Windows registry) to determine the proper locale. On Mac OS X, this works fine for scripts launched from the command line, but spiffy desktop applications don’t necessarily have access to the same environment variables. If no supported environment variables are available, msgcat uses the locale implied by the “Region” setting of the “Formats” pane of Mac OS X’s “International” system preferences. You can query this string from the command line with:

defaults read 'Apple Global Domain' AppleLocale

It is also available from within Tcl as the variable ::tcl::mac::locale.


To test localized Tcl desktop applications on Mac OS X without changing your system region, you can define the locale in ~/.MacOSX/environment.plist. Environment variables specified in this file are available to programs launched from the Finder, so msgcat will see these settings before resorting to the system locale. For instance, to set the locale to es, add this property to environment.plist:

<key>LANG</key>
<string>es</string>

See QA1067 for more information about environment.plist, including how to create it if it does not already exist. Note that variables defined in this file will be visible to all programs, and may therefore affect other applications.


Here’s how I usually comment the code I use to initialize msgcat:

# localization courtesy of Message Cat:
#
#  |\        /|
#  | \______/ |
#  /  _    _  \
#  | <l>  <l> |
#  \    __    /
# ----- \/ -----
#  --\__/\__/--
#

Posted on Tuesday, January 29th, 2008. Tags: , .