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".
7 # Copyright (c) 1998 by Scriptics Corporation.
8 # Copyright (c) 1998 by Mark Harrison.
10 # See the file "license.terms" for information on usage and redistribution
11 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 package provide msgcat 1.1
17 namespace eval msgcat {
18 namespace export mc mcset mclocale mcpreferences mcunknown
20 # Records the current locale as passed to mclocale
23 # Records the list of locales to search
26 # Records the mapping between source strings and translated strings. The
27 # array key is of the form "<locale>,<namespace>,<src>" and the value is
28 # the translated string.
34 # Find the translation for the given string based on the current
35 # locale setting. Check the local namespace first, then look in each
36 # parent namespace until the source is found. If additional args are
37 # specified, use the format command to work them into the traslated
41 # src The string to translate.
42 # args Args to pass to the format command
45 # Returns the translatd string. Propagates errors thrown by the
48 proc msgcat::mc {src args} {
49 # Check for the src in each namespace starting from the local and
50 # ending in the global.
52 set ns [uplevel {namespace current}]
55 foreach loc $::msgcat::loclist {
56 if {[info exists ::msgcat::msgs($loc,$ns,$src)]} {
57 if {[llength $args] == 0} {
58 return $::msgcat::msgs($loc,$ns,$src)
61 [list format $::msgcat::msgs($loc,$ns,$src)] \
66 set ns [namespace parent $ns]
68 # we have not found the translation
69 return [uplevel 1 [list [namespace origin mcunknown] \
70 $::msgcat::locale $src] $args]
75 # Query or set the current locale.
78 # newLocale (Optional) The new locale string. Locale strings
79 # should be composed of one or more sublocale parts
80 # separated by underscores (e.g. en_US).
83 # Returns the current locale.
85 proc msgcat::mclocale {args} {
86 set len [llength $args]
89 error {wrong # args: should be "mclocale ?newLocale?"}
92 set args [string tolower $args]
94 set ::msgcat::locale $args
95 set ::msgcat::loclist {}
97 foreach part [split $args _] {
98 set word [string trimleft "${word}_${part}" _]
99 set ::msgcat::loclist [linsert $::msgcat::loclist 0 $word]
102 return $::msgcat::locale
105 # msgcat::mcpreferences --
107 # Fetch the list of locales used to look up strings, ordered from
108 # most preferred to least preferred.
114 # Returns an ordered list of the locales preferred by the user.
116 proc msgcat::mcpreferences {} {
117 return $::msgcat::loclist
122 # Attempt to load message catalogs for each locale in the
123 # preference list from the specified directory.
126 # langdir The directory to search.
129 # Returns the number of message catalogs that were loaded.
131 proc msgcat::mcload {langdir} {
133 foreach p [::msgcat::mcpreferences] {
134 set langfile [file join $langdir $p.msg]
135 if {[file exists $langfile]} {
137 uplevel [list source $langfile]
145 # Set the translation for a given string in a specified locale.
148 # locale The locale to use.
149 # src The source string.
150 # dest (Optional) The translated string. If omitted,
151 # the source string is used.
154 # Returns the new locale.
156 proc msgcat::mcset {locale src {dest ""}} {
157 if {[string equal $dest ""]} {
161 set ns [uplevel {namespace current}]
163 set ::msgcat::msgs([string tolower $locale],$ns,$src) $dest
167 # msgcat::mcunknown --
169 # This routine is called by msgcat::mc if a translation cannot
170 # be found for a string. This routine is intended to be replaced
171 # by an application specific routine for error reporting
172 # purposes. The default behavior is to return the source string.
173 # If additional args are specified, the format command will be used
174 # to work them into the traslated string.
177 # locale The current locale.
178 # src The string to be translated.
179 # args Args to pass to the format command
182 # Returns the translated value.
184 proc msgcat::mcunknown {locale src args} {
185 if {[llength $args]} {
186 return [eval [list format $src] $args]
192 # Initialize the default locale
194 namespace eval msgcat {
195 # set default locale, try to get from environment
196 if {[info exists ::env(LANG)]} {
197 mclocale $::env(LANG)