Index: openacs-4/packages/acs-lang/tcl/lang-catalog-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-lang/tcl/lang-catalog-procs.tcl,v diff -u -r1.52 -r1.53 --- openacs-4/packages/acs-lang/tcl/lang-catalog-procs.tcl 29 Dec 2017 11:13:40 -0000 1.52 +++ openacs-4/packages/acs-lang/tcl/lang-catalog-procs.tcl 29 Dec 2017 11:17:34 -0000 1.53 @@ -5,16 +5,16 @@ Routines for importing/exporting messages from/to XML message catalog files. Every OpenACS package has one message catalog file for each locale (language and region) that its UI supports. Importing of messages means reading the messages - from XML catalog files and storing them in the database. Exporting of messages refers to the opposite process. + from XML catalog files and storing them in the database. Exporting of messages refers to the opposite process. The key procedures in this library are:

@@ -38,15 +38,15 @@ ad_proc -private lang::catalog::default_charset_if_unsupported { charset } { Will return the system default charset and issue a warning in the log file if the given charset is not supported by tcl. Otherwise - the given charset is simply returned. + the given charset is simply returned. @author Jeff Davis @author Peter Marklund (peter@collaboraid.biz) } { set ns_charsets [concat [ns_charsets] [encoding names]] # Do case insensitive matching - if {[lsearch -regexp $ns_charsets "(?i)^${charset}\$"] < 0} { - #set default_charset [encoding system] + if {[lsearch -regexp $ns_charsets "(?i)^${charset}\$"] < 0} { + #set default_charset [encoding system] # LARS: Default to utf-8 set default_charset utf-8 ns_log Warning "charset $charset not supported by tcl, assuming $default_charset" @@ -82,7 +82,7 @@ } { return [db_list_of_lists get_messages {}] } - + ad_proc -private lang::catalog::package_catalog_dir { package_key } { Return the catalog directory of the given package. @@ -93,7 +93,7 @@ } ad_proc -private lang::catalog::is_upgrade_backup_file { file_path } { - Given a file path return 1 if the path represents a + Given a file path return 1 if the path represents a file with messages backed up from message catalog upgrade. @author Peter Marklund @@ -151,14 +151,14 @@ } on error {errorMsg} { set has_file_in_locale_p 0 } - + return $has_file_in_locale_p } -ad_proc -private lang::catalog::get_catalog_file_path { +ad_proc -private lang::catalog::get_catalog_file_path { {-backup_from_version ""} {-backup_to_version ""} - {-package_key:required} + {-package_key:required} {-locale:required} {-charset ""} } { @@ -194,7 +194,7 @@ set filename "${message_backup_prefix}${package_key}.${locale}.${file_charset}.xml" set file_path "[package_catalog_dir $package_key]/$filename" - + return $file_path } @@ -246,9 +246,9 @@ {-package_key:required} {-locale:required} } { - For a certain package, and locale, return the messages in + For a certain package, and locale, return the messages in the database the last time catalog files and db were in sync. - This is the message that we use as merge base during message catalog + This is the message that we use as merge base during message catalog upgrades. @return An array list with message keys as keys and messages as @@ -267,7 +267,7 @@ } ad_proc -private lang::catalog::uninitialized_packages {} { - Return a list of keys for installed and enabled packages + Return a list of keys for installed and enabled packages that do not have any message keys associated with them. This would suggest that either the package is not internationalized, or we have not yet imported the message keys for the package. @@ -283,18 +283,18 @@ # ################## -ad_proc -private lang::catalog::export_to_file { +ad_proc -private lang::catalog::export_to_file { {-descriptions_list ""} - file_path - messages_list + file_path + messages_list } { - Export messages for a certain locale and package from the database + 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. - + @param 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. @@ -306,7 +306,7 @@ } { # 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" @@ -331,7 +331,7 @@ 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 @@ -367,7 +367,7 @@ # Close the root node and close the file puts $catalog_file_id "" - close $catalog_file_id + close $catalog_file_id ns_log Notice "Wrote $message_count messages to file $file_path with encoding $file_encoding" } @@ -392,35 +392,35 @@ } foreach package_key $package_key_list { - # We do not want to export acs-translations. This usually is a very bad idea as the object_ids are different from site to site. - if {$package_key ne "acs-translations" } { - # Loop over all locales that the package has messages in - # and write a catalog file for each such locale - db_foreach get_locales_for_package {} { - # If we are only exporting certain locales and this is not one of them - continue - if { [llength $locales] > 0 && $locale ni $locales } { - continue - } - - # Get messages and descriptions for the locale - set messages_list [list] - set descriptions_list [list] - foreach message_tuple [all_messages_for_package_and_locale $package_key $locale] { + # We do not want to export acs-translations. This usually is a very bad idea as the object_ids are different from site to site. + if {$package_key ne "acs-translations" } { + # Loop over all locales that the package has messages in + # and write a catalog file for each such locale + db_foreach get_locales_for_package {} { + # If we are only exporting certain locales and this is not one of them - continue + if { [llength $locales] > 0 && $locale ni $locales } { + continue + } + + # Get messages and descriptions for the locale + set messages_list [list] + set descriptions_list [list] + foreach message_tuple [all_messages_for_package_and_locale $package_key $locale] { lassign $message_tuple message_key message description - lappend messages_list $message_key $message - lappend descriptions_list $message_key $description - } + lappend messages_list $message_key $message + lappend descriptions_list $message_key $description + } - set catalog_file_path [get_catalog_file_path \ - -package_key $package_key \ - -locale $locale] - - export_to_file -descriptions_list $descriptions_list $catalog_file_path $messages_list - - # Messages exported to file are in sync with file - db_dml update_sync_time {} - } - } + set catalog_file_path [get_catalog_file_path \ + -package_key $package_key \ + -locale $locale] + + export_to_file -descriptions_list $descriptions_list $catalog_file_path $messages_list + + # Messages exported to file are in sync with file + db_dml update_sync_time {} + } + } } } @@ -433,9 +433,9 @@ ad_proc -private lang::catalog::read_file { catalog_filename } { Returns the contents of the given catalog file as a string reading the file with the charset given in the filename. - + @param catalog_file_name The full path of the catalog file to read. - The basename of the file should be on the form + The basename of the file should be on the form package_key.locale.charset.ending where ending is either cat or xml (i.e. dotlrn.en_US.iso-8859-1.xml or dotlrn.en_US.iso-8859-1.cat). The cat ending @@ -444,18 +444,18 @@ @author Jeff Davis @author Peter Marklund (peter@collaboraid.biz) } { - if {![regexp {/([^/]*)\.([^/]*)\.(?:xml|cat)$} $catalog_filename match base msg_encoding]} { - ns_log Warning "Charset info missing in filename assuming $catalog_filename is iso-8859-1" + if {![regexp {/([^/]*)\.([^/]*)\.(?:xml|cat)$} $catalog_filename match base msg_encoding]} { + ns_log Warning "Charset info missing in filename assuming $catalog_filename is iso-8859-1" set msg_encoding iso-8859-1 } - + set msg_encoding [default_charset_if_unsupported $msg_encoding] ns_log Notice "reading $catalog_filename in $msg_encoding" set in [open $catalog_filename] fconfigure $in -encoding [ns_encodingforcharset $msg_encoding] - set catalog_file_contents [read $in] - close $in + set catalog_file_contents [read $in] + close $in return $catalog_file_contents } @@ -474,7 +474,7 @@ @author Peter Marklund (peter@collaboraid.biz) @author Simon Carstensen (simon@collaboraid.biz) -} { +} { # Check arguments if { $catalog_file_contents eq "" } { @@ -535,11 +535,11 @@ return [array get msg_catalog_array] } -ad_proc -private lang::catalog::import_from_file { +ad_proc -private lang::catalog::import_from_file { file_path } {

- Import messages for a certain locale and package from a given XML + Import messages for a certain locale and package from a given XML catalog file to the database. This procedure invokes lang::catalog::parse to read the catalog file and lang::message::register to register the messages with the system (updates database and cache). @@ -555,14 +555,14 @@ @param file_path The absolute path of the XML file to import messages from. The path must be on valid format, see apm_is_catalog_file - @return An array list containing the number of messages processed, number of messages added, + @return An array list containing the number of messages processed, number of messages added, number of messages updated, and the number of messages deleted by the import. The keys of the array list are processed, added, updated, and deleted. @see lang::catalog::parse @see lang::message::register @see lang::catalog::import_messages - + @author Peter Marklund } { # Check arguments @@ -625,7 +625,7 @@ Import a given set of messages from a catalog file to the database for a certain package and locale. If we already have messages in the db for the given package and locale then a merge - between the database messages and the file messages will be performed. + between the database messages and the file messages will be performed.

@@ -649,13 +649,13 @@

What follows below is a description of the logic of the proc in terms of its input, the cases considered, and the logical actions taken for each case. -

+

- There are three sets of keys, file, db, and base keys. For each key in + There are three sets of keys, file, db, and base keys. For each key in the union of these keys there are three messages that can exist: the file message, the db message, and the base message. The base message serves as the base for the merge. We will distinguish all the different permutations - of each of the three messages existing or not, and all permutations of the messages being different from eachother. + of each of the three messages existing or not, and all permutations of the messages being different from eachother. We don't distinguish how two messages are different, only whether they are different or not. In total that gives us 14 cases (permutations) to consider.

@@ -696,15 +696,15 @@ 14. All different. upgrade_action=update, conflict_p=t - @param file_messages_list An array list with message keys as keys and - the message of those keys as values, + @param file_messages_list An array list with message keys as keys and + the message of those keys as values, i.e. (key, value, key, value, ...) @param package_key The package_key for the messages. @param locale The locale of the messages. - @return An array list containing the number of messages processed, number of messages added, + @return An array list containing the number of messages processed, number of messages added, number of messages updated, number of messages deleted by the import, and a list of errors produced. The keys of the array list are processed, added, updated, and deleted, and errors. @@ -737,13 +737,13 @@ # Remember each time we've processed a key, so we don't process it twice array set message_key_processed_p [list] - # Loop over the union of import and db keys. + # Loop over the union of import and db keys. foreach message_key [lsort [concat [array names db_messages] [array names file_messages] [array names base_messages]]] { if { [info exists message_key_processed_p($message_key)] } { continue } set message_key_processed_p($message_key) 1 - + ########################################### # # Figure out how db and file messages have changed with regards to the base message @@ -794,8 +794,8 @@ ########################################### # - # Based on the change in file and db messages, - # and based on whether file and db messages differ, decide + # Based on the change in file and db messages, + # and based on whether file and db messages differ, decide # which upgrade actions to take # ########################################### @@ -831,7 +831,7 @@ } add { switch $file_change { - none {} + none {} add { if { $db_messages($message_key) ne $file_messages($message_key) } { # case 8 @@ -841,7 +841,7 @@ set conflict_p "t" } } - } + } } update { switch $file_change { @@ -862,7 +862,7 @@ set upgrade_status "deleted" set conflict_p "t" } - } + } } delete { switch $file_change { @@ -877,7 +877,7 @@ delete { # case 1 set import_case 1 - # deletion in both db and file + # deletion in both db and file # no status change, no conflict # sync time should be updated below } @@ -889,12 +889,12 @@ # # Execute upgrade actions # - ########################################### + ########################################### - # For certain messages we need to move the sync point so that we have a current base for the next upgrade. + # For certain messages we need to move the sync point so that we have a current base for the next upgrade. if { $db_change eq "none" || $file_change ne "none" } { - # If there is no db change then any change in the file will be reflected in - # db (file takes precedence) and file and db are identical. + # If there is no db change then any change in the file will be reflected in + # db (file takes precedence) and file and db are identical. # Also, regardless of what's happened in db, if # there has been a change in the file then that change will take effect in # the db and file and db are again identical (in sync). @@ -932,7 +932,7 @@ set edit_array(deleted_p) [string equal $upgrade_status "deleted"] set edit_array(conflict_p) $conflict_p } - + ns_log Debug "lang::catalog::import_messages - invoking lang::message::edit with import_case=\"$import_case\" -update_sync=$update_sync_p $message_key [array get edit_array]" ad_try { lang::message::edit \ @@ -953,7 +953,7 @@ if { ! $error_p } { incr message_count($upgrade_status) } - } + } incr message_count(processed) } ;# End of message key loop @@ -977,7 +977,7 @@ @param initialize Only load messages from packages that have never before had any message imported @param cache Provide this switch if you want the proc to cache all the imported messages - @return An array list containing the number of messages processed, number of messages added, + @return An array list containing the number of messages processed, number of messages added, number of messages updated, number of messages deleted by the import, and a list of errors produced. The keys of the array list are processed, added, updated, and deleted, and errors. @@ -1073,7 +1073,7 @@ # Get all catalog files for enabled locales set catalog_files [list] - foreach locale $locales_list { + foreach locale $locales_list { # If we are only processing certain locales and this is not one of them - continue if { [llength $locales] > 0 && $locale ni $locales } { @@ -1134,10 +1134,10 @@ # ################## -ad_proc -private lang::catalog::translate {} { +ad_proc -private lang::catalog::translate {} { Translates all untranslated strings in a message catalog from English into Spanish, French and German - using Babelfish. NOTE: this proc is unmaintained. + using Babelfish. NOTE: this proc is unmaintained. Quick way to get a multilingual site up and running if you can live with the quality of the translations.

@@ -1148,7 +1148,7 @@ } { set default_locale [parameter::get -package_id [apm_package_id_from_key acs-lang] -parameter SiteWideLocale] - db_foreach get_untranslated_messages {} { + db_foreach get_untranslated_messages {} { foreach lang [list es_ES fr_FR de_DE] { ad_try { set translated_message [lang_babel_translate $message en_$lang] @@ -1158,7 +1158,7 @@ lang::message::register $lang $package_key $message_key $translated_message } } - } + } } # Local variables: