Index: openacs-4/packages/acs-lang/tcl/acs-lang-init.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/acs-lang/tcl/acs-lang-init.tcl,v
diff -u -r1.1 -r1.2
--- openacs-4/packages/acs-lang/tcl/acs-lang-init.tcl	23 Oct 2002 11:50:50 -0000	1.1
+++ openacs-4/packages/acs-lang/tcl/acs-lang-init.tcl	11 Nov 2002 09:36:31 -0000	1.2
@@ -12,4 +12,7 @@
 ad_schedule_proc -once t 5 lang::catalog::import_from_all_files
 
 # Cache the message catalog from the database
-lang::message::cache
+global message_cache_loaded_p
+if { ![info exists message_cache_loaded_p] } {
+    lang::message::cache
+}
Index: openacs-4/packages/acs-lang/tcl/lang-message-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/acs-lang/tcl/lang-message-procs.tcl,v
diff -u -r1.9 -r1.10
--- openacs-4/packages/acs-lang/tcl/lang-message-procs.tcl	5 Nov 2002 17:03:43 -0000	1.9
+++ openacs-4/packages/acs-lang/tcl/lang-message-procs.tcl	11 Nov 2002 09:36:31 -0000	1.10
@@ -217,67 +217,81 @@
         
         @return A localized piece of text.
     } { 
-        # Peter TODO: add translation links
-        # Peter TODO/FIXME: Should we prefix with ad_conn package_key if the lookup fails?
-    
-        set system_locale [parameter::get -package_id [apm_package_id_from_key acs-lang] -parameter SiteWideLocale]
-
-        # Set default locale if none was provided
+        # If the cache hasn't been loaded - do so now
+        # Peter: should we go to the database on first hit and cache the messages as they are used
+        # instead of loading the whole cache up-front?
+        global message_cache_loaded_p
+        if { ![info exists message_cache_loaded_p] } {
+            lang::message::cache
+        }
+        
         if { [empty_string_p $locale] } {
+            # No locale provided
 
             global ad_conn
             if { [info exists ad_conn] } {
                 # We are in an HTTP connection (request) so use that locale
                 set locale [ad_conn locale]
             } else {
                 # There is no HTTP connection - resort to system locale
+                set system_locale [parameter::get -package_id [apm_package_id_from_key acs-lang] -parameter SiteWideLocale]
                 set locale $system_locale
             }
-        }
-        
-        if { [string length $locale] == 2 } {
-    
-            # it's a language and not a locale
+        } elseif { [string length $locale] == 2 } {
+            # Only language provided
+
             # let's get the default locale for this language
             # The cache is flushed if the default locale for this language is
-            # is changed.
+            # changed.
             set locale [util_memoize [list ad_locale_locale_from_lang $locale]]    
         } 
     
         if { [nsv_exists lang_message_$locale $key] } {
-            # Message catalog lookup succeeded
+            # Message exists in the given locale
+
             set return_value [nsv_get lang_message_$locale $key]
+            # Do any variable substitutions (interpolation of variables)
+            if { [llength $substitution_list] > 0 || ($upvar_level >= 1 && [string first "%" $return_value] != -1) } {
+                set return_value [lang::message::format $return_value $substitution_list [expr $upvar_level + 1]]
+            }
 
         } else {
             # There is no entry in the message catalog for the given locale
 
-            # is there any thing under this message key in the default locale
-
             if { [nsv_exists lang_message_en_US $key] != 0 } {
-                set return_value "$default: $key"
+                # The key exists but there is no translation in the current locale
+
+                if { ![lang::util::translator_mode_p] } {
+                    # We are not in translator mode
+
+                    if { [string equal $default "TRANSLATION MISSING"] } {
+                        set return_value "$default: $key"
+                    } else {
+                        set return_value $default
+                    }
+                } else {
+                    # Translator mode - return a translation link
+
+                    set key_split [split $key "."]
+                    set package_key_part [lindex $key_split 0]
+                    set message_key_part [lindex $key_split 1]
+                    
+                    set return_url [ad_conn url]
+                    if { [ns_getform] != "" } {
+                        append return_url "?[export_entire_form_as_url_vars]"
+                    }
+                    
+                    set return_value "&nbsp;<a href=\"/acs-lang/admin/edit-localized-message?[export_vars { { message_key $message_key_part } { locales $locale } { package_key $package_key_part } return_url }]\"><span style=\"background-color: yellow\"><font size=\"-2\">$message_key_part - TRANSLATE</font></span></a>&nbsp;"
+                }
+
             } {
+                # The key doesn't exist - this is a programming error
+
                 set return_value "NO KEY: $key"
+                ns_log Error "lang::message::lookup key doesn't exist: $key"
             }
-
-            if { [lang::util::translator_mode_p] } {
-                set key_split [split $key "."]
-                set package_key_part [lindex $key_split 0]
-                set message_key_part [lindex $key_split 1]
-                
-                set return_url [ad_conn url]
-                if { [ns_getform] != "" } {
-                    append return_url "?[export_entire_form_as_url_vars]"
-                }
-                
-                set return_value "&nbsp;<a href=\"/acs-lang/admin/edit-localized-message?[export_vars { { message_key $message_key_part } { locales $locale } { package_key $package_key_part } return_url }]\"><span style=\"background-color: yellow\"><font size=\"-2\">$message_key_part - TRANSLATE</font></span></a>&nbsp;"
-            }
         }
 
-        # Do any variable substitutions (interpolation of variables)
-        if { [llength $substitution_list] > 0 || ($upvar_level >= 1 && [string first "%" $return_value] != -1) } {
-            set return_value [lang::message::format $return_value $substitution_list [expr $upvar_level + 1]]
-        }
-
         return $return_value
     }
 
@@ -316,6 +330,8 @@
     } {
         # We segregate messages by language. It might reduce contention
         # if we segregage instead by package. Check for problems with ns_info locks.
+        global message_cache_loaded_p
+        set message_cache_loaded_p 1
         
         set i 0 
         db_foreach select_locale_keys {} {
Index: openacs-4/packages/acs-lang/tcl/lang-util-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/acs-lang/tcl/lang-util-procs.tcl,v
diff -u -r1.14 -r1.15
--- openacs-4/packages/acs-lang/tcl/lang-util-procs.tcl	10 Nov 2002 20:07:16 -0000	1.14
+++ openacs-4/packages/acs-lang/tcl/lang-util-procs.tcl	11 Nov 2002 09:36:31 -0000	1.15
@@ -595,13 +595,21 @@
         "not translated" message.
         
         @author Lars Pind (lars@collaboraid.biz)
-        @create-date October 24, 2002
+        @creation-date October 24, 2002
 
-        @return 1 if translator mode is enabled, 0 otherwise.
+        @return 1 if translator mode is enabled, 0 otherwise. Returns 0 if there is
+                no HTTP connection.
 
         @see lang::util::translator_mode_set
     } {
-        return [ad_get_client_property -default 0 acs-lang translator_mode_p]
+        global ad_conn
+        if { [info exists ad_conn] } {
+            # THere is an HTTP connection - return the client property
+            return [ad_get_client_property -default 0 acs-lang translator_mode_p]
+        } else {
+            # No HTTP connection
+            return 0
+        }
     }
     
     ad_proc -public translator_mode_set {
@@ -611,7 +619,7 @@
         not. 
         
         @author Lars Pind (lars@collaboraid.biz)
-        @create-date October 24, 2002
+        @creation-date October 24, 2002
 
         @param translator_mode_p 1 if you want translator mode to be enabled, 0 otherwise.