home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / MSGCAT.TCL < prev    next >
Encoding:
Text File  |  2000-08-08  |  4.9 KB  |  202 lines

  1. # msgcat.tcl --
  2. #
  3. #    This file defines various procedures which implement a
  4. #    message catalog facility for Tcl programs.  It should be
  5. #    loaded with the command "package require msgcat".
  6. #
  7. # Copyright (c) 1998 by Scriptics Corporation.
  8. # Copyright (c) 1998 by Mark Harrison.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. # RCS: @(#) $Id: msgcat.tcl,v 1.4.2.3 2000/08/08 17:53:42 hobbs Exp $
  13.  
  14. package provide msgcat 1.1
  15.  
  16. namespace eval msgcat {
  17.     namespace export mc mcset mclocale mcpreferences mcunknown
  18.  
  19.     # Records the current locale as passed to mclocale
  20.     variable locale ""
  21.  
  22.     # Records the list of locales to search
  23.     variable loclist {}
  24.  
  25.     # Records the mapping between source strings and translated strings.  The
  26.     # array key is of the form "<locale>,<namespace>,<src>" and the value is
  27.     # the translated string.
  28.     array set msgs {}
  29. }
  30.  
  31. # msgcat::mc --
  32. #
  33. #    Find the translation for the given string based on the current
  34. #    locale setting. Check the local namespace first, then look in each
  35. #    parent namespace until the source is found.  If additional args are
  36. #    specified, use the format command to work them into the traslated
  37. #    string.
  38. #
  39. # Arguments:
  40. #    src    The string to translate.
  41. #    args    Args to pass to the format command
  42. #
  43. # Results:
  44. #    Returns the translatd string.  Propagates errors thrown by the 
  45. #    format command.
  46.  
  47. proc msgcat::mc {src args} {
  48.     # Check for the src in each namespace starting from the local and
  49.     # ending in the global.
  50.  
  51.     set ns [uplevel {namespace current}]
  52.     
  53.     while {$ns != ""} {
  54.     foreach loc $::msgcat::loclist {
  55.         if {[info exists ::msgcat::msgs($loc,$ns,$src)]} {
  56.         if {[llength $args] == 0} {
  57.             return $::msgcat::msgs($loc,$ns,$src)
  58.         } else {
  59.             return [eval \
  60.                 [list format $::msgcat::msgs($loc,$ns,$src)] \
  61.                 $args]
  62.         }
  63.         }
  64.     }
  65.     set ns [namespace parent $ns]
  66.     }
  67.     # we have not found the translation
  68.     return [uplevel 1 [list [namespace origin mcunknown] \
  69.         $::msgcat::locale $src] $args]
  70. }
  71.  
  72. # msgcat::mclocale --
  73. #
  74. #    Query or set the current locale.
  75. #
  76. # Arguments:
  77. #    newLocale    (Optional) The new locale string. Locale strings
  78. #            should be composed of one or more sublocale parts
  79. #            separated by underscores (e.g. en_US).
  80. #
  81. # Results:
  82. #    Returns the current locale.
  83.  
  84. proc msgcat::mclocale {args} {
  85.     set len [llength $args]
  86.  
  87.     if {$len > 1} {
  88.     error {wrong # args: should be "mclocale ?newLocale?"}
  89.     }
  90.  
  91.     set args [string tolower $args]
  92.     if {$len == 1} {
  93.     set ::msgcat::locale $args
  94.     set ::msgcat::loclist {}
  95.     set word ""
  96.     foreach part [split $args _] {
  97.         set word [string trimleft "${word}_${part}" _]
  98.         set ::msgcat::loclist [linsert $::msgcat::loclist 0 $word]
  99.     }
  100.     }
  101.     return $::msgcat::locale
  102. }
  103.  
  104. # msgcat::mcpreferences --
  105. #
  106. #    Fetch the list of locales used to look up strings, ordered from
  107. #    most preferred to least preferred.
  108. #
  109. # Arguments:
  110. #    None.
  111. #
  112. # Results:
  113. #    Returns an ordered list of the locales preferred by the user.
  114.  
  115. proc msgcat::mcpreferences {} {
  116.     return $::msgcat::loclist
  117. }
  118.  
  119. # msgcat::mcload --
  120. #
  121. #    Attempt to load message catalogs for each locale in the
  122. #    preference list from the specified directory.
  123. #
  124. # Arguments:
  125. #    langdir        The directory to search.
  126. #
  127. # Results:
  128. #    Returns the number of message catalogs that were loaded.
  129.  
  130. proc msgcat::mcload {langdir} {
  131.     set x 0
  132.     foreach p [::msgcat::mcpreferences] {
  133.     set langfile [file join $langdir $p.msg]
  134.     if {[file exists $langfile]} {
  135.         incr x
  136.         uplevel [list source $langfile]
  137.     }
  138.     }
  139.     return $x
  140. }
  141.  
  142. # msgcat::mcset --
  143. #
  144. #    Set the translation for a given string in a specified locale.
  145. #
  146. # Arguments:
  147. #    locale        The locale to use.
  148. #    src        The source string.
  149. #    dest        (Optional) The translated string.  If omitted,
  150. #            the source string is used.
  151. #
  152. # Results:
  153. #    Returns the new locale.
  154.  
  155. proc msgcat::mcset {locale src {dest ""}} {
  156.     if {[string equal $dest ""]} {
  157.     set dest $src
  158.     }
  159.  
  160.     set ns [uplevel {namespace current}]
  161.  
  162.     set ::msgcat::msgs([string tolower $locale],$ns,$src) $dest
  163.     return $dest
  164. }
  165.  
  166. # msgcat::mcunknown --
  167. #
  168. #    This routine is called by msgcat::mc if a translation cannot
  169. #    be found for a string.  This routine is intended to be replaced
  170. #    by an application specific routine for error reporting
  171. #    purposes.  The default behavior is to return the source string.  
  172. #    If additional args are specified, the format command will be used
  173. #    to work them into the traslated string.
  174. #
  175. # Arguments:
  176. #    locale        The current locale.
  177. #    src        The string to be translated.
  178. #    args        Args to pass to the format command
  179. #
  180. # Results:
  181. #    Returns the translated value.
  182.  
  183. proc msgcat::mcunknown {locale src args} {
  184.     if {[llength $args]} {
  185.     return [eval [list format $src] $args]
  186.     } else {
  187.     return $src
  188.     }
  189. }
  190.  
  191. # Initialize the default locale
  192.  
  193. namespace eval msgcat {
  194.     # set default locale, try to get from environment
  195.     if {[info exists ::env(LANG)]} {
  196.         mclocale $::env(LANG)
  197.     } else {
  198.         mclocale "C"
  199.     }
  200. }
  201.