hectorr
committed
on 14 Jun 18
Whitespace cleanup
/tcl/lang-message-procs.tcl (+11 -11)
621 621     in the given localized message with the value from a lookup in the value_array_list
622 622     with array_key (what's between the percentage sings). If value_array_list is not
623 623     provided then attempt to fetch variable values the number of levels up given by
624 624     upvar_level (defaults to 3 because this proc is typically invoked from the underscore
625 625     lookup proc).
626 626
627 627     Here is an example:
628 628
629 629     set localized_message "The %animal% jumped across the %barrier%. About 50% of the time, he stumbled, or maybe it was %%20 %times%."
630 630     set value_list { animal "frog" barrier "fence" }
631 631
632 632     ns_log notice formatted=[format $localized_message $value_list]
633 633    
634 634     The output from the example is:
635 635
636 636     The frog jumped across the fence. About 50% of the time, he stumbled, or maybe it was %20 %times%.
637 637 } {       
638 638     array set value_array $value_array_list
639 639     set value_array_keys [array names value_array]
640 640     set remaining_message $localized_message
641       set formated_message ""
  641     set formatted_message ""
642 642     while { [regexp [embedded_vars_regexp] $remaining_message match before_percent percent_match remaining_message] } {
643 643
644           append formated_message $before_percent
  644         append formatted_message $before_percent
645 645
646 646         if {$percent_match eq "%%"} {
647 647             # A quoted percent sign
648               append formated_message "%"
  648             append formatted_message "%"
649 649         } else {
650 650             set variable_string [string range $percent_match 1 end-1]
651 651
652 652             if { [llength $value_array_list] > 0 } {
653 653                 # A substitution list is provided, the key should be in there
654 654                
655 655                 if {$variable_string ni $value_array_keys} {
656 656                     ns_log Warning "lang::message::format: The value_array_list \"$value_array_list\" does not contain the variable name $variable_string found in the message: $localized_message"
657 657                    
658 658                     # There is no value available to do the substitution with
659 659                     # so don't substitute at all
660                       append formated_message $percent_match
  660                     append formatted_message $percent_match
661 661                 } else {
662 662                     # Do the substitution
663                       append formated_message $value_array($variable_string)
  663                     append formatted_message $value_array($variable_string)
664 664                 }
665 665             } else {
666 666                 regexp {^([^.]+)(?:\.([^.]+))?$} $variable_string match variable_name array_key
667 667
668 668                 # No substitution list provided - attempt to fetch variable value
669 669                 # from scope calling lang::message::lookup
670 670                 upvar $upvar_level $variable_name local_variable
671 671
672 672                 if { [info exists local_variable] } {
673 673                     if { (![info exists array_key] || $array_key eq "") } {
674 674                         # Normal Tcl variable
675                           append formated_message $local_variable
  675                         append formatted_message $local_variable
676 676                     } else {
677 677                         # Array variable
678                           append formated_message $local_variable($array_key)
  678                         append formatted_message $local_variable($array_key)
679 679                     }
680 680                 } else {
681 681                     ns_log warning "Message contains a variable named '$variable_name' which doesn't exist in the caller's environment: message $localized_message"
682                       append formated_message "MISSING: variable '$variable_name' is not available"
  682                     append formatted_message "MISSING: variable '$variable_name' is not available"
683 683                 }
684 684             }
685 685         }
686 686     }
687 687
688 688     # Append text after the last match
689       append formated_message $remaining_message
  689     append formatted_message $remaining_message
690 690
691       return $formated_message
  691     return $formatted_message
692 692 }
693 693
694 694 ad_proc -private lang::message::embedded_vars_regexp {} {
695 695     The regexp pattern used to loop over variables embedded in
696 696     message catalog texts.
697 697
698 698     @author Peter Marklund (peter@collaboraid.biz)
699 699     @creation-date 12 November 2002
700 700 } {
701 701     return {^(.*?)(%%|%[-a-zA-Z0-9_:\.]+(?:;noquote)?%)(.*)$}
702 702 }
703 703
704 704 ad_proc -public lang::message::message_exists_p { locale key } {
705 705     Return 1 if message exists in given locale, 0 otherwise.
706 706
707 707     @author Peter Marklund
708 708 } {
709 709     # Make sure messages are in the cache
710 710     lang::message::cache
711 711