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