[5613] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # gsprintf.pm --
|
---|
| 4 | # A component of the Greenstone digital library software
|
---|
| 5 | # from the New Zealand Digital Library Project at the
|
---|
| 6 | # University of Waikato, New Zealand.
|
---|
| 7 | #
|
---|
| 8 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
| 9 | #
|
---|
| 10 | # This program is free software; you can redistribute it and/or modify
|
---|
| 11 | # it under the terms of the GNU General Public License as published by
|
---|
| 12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | # (at your option) any later version.
|
---|
| 14 | #
|
---|
| 15 | # This program is distributed in the hope that it will be useful,
|
---|
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | # GNU General Public License for more details.
|
---|
| 19 | #
|
---|
| 20 | # You should have received a copy of the GNU General Public License
|
---|
| 21 | # along with this program; if not, write to the Free Software
|
---|
| 22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | #
|
---|
| 24 | ###########################################################################
|
---|
[7828] | 25 | use strict;
|
---|
| 26 | no strict 'refs';
|
---|
[5613] | 27 |
|
---|
| 28 | package gsprintf;
|
---|
[7828] | 29 | require Exporter;
|
---|
| 30 | @gsprintf::ISA=qw(Exporter);
|
---|
[5613] | 31 |
|
---|
[6934] | 32 | use unicode;
|
---|
[5613] | 33 | use util;
|
---|
| 34 |
|
---|
[15007] | 35 | @gsprintf::EXPORT_OK = qw(gsprintf); # functions we can export into namespace
|
---|
[5613] | 36 |
|
---|
[7828] | 37 |
|
---|
[6945] | 38 | # Language-specific resource bundle
|
---|
| 39 | my %specialresourcebundle = ();
|
---|
[6993] | 40 | our $specialoutputencoding; # our, so that it can be changed outside.
|
---|
[6925] | 41 |
|
---|
[6945] | 42 | # Default resource bundle
|
---|
| 43 | my %defaultresourcebundle;
|
---|
| 44 | my $defaultoutputencoding;
|
---|
[6925] | 45 |
|
---|
[6945] | 46 | # English resource bundle
|
---|
| 47 | my %englishresourcebundle;
|
---|
| 48 | my $englishoutputencoding;
|
---|
| 49 |
|
---|
| 50 | # Ignore the OutputEncoding strings in the resource bundles and output all text in UTF-8
|
---|
| 51 | my $outputstringsinUTF8 = 0;
|
---|
[25499] | 52 | my $freetext_xml_mode = 0;
|
---|
[6945] | 53 |
|
---|
| 54 |
|
---|
[25499] | 55 | sub make_freetext_xml_safe
|
---|
| 56 | {
|
---|
| 57 | my ($text) = @_;
|
---|
| 58 |
|
---|
| 59 | $text =~ s/\&/&/g;
|
---|
| 60 | $text =~ s/\"/"/g;
|
---|
| 61 | $text =~ s/\</</g;
|
---|
| 62 | $text =~ s/\>/>/g;
|
---|
| 63 |
|
---|
| 64 | return $text;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 |
|
---|
[5613] | 68 | sub gsprintf
|
---|
| 69 | {
|
---|
| 70 | my ($handle, $text_string, @text_arguments) = @_;
|
---|
| 71 |
|
---|
| 72 | # Return unless the required arguments were supplied
|
---|
| 73 | return unless (defined($handle) && defined($text_string));
|
---|
[18981] | 74 |
|
---|
[5613] | 75 | # Look up all the strings in the dictionary
|
---|
[6925] | 76 | $text_string =~ s/(\{[^\}]+\})/&lookup_string($1)/eg;
|
---|
[5613] | 77 |
|
---|
| 78 | # Resolve the string arguments using sprintf, then write out to the handle
|
---|
[25499] | 79 | my $text_string_resolved = sprintf($text_string, @text_arguments);
|
---|
| 80 |
|
---|
| 81 | if ($freetext_xml_mode) {
|
---|
| 82 | $text_string_resolved = make_freetext_xml_safe($text_string_resolved);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | print $handle $text_string_resolved;
|
---|
[5613] | 86 | }
|
---|
| 87 |
|
---|
| 88 |
|
---|
[6920] | 89 | sub lookup_string
|
---|
[5613] | 90 | {
|
---|
[6934] | 91 | my ($stringkey) = @_;
|
---|
[10228] | 92 | return "" unless defined $stringkey;
|
---|
[6945] | 93 | # Try the language-specific resource bundle first
|
---|
| 94 | my $utf8string = $specialresourcebundle{$stringkey};
|
---|
| 95 | my $outputencoding = $specialoutputencoding;
|
---|
| 96 |
|
---|
| 97 | # Try the default resource bundle next
|
---|
| 98 | if (!defined($utf8string)) {
|
---|
[18981] | 99 | # Load the default resource bundle if it is not already loaded
|
---|
| 100 | &load_default_resource_bundle() if (!%defaultresourcebundle);
|
---|
| 101 |
|
---|
| 102 | $utf8string = $defaultresourcebundle{$stringkey};
|
---|
| 103 | $outputencoding = $defaultoutputencoding;
|
---|
[6925] | 104 | }
|
---|
[18981] | 105 |
|
---|
[6945] | 106 | # Try the English resource bundle last
|
---|
| 107 | if (!defined($utf8string)) {
|
---|
[18981] | 108 | # Load the English resource bundle if it is not already loaded
|
---|
| 109 | &load_english_resource_bundle() if (!%englishresourcebundle);
|
---|
| 110 |
|
---|
| 111 | $utf8string = $englishresourcebundle{$stringkey};
|
---|
| 112 | $outputencoding = $englishoutputencoding;
|
---|
[6945] | 113 | }
|
---|
[18981] | 114 |
|
---|
[6945] | 115 | # No matching string was found, so just return the key
|
---|
| 116 | if (!defined($utf8string)) {
|
---|
[18981] | 117 | return $stringkey;
|
---|
[6945] | 118 | }
|
---|
[18981] | 119 |
|
---|
[6945] | 120 | # Return the string matching the key
|
---|
[6993] | 121 | return $utf8string if (!defined($outputencoding) || $outputstringsinUTF8
|
---|
[18981] | 122 | || $outputencoding eq "utf8");
|
---|
| 123 |
|
---|
[6934] | 124 | # If an 8-bit output encoding has been defined, encode the string appropriately
|
---|
[9969] | 125 | my $encoded=unicode::unicode2singlebyte(&unicode::utf82unicode($utf8string), $outputencoding);
|
---|
| 126 |
|
---|
| 127 | # If we successfully encoded it, return it
|
---|
| 128 | if ($encoded) { return $encoded }
|
---|
[18981] | 129 |
|
---|
[9969] | 130 | # Otherwise, we can't convert to the requested encoding. return the utf8?
|
---|
| 131 | $specialoutputencoding='utf8';
|
---|
| 132 | return $utf8string;
|
---|
[5613] | 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
[6945] | 136 | sub load_language_specific_resource_bundle
|
---|
[5613] | 137 | {
|
---|
[6934] | 138 | my $language = shift(@_);
|
---|
[18981] | 139 |
|
---|
[6945] | 140 | # Read the specified resource bundle
|
---|
[11632] | 141 | my $resourcebundlename = "strings_" . $language . ".properties";
|
---|
[18981] | 142 |
|
---|
| 143 | %specialresourcebundle
|
---|
| 144 | = &read_resource_bundle_and_extensions($ENV{'GSDLHOME'},"perllib",$resourcebundlename);
|
---|
[6945] | 145 | return if (!%specialresourcebundle);
|
---|
[18981] | 146 |
|
---|
[6945] | 147 | # Read the output encoding to use from the resource bundle
|
---|
| 148 | if ($ENV{'GSDLOS'} =~ /windows/) {
|
---|
[18981] | 149 | $specialoutputencoding = $specialresourcebundle{"{OutputEncoding.windows}"};
|
---|
[5613] | 150 | }
|
---|
[6945] | 151 | else {
|
---|
[18981] | 152 | # see if there is an encoding set in the appropriate locale env var
|
---|
| 153 |
|
---|
| 154 | foreach my $envvar ('LC_ALL', 'LANG') {
|
---|
| 155 | if (!exists $ENV{$envvar}) { next }
|
---|
| 156 | my $locale=$ENV{$envvar};
|
---|
| 157 | if ($locale !~ /^\w+\.(.+)$/) { next }
|
---|
| 158 | my $enc=lc($1);
|
---|
| 159 | $enc =~ s/-/_/g;
|
---|
| 160 | if ($enc eq 'utf_8') { $enc='utf8' } # normalise to this name
|
---|
| 161 | $specialoutputencoding = $enc;
|
---|
| 162 | return;
|
---|
| 163 | }
|
---|
| 164 | $specialoutputencoding = $specialresourcebundle{"{OutputEncoding.unix}"};
|
---|
[6945] | 165 | }
|
---|
| 166 | }
|
---|
[5613] | 167 |
|
---|
[6945] | 168 |
|
---|
| 169 | sub load_default_resource_bundle
|
---|
| 170 | {
|
---|
| 171 | # Read the default resource bundle
|
---|
[11632] | 172 | my $resourcebundlename = "strings.properties";
|
---|
[5613] | 173 |
|
---|
[18981] | 174 | %defaultresourcebundle
|
---|
| 175 | = &read_resource_bundle_and_extensions($ENV{'GSDLHOME'},"perllib",$resourcebundlename);
|
---|
[10982] | 176 | if (!%defaultresourcebundle) {
|
---|
| 177 | # $! will still have the error value for the last failed syscall
|
---|
[25499] | 178 |
|
---|
| 179 | my $error_message = "$! $resourcebundlename\n";
|
---|
| 180 |
|
---|
| 181 | if ($freetext_xml_mode) {
|
---|
| 182 | $error_message = make_freetext_xml_safe($error_message);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | print STDERR $error_message;
|
---|
| 186 |
|
---|
[18981] | 187 | # set something so we don't bother trying to load it again
|
---|
| 188 | $defaultresourcebundle{0}=undef;
|
---|
[10982] | 189 | return;
|
---|
| 190 | }
|
---|
[18981] | 191 |
|
---|
[6945] | 192 | # Read the output encoding to use from the resource bundle
|
---|
| 193 | if ($ENV{'GSDLOS'} =~ /windows/) {
|
---|
[18981] | 194 | $defaultoutputencoding = $defaultresourcebundle{"{OutputEncoding.windows}"};
|
---|
[5613] | 195 | }
|
---|
[6945] | 196 | else {
|
---|
[18981] | 197 | $defaultoutputencoding = $defaultresourcebundle{"{OutputEncoding.unix}"};
|
---|
[6945] | 198 | }
|
---|
| 199 | }
|
---|
[5613] | 200 |
|
---|
[6945] | 201 |
|
---|
| 202 | sub load_english_resource_bundle
|
---|
| 203 | {
|
---|
| 204 | # Ensure the English resource bundle hasn't already been loaded
|
---|
[9362] | 205 | if (%specialresourcebundle && $specialresourcebundle{"{Language.code}"} eq "en") {
|
---|
[18981] | 206 | %englishresourcebundle = %specialresourcebundle;
|
---|
| 207 | $englishoutputencoding = $specialoutputencoding;
|
---|
[6945] | 208 | }
|
---|
[10982] | 209 |
|
---|
| 210 | if ($defaultresourcebundle{"{Language.code}"} &&
|
---|
[17533] | 211 | $defaultresourcebundle{"{Language.code}"} eq "en") {
|
---|
[18981] | 212 | %englishresourcebundle = %defaultresourcebundle;
|
---|
| 213 | $englishoutputencoding = $defaultoutputencoding;
|
---|
[6945] | 214 | }
|
---|
[18981] | 215 |
|
---|
[6945] | 216 | # Read the English resource bundle
|
---|
[11632] | 217 | my $resourcebundlename = "strings_en.properties";
|
---|
[6945] | 218 |
|
---|
[18981] | 219 | %englishresourcebundle
|
---|
| 220 | = &read_resource_bundle_and_extensions($ENV{'GSDLHOME'},"perllib",$resourcebundlename);
|
---|
[6945] | 221 | return if (!%englishresourcebundle);
|
---|
[18981] | 222 |
|
---|
[6945] | 223 | # Read the output encoding to use from the resource bundle
|
---|
| 224 | if ($ENV{'GSDLOS'} =~ /windows/) {
|
---|
[18981] | 225 | $englishoutputencoding = $englishresourcebundle{"{OutputEncoding.windows}"};
|
---|
[6945] | 226 | }
|
---|
| 227 | else {
|
---|
[18981] | 228 | $englishoutputencoding = $englishresourcebundle{"{OutputEncoding.unix}"};
|
---|
[6945] | 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 |
|
---|
[18981] | 233 | sub read_resource_bundle_and_extensions
|
---|
| 234 | {
|
---|
| 235 | my ($bundle_base,$primary_dir,$resourcename) = @_;
|
---|
| 236 |
|
---|
| 237 | my $primary_resourcebundlefile
|
---|
| 238 | = &util::filename_cat($bundle_base,$primary_dir,$resourcename);
|
---|
| 239 |
|
---|
| 240 | my $resourcebundle = read_resource_bundle($primary_resourcebundlefile);
|
---|
| 241 | return if (!defined $resourcebundle);
|
---|
| 242 |
|
---|
[19026] | 243 | if (defined $ENV{'GSDLEXTS'}) {
|
---|
| 244 | my @extensions = split(/:/,$ENV{'GSDLEXTS'});
|
---|
| 245 | foreach my $e (@extensions) {
|
---|
| 246 | my $ext_base
|
---|
| 247 | = &util::filename_cat($bundle_base,"ext",$e);
|
---|
| 248 |
|
---|
| 249 | my $ext_resourcebundlefile
|
---|
| 250 | = &util::filename_cat($ext_base,$primary_dir,$resourcename);
|
---|
| 251 |
|
---|
| 252 | # can ignore return value (will be same reference to $resourcebundle)
|
---|
| 253 | read_resource_bundle($ext_resourcebundlefile,$resourcebundle);
|
---|
| 254 | }
|
---|
[18981] | 255 | }
|
---|
[21289] | 256 | if (defined $ENV{'GSDL3EXTS'}) {
|
---|
| 257 | my @extensions = split(/:/,$ENV{'GSDL3EXTS'});
|
---|
| 258 | foreach my $e (@extensions) {
|
---|
| 259 | my $ext_base
|
---|
| 260 | = &util::filename_cat($ENV{'GSDL3SRCHOME'},"ext",$e);
|
---|
| 261 |
|
---|
| 262 | my $ext_resourcebundlefile
|
---|
| 263 | = &util::filename_cat($ext_base,$primary_dir,$resourcename);
|
---|
| 264 |
|
---|
| 265 | # can ignore return value (will be same reference to $resourcebundle)
|
---|
| 266 | read_resource_bundle($ext_resourcebundlefile,$resourcebundle);
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
[18981] | 269 |
|
---|
| 270 | return %$resourcebundle;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 |
|
---|
[6945] | 274 | sub read_resource_bundle
|
---|
| 275 | {
|
---|
[18981] | 276 | my ($resourcebundlefilepath,$resourcebundle) = @_;
|
---|
[6945] | 277 |
|
---|
[18981] | 278 | if (!open(RESOURCE_BUNDLE, "<$resourcebundlefilepath")) {
|
---|
| 279 | # When called for the first time (primary resource), $resourcebundle
|
---|
| 280 | # is not defined (=undef). If the file does not exist, then we return
|
---|
| 281 | # this 'undef' to signal it was not found
|
---|
| 282 | # For an extension resource bundle, if it does not exist this
|
---|
| 283 | # is not so serious (in fact quite likely) => return what we
|
---|
| 284 | # have built up so far
|
---|
| 285 |
|
---|
| 286 | return $resourcebundle;
|
---|
| 287 | }
|
---|
[6945] | 288 |
|
---|
[18981] | 289 | if (!defined $resourcebundle) {
|
---|
| 290 | # resource files exists, so exect some content to be stored
|
---|
| 291 | $resourcebundle = {};
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[6945] | 294 | # Load this resource bundle
|
---|
[6934] | 295 | my @resourcebundlelines = <RESOURCE_BUNDLE>;
|
---|
[5613] | 296 | close(RESOURCE_BUNDLE);
|
---|
| 297 |
|
---|
[6945] | 298 | # Parse the resource bundle
|
---|
[18981] | 299 |
|
---|
[7828] | 300 | foreach my $line (@resourcebundlelines) {
|
---|
[17533] | 301 | # Remove any trailing whitespace
|
---|
| 302 | $line =~ s/(\s*)$//;
|
---|
[5613] | 303 |
|
---|
[17533] | 304 | # Ignore comments and empty lines
|
---|
| 305 | if ($line !~ /^\#/ && $line ne "") {
|
---|
| 306 | # Parse key (everything up to the first colon)
|
---|
| 307 | if ($line =~ m/^([^:]+):(.+)$/) {
|
---|
| 308 | my $linekey = "{" . $1 . "}";
|
---|
| 309 | my $linetext = $2;
|
---|
| 310 | $linetext =~ s/(\s*)\#\s+Updated\s+(\d?\d-\D\D\D-\d\d\d\d).*$//i;
|
---|
[5613] | 311 |
|
---|
[17533] | 312 | # Map key to text
|
---|
[18981] | 313 | $resourcebundle->{$linekey} = $linetext;
|
---|
[17533] | 314 | }
|
---|
| 315 | }
|
---|
[5613] | 316 | }
|
---|
[6934] | 317 |
|
---|
[18981] | 318 | return $resourcebundle;
|
---|
[5613] | 319 | }
|
---|
| 320 |
|
---|
| 321 |
|
---|
[25499] | 322 | sub set_print_freetext_for_xml
|
---|
| 323 | {
|
---|
| 324 | $freetext_xml_mode = 1;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | sub set_print_xml_tags
|
---|
| 328 | {
|
---|
| 329 | $freetext_xml_mode = 0;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
[6945] | 332 | sub output_strings_in_UTF8
|
---|
[6934] | 333 | {
|
---|
[6945] | 334 | $outputstringsinUTF8 = 1;
|
---|
[6934] | 335 | }
|
---|
| 336 |
|
---|
| 337 |
|
---|
[15007] | 338 | sub debug_unicode_string
|
---|
| 339 | {
|
---|
| 340 | join("",
|
---|
[17533] | 341 | map { $_ > 255 ? # if wide character...
|
---|
| 342 | sprintf("\\x{%04X}", $_) : # \x{...}
|
---|
| 343 | chr($_)
|
---|
| 344 | } unpack("U*", $_[0])); # unpack Unicode characters
|
---|
[15007] | 345 | }
|
---|
| 346 |
|
---|
| 347 |
|
---|
[5613] | 348 | 1;
|
---|