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.29.2.2 -r1.29.2.3 --- openacs-4/packages/acs-lang/tcl/lang-util-procs.tcl 11 Feb 2004 16:00:08 -0000 1.29.2.2 +++ openacs-4/packages/acs-lang/tcl/lang-util-procs.tcl 9 Mar 2004 13:13:52 -0000 1.29.2.3 @@ -320,25 +320,34 @@ @author Peter marklund (peter@collaboraid.biz) } { + # Return quickly for the fairly frequent case where there are no embedded message keys + if { ![string match "*#*" $string_with_hashes] } { + return $string_with_hashes + } + set indices_list [get_hash_indices $string_with_hashes] - set subst_string $string_with_hashes + set subst_string "" + set start_idx 0 foreach item_idx $indices_list { # The replacement string starts and ends with a hash mark set replacement_string [string range $string_with_hashes [lindex $item_idx 0] \ [lindex $item_idx 1]] set message_key [string range $replacement_string 1 [expr [string length $replacement_string] - 2]] - + # Attempt a message lookup set message_value [lang::message::lookup [ad_conn locale] $message_key "" "" 2] # Replace the string # LARS: We don't use regsub here, because regsub interprets certain characters # in the replacement string specially. - set subst_string [string range $string_with_hashes 0 [expr [lindex $item_idx 0]-1]] + append subst_string [string range $string_with_hashes $start_idx [expr [lindex $item_idx 0]-1]] append subst_string $message_value - append subst_string [string range $string_with_hashes [expr [lindex $item_idx 1]+1] end] + + set start_idx [expr [lindex $item_idx 1] + 1] } + + append subst_string [string range $string_with_hashes $start_idx end] return $subst_string } Index: openacs-4/packages/acs-lang/tcl/test/acs-lang-test-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-lang/tcl/test/acs-lang-test-procs.tcl,v diff -u -r1.11.2.1 -r1.11.2.2 --- openacs-4/packages/acs-lang/tcl/test/acs-lang-test-procs.tcl 9 Dec 2003 13:41:52 -0000 1.11.2.1 +++ openacs-4/packages/acs-lang/tcl/test/acs-lang-test-procs.tcl 9 Mar 2004 13:13:54 -0000 1.11.2.2 @@ -967,3 +967,46 @@ lang::test::teardown_test_package } } + +aa_register_case localize { + Test the lang::util::localize proc. + + @author Peter Marklund +} { + set package_key "acs-lang" + set message_key "__test-key" + set message "Test message" + + aa_run_with_teardown \ + -rollback \ + -test_code { + + # Create a temporary test message to test with + lang::message::register en_US $package_key $message_key $message + + # Create some random character strings to surround the embedded key + set pre_text "a;iuwgon#" + set message_key_embedded "#${package_key}.${message_key}#" + + # Test replacements + set text1 $message_key_embedded + aa_equals "One message key with no surrounding text" [lang::util::localize $text1] "${message}" + + set text1 "${pre_text}${message_key_embedded}${post_text}" + aa_equals "One message key with surrounding text" [lang::util::localize $text1] "${pre_text}${message}${post_text}" + + set text1 "${pre_text}${message_key_embedded}" + aa_equals "One message key with text before" [lang::util::localize $text1] "${pre_text}${message}" + + set text1 "${message_key_embedded}${post_text}" + aa_equals "One message key with text after" [lang::util::localize $text1] "${message}${post_text}" + + set text1 "${pre_text}${message_key_embedded}${post_text}${pre_text}${message_key_embedded}${post_text}" + aa_equals "Two message keys with surrounding text" [lang::util::localize $text1] \ + "${pre_text}${message}${post_text}${pre_text}${message}${post_text}" + } -teardown_code { + # We need to clear the cache + lang::message::unregister $package_key $message_key + } +}