lang::catalog::export_to_file (private)

 lang::catalog::export_to_file [ -descriptions_list descriptions_list ] \
    file_path messages_list

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

Export messages for a certain locale and package from the database to a given XML catalog file. If the catalog file already exists it will be backed up to a file with the same name but the extension .orig added to it. If there is an old backup file no new backup is done.

Switches:
-descriptions_list
(optional)
Parameters:
file_path - The path of the catalog file to write messages to. The filename needs to be parseable by apm_parse_catalog_path. The file and the catalog directory will be created if they don't exist.
messages_list - A list with message keys on even indices followed by corresponding messages on odd indices.
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::catalog::export_to_file lang::catalog::export_to_file test_util__replace_temporary_tags_with_lookups->lang::catalog::export_to_file apm_parse_catalog_path apm_parse_catalog_path (public) lang::catalog::export_to_file->apm_parse_catalog_path lang::catalog::default_charset_if_unsupported lang::catalog::default_charset_if_unsupported (private) lang::catalog::export_to_file->lang::catalog::default_charset_if_unsupported lang::catalog::get_catalog_files lang::catalog::get_catalog_files (private) lang::catalog::export_to_file->lang::catalog::get_catalog_files lang::catalog::package_catalog_dir lang::catalog::package_catalog_dir (public) lang::catalog::export_to_file->lang::catalog::package_catalog_dir lang::catalog::export lang::catalog::export (public) lang::catalog::export->lang::catalog::export_to_file lang::test::execute_upgrade lang::test::execute_upgrade (private) lang::test::execute_upgrade->lang::catalog::export_to_file

Testcases:
util__replace_temporary_tags_with_lookups
Source code:
    # Extract package_key, locale, and charset from the file path
    array set filename_info [apm_parse_catalog_path $file_path]

    # Check that the filename is parsable. We are not requiring any particular directory though
    if { [array size filename_info] == 0 } {
        error "Could not parse package_key, locale, and charset from filename of file $file_path"
    }

    # Put the messages and descriptions in an array so it's easier to access them
    array set messages_array $messages_list
    array set descriptions_array $descriptions_list

    # Sort the keys so that it's easier to manually read and edit the catalog files
    set message_key_list [lsort -dictionary [array names messages_array]]

    # Create the catalog directory if it doesn't exist
    set catalog_dir [package_catalog_dir $filename_info(package_key)]
    if { ![file isdirectory $catalog_dir] } {
        ns_log Notice "Creating new catalog directory $catalog_dir"
        file mkdir $catalog_dir
    }

    # Create a backup file first if a file already exists
    set backup_path "${file_path}.orig"
    if { [file exists $file_path] } {
        ns_log Notice "Creating backup catalog file $backup_path"
        file copy -force -- $file_path $backup_path
    }

    # Since the output charset, and thus the filename, may have changed since
    # last time that we wrote the catalog file we remove old files with the same locale
    foreach old_catalog_file [get_catalog_files $filename_info(package_key)] {
        # Parse locale from filename
        array set old_filename_info [apm_parse_catalog_path $old_catalog_file]

        if {$old_filename_info(locale) eq $filename_info(locale)} {
            file delete -- $old_catalog_file
        }
    }

    # Open the catalog file for writing, truncate if it exists
    set file_encoding [ns_encodingforcharset [default_charset_if_unsupported $filename_info(charset)]]

    set catalog_file_id [open $file_path w]
    fconfigure $catalog_file_id -encoding $file_encoding

    # Open the root node of the document
    puts $catalog_file_id "<?xml version=\"1.0\" encoding=\"$filename_info(charset)\"?>
<message_catalog package_key=\"$filename_info(package_key)\" locale=\"$filename_info(locale)\" charset=\"$filename_info(charset)\">
"

   # Loop over and write the messages to the file
   set message_count "0"
   foreach message_key $message_key_list {
       puts $catalog_file_id "  <msg key=\"[ns_quotehtml $message_key]\">[ns_quotehtml $messages_array($message_key)]</msg>"
       if { [info exists descriptions_array($message_key)] && $descriptions_array($message_key) ne ""
            && $filename_info(locale) eq "en_US"
        } {
           puts $catalog_file_id "  <description key=\"[ns_quotehtml $message_key]\">[ns_quotehtml $descriptions_array($message_key)]</description>\n"
       }
       incr message_count
   }

   # Close the root node and close the file
   puts $catalog_file_id "</message_catalog>"
   close $catalog_file_id

   ns_log Notice "Wrote $message_count messages to file $file_path with encoding $file_encoding"
Generic XQL file:
packages/acs-lang/tcl/lang-catalog-procs.xql

PostgreSQL XQL file:
packages/acs-lang/tcl/lang-catalog-procs-postgresql.xql

Oracle XQL file:
packages/acs-lang/tcl/lang-catalog-procs-oracle.xql

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