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.54 -r1.55 --- openacs-4/packages/acs-lang/tcl/lang-util-procs.tcl 9 May 2018 15:33:28 -0000 1.54 +++ openacs-4/packages/acs-lang/tcl/lang-util-procs.tcl 3 Sep 2024 15:37:33 -0000 1.55 @@ -19,7 +19,7 @@ namespace eval lang::util {} -ad_proc -public lang::util::lang_sort { +ad_proc -deprecated lang::util::lang_sort { field {locale ""} } { @@ -32,6 +32,14 @@ insert into lang_testsort values ('lzim'); + DEPRECATED: this api only supports Oracle. It also uses hardcoded + mapping between language and collation. It is unclear if an api is + needed for this, or if one can just have database-specific SQL in + xql files in order to achieve cross-db collation behavior. + + @see https://www.postgresql.org/docs/current/collation.html + @see https://docs.oracle.com/cd/B14117_01/server.101/b10759/functions089.htm + @author Jeff Davis (davis@xarg.net) @param field Name of Oracle column @@ -54,7 +62,7 @@ } } -ad_proc -private lang::util::get_hash_indices { multilingual_string } { +ad_proc -public lang::util::get_hash_indices { multilingual_string } { Returns a list of two element lists containing the start and end indices of a #message_key# match in the multilingual string. This proc is used by the localize proc. @@ -66,7 +74,7 @@ ad_proc lang::util::message_tag_regexp {} { The regexp expression used by proc get_temporary_tags_indices and elsewhere - to extract temporary message catalog tags (<#...#>) from ADP and Tcl files. + to extract temporary message catalog tags (<#...#>) from ADP and Tcl files. The first sub match of the expression is the whole tag, the second sub match is the message key, and the third sub match is the message text in en_US locale. @@ -88,7 +96,7 @@ Given the contents of an adp file return the indices of the start and end chars of embedded message keys on the syntax: - <#package_key.message_key Some en_US text#> + <#package_key.message_key Some en_US text#> @author Peter marklund (peter@collaboraid.biz) } { @@ -133,14 +141,14 @@ 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 autogenerated based on the text. + 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. - @param file_list A list of paths to adp or Tcl files to do replacements in. The + @param 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. @@ -256,7 +264,7 @@ 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 fom $unique_key to $message_key] + 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] @@ -368,10 +376,9 @@ @param locale Name of a locale, as language_COUNTRY using ISO 639 and ISO 3166 @return IANA MIME character set name } { - # DRB: cache this now that ad_conn tracks it - set key ::lang::util::charset_for_locale($locale) - if {[info exists $key]} {return [set $key]} - set $key [db_string -cache_key ad_lang_mime_charset_$locale charset_for_locale {}] + return [acs::per_thread_cache eval -key acs-lang.charset_for_locale($locale) { + db_string -cache_key ad_lang_mime_charset_$locale charset_for_locale {} + }] } ad_proc -private lang::util::default_locale_from_lang_not_cached { @@ -385,11 +392,24 @@ @see lang::util::default_locale_from_lang } { - # LARS: - # Note that this query does not use bind variables, because these cause the query to not - # match any rows in Oracle when the language key is less than 3 characters, - # because the column is a char(3), not a varchar2(3). - return [db_string default_locale_from_lang {} -default ""] + set locales [db_list locales_from_lang { + select locale + from ad_locales l + where language = :language + and enabled_p = 't' + and (default_p = 't' or not exists + (select 1 from ad_locales + where language = :language + and locale <> l.locale)) + }] + if {[llength $locales] > 1} { + ad_log error "multiple locales '$locales' defined for language '$language'. Define default locale longuage in /acs-lang/admin" + set locales [lindex $locales 0] + } + # + # return 0 or 1 locale + # + return $locales } ad_proc -public lang::util::default_locale_from_lang { @@ -415,14 +435,20 @@ @param language Name of a country, using ISO-3166 two letter code @return The nls_language name of the language. } { - return [db_string nls_language_from_language {}] + return [db_string nls_language_from_language { + select nls_language + from ad_locales + where lower(trim(language)) = lower(:language) + and enabled_p = 't' + fetch first 1 rows only + }] } ad_proc -private lang::util::remove_gt_lt { s } { - Removes < > and replaces them with < > + Removes < > and replaces them with &lt &gt; } { regsub -all "<" $s {\<} s regsub -all ">" $s {\>} s @@ -668,7 +694,7 @@ if {[info exists ::acs_translator_mode_p]} { return $::acs_translator_mode_p } - if { [ad_conn isconnected] } { + if { [ns_conn isconnected] } { # There is an HTTP connection - return the client property set ::acs_translator_mode_p [ad_get_client_property -default 0 acs-lang translator_mode_p] if {$::acs_translator_mode_p eq ""} { @@ -695,6 +721,7 @@ @see lang::util::translator_mode_p } { ad_set_client_property acs-lang translator_mode_p $translator_mode_p + set ::acs_translator_mode_p $translator_mode_p } ad_proc -private lang::util::record_message_lookup { @@ -765,13 +792,18 @@ } ad_proc -public lang::util::convert_to_i18n { - {-locale ""} + {-locale "en_US"} {-package_key "acs-translations"} {-message_key ""} {-prefix ""} {-text:required} + {-object_id ""} } { Internationalising of Attributes. This is done by storing the attribute with its acs-lang key + + @param object_id bind the newly created message key to this + acs_object id. Upon object's deletion, the + message key will be deleted as well. } { # If the package acs-translations is installed do the conversion @@ -791,10 +823,9 @@ } # Register the language keys - lang::message::register en_US $package_key $message_key $text - if {$locale ne ""} { - lang::message::register $locale $package_key $message_key $text - } + lang::message::register \ + -object_id $object_id \ + $locale $package_key $message_key $text return "#${package_key}.${message_key}#" } else { @@ -837,6 +868,14 @@ -message:required {-package_key "acs-translations"} } { + Generates the URL to edit a message key. + + @param message key with or without hashes, such as + \#acs-admin.Actions\# or acs-admin.Actions. + @param package_key must correspond to that in the message key. + + @return a local URL or the empty string when no URL can be + generated. } { if { [regsub "^${package_key}." [string trim $message "\#"] {} message_key] } { set edit_url [export_vars -base "[apm_package_url_from_key "acs-lang"]admin/edit-localized-message" { @@ -861,12 +900,12 @@ set iso6392_code "" set lang_len [string length $language] - if { $lang_len eq 2 } { + if { $lang_len == 2 } { # input is iso-639-1 language code set iso6392_code [db_string get_iso2_code_from_iso1 {} -default ""] - } elseif { $lang_len eq 3 } { + } elseif { $lang_len == 3 } { # input is iso-639-2 language code # we check in the table in case the language code is wrong @@ -905,12 +944,12 @@ set lang_label "" set lang_len [string length $language] - if { $lang_len eq 2 } { + if { $lang_len == 2 } { # input is iso-639-1 language code set lang_label [db_string get_label_from_iso1 {} -default ""] - } elseif { $lang_len eq 3 } { + } elseif { $lang_len == 3 } { # input is iso-639-2 language code # we check in the table in case the language code is wrong