lang::util::replace_temporary_tags_with_lookups (public)

 lang::util::replace_temporary_tags_with_lookups file_list

Defined in packages/acs-lang/tcl/lang-util-procs.tcl

Modify the given ADP or Tcl files by replacing occurencies of message keys with message lookups (i.e. #package_key.message_key# for ADP files and [_ "package_key.message_key"] for Tcl files) and create entries in the catalog file for each of these keys. If the short hand form <#_ Some en_US text#> is used then the key will be auto-generated based on the text. Returns the number of replacements done. This procedure only reads from and writes to the catalog file specified (the en_US catalog file per default) of the package that the files belong to, the database is not accessed in any way.

Parameters:
file_list - A list of paths to .adp or .tcl files to do replacements in. The paths should be relative to $::acs::rootdir. All files must belong to the same package.
Author:
Peter marklund <peter@collaboraid.biz>

Partial Call Graph (max 5 caller/called nodes):
%3 test_util__replace_temporary_tags_with_lookups util__replace_temporary_tags_with_lookups (test acs-lang) lang::util::replace_temporary_tags_with_lookups lang::util::replace_temporary_tags_with_lookups test_util__replace_temporary_tags_with_lookups->lang::util::replace_temporary_tags_with_lookups lang::catalog::export lang::catalog::export (public) lang::util::replace_temporary_tags_with_lookups->lang::catalog::export lang::catalog::get_catalog_file_path lang::catalog::get_catalog_file_path (private) lang::util::replace_temporary_tags_with_lookups->lang::catalog::get_catalog_file_path lang::catalog::parse lang::catalog::parse (private) lang::util::replace_temporary_tags_with_lookups->lang::catalog::parse lang::catalog::read_file lang::catalog::read_file (private) lang::util::replace_temporary_tags_with_lookups->lang::catalog::read_file lang::message::register lang::message::register (public) lang::util::replace_temporary_tags_with_lookups->lang::message::register packages/acs-admin/www/apm/version-i18n-process-2.tcl packages/acs-admin/ www/apm/version-i18n-process-2.tcl packages/acs-admin/www/apm/version-i18n-process-2.tcl->lang::util::replace_temporary_tags_with_lookups

Testcases:
util__replace_temporary_tags_with_lookups
Source code:
    # Return if there are no files to process
    if { [llength $file_list] == 0 } {
        ns_log Warning "lang::util::replace_temporary_tags_with_lookups: Invoked with no files to process, returning"
        return
    }

    # Get package_key
    set first_file [lindex $file_list 0]
    if { ![regexp {/?packages/([^/]+)/} $first_file match package_key] } {
        error "lang::util::replace_temporary_tags_with_lookups: Could not extract package_key from file $first_file"
    }

    # Always create new keys in en_US
    set locale "en_US"

    # Read messages from any existing catalog file
    set catalog_file_path [lang::catalog::get_catalog_file_path  -package_key $package_key  -locale $locale]
    if { [file exists $catalog_file_path] } {
        set catalog_file_contents [lang::catalog::read_file $catalog_file_path]
        array set catalog_array [lang::catalog::parse $catalog_file_contents]
        array set messages_array $catalog_array(messages)
    } else {
        array set messages_array {}
    }

    # Keep track of how many message tags we have replaced (will be returned by this proc)
    set number_of_replacements "0"

    # Loop over and process each file
    foreach file $file_list {
        ns_log debug "lang::util::replace_temporary_tags_with_lookups: processing file $file"

        set full_file_path "$::acs::rootdir/$file"
        regexp {\.([^.]+)$} $file match file_ending

        # Attempt a backup of the file first. Do not overwrite an old backup file.
        if { [catch "file -- copy $full_file_path \"${full_file_path}.orig\"" errmsg] } {
            ns_log Warning "The file $full_file_path could not be backed up before message key extraction since backup file ${full_file_path}.orig already exists"
        }

        # Read the contents of the file
        set file_contents [template::util::read_file $full_file_path]

        set modified_file_contents $file_contents

        # Loop over each message tag in the file
        # Get the indices of the first and last char of the <#...#> text snippets
        set message_key_indices [lang::util::get_temporary_tags_indices $file_contents]
        foreach index_pair $message_key_indices {

            incr number_of_replacements

            lassign $index_pair tag_start_idx tag_end_idx
            set message_tag "[string range $file_contents $tag_start_idx $tag_end_idx]"

            # Extract the message key and the text from the message tag
            # The regexp on the message tag string should never fail as the message tag
            # was extracted with a known regexp
            if { ![regexp [message_tag_regexp$message_tag full_match  message_tag message_key new_text] } {

                ns_log Error [list lang::util::replace_temporary_tags_with_lookups - could not extract message key  and text from the message tag $message_tag in file $file. This means there is a  mismatch with the regexp that extracted the message key.]
                continue
            }

            # if the message key is the _ symbol (an underscore) then automatically generate a key
            # based on the message text
            if {$message_key eq "_"} {
                set message_key [suggest_key $new_text]
            }

            # If this is an adp file - replace adp variable syntax with percentage variables
            if {$file_ending eq "adp"} {
                set new_text [convert_adp_variables_to_percentage_signs $new_text]
            }

            # Check if the key already exists, if it does and texts differ - make key unique
            set key_comp_counter "0"
            set unique_key $message_key
            while { 1 } {

                if { [info exists messages_array($unique_key)] } {
                    # The key already exists

                    if {$messages_array($unique_key) eq $new_text} {
                        # New and old texts are identical - don't add the key
                        ns_log Notice [list lang::util::replace_temporary_tags_with_lookups -  message key $unique_key already exists in catalog  file with same value, will not add]

                        # We are done
                        break
                    } else {
                        # New and old texts differ, try to make the key unique and check again
                        set unique_key "${message_key}_[expr {${key_comp_counter} + 1}]"
                    }
                } else {
                    # The key is new - save it in the array for addition

                    if { $message_key ne $unique_key } {
                        # The message key had to be changed to be made unique
                        ns_log Warning [list lang::util::replace_temporary_tags_with_lookups -  The message key $message_key was changed to $unique_key  to be made unique. If the value was mistyped and should have been  the same as previously then you must manually remove the entry for  $unique_key from the catalog file and change the key in  the file $file from $unique_key to $message_key]
                    } else {
                        ns_log Notice [list lang::util::replace_temporary_tags_with_lookups - Will be adding  new key $unique_key to catalog file for package $package_key]
                    }

                    set messages_array($unique_key$new_text

                    # We are done
                    break
                }

                incr key_comp_counter
            }

            # Replace the message tag with a message key lookup in the file
            switch -regexp -- $file_ending {
                {^(adp|sql)$} {
                    regsub [message_tag_regexp]  $modified_file_contents  "#${package_key}.${unique_key}#"  modified_file_contents
                }
                {^tcl$} {
                    regsub [message_tag_regexp]  $modified_file_contents  "\[_ ${package_key}.${unique_key}\]"  modified_file_contents
                }
                {.*} {
                    error "Unknown ending $file_ending of file $file, aborting"
                }
            }
        }

        # Update the file with the replaced message keys
        set file_id [open "${full_file_path}" w]
        puts -nonewline $file_id $modified_file_contents
        close $file_id
    }

    if { $number_of_replacements > 0 } {
        # Register the messages in the database so that the new messages are immediately reflected
        # in the system
        foreach {message_key message_text} [array get messages_array] {
            lang::message::register en_US $package_key $message_key $message_text
        }

        # Generate a new catalog file
        lang::catalog::export -locales [list $locale] -package_key $package_key
    }

    return $number_of_replacements
XQL Not present:
PostgreSQL, Oracle
Generic XQL file:
packages/acs-lang/tcl/lang-util-procs.xql

[ hide source ] | [ make this the default ]
Show another procedure: