[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;
|
---|
| 52 |
|
---|
| 53 |
|
---|
[5613] | 54 | sub gsprintf
|
---|
| 55 | {
|
---|
| 56 | my ($handle, $text_string, @text_arguments) = @_;
|
---|
| 57 |
|
---|
| 58 | # Return unless the required arguments were supplied
|
---|
| 59 | return unless (defined($handle) && defined($text_string));
|
---|
| 60 |
|
---|
| 61 | # Look up all the strings in the dictionary
|
---|
[6925] | 62 | $text_string =~ s/(\{[^\}]+\})/&lookup_string($1)/eg;
|
---|
[5613] | 63 |
|
---|
| 64 | # Resolve the string arguments using sprintf, then write out to the handle
|
---|
| 65 | print $handle sprintf($text_string, @text_arguments);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
[6920] | 69 | sub lookup_string
|
---|
[5613] | 70 | {
|
---|
[6934] | 71 | my ($stringkey) = @_;
|
---|
[10228] | 72 | return "" unless defined $stringkey;
|
---|
[6945] | 73 | # Try the language-specific resource bundle first
|
---|
| 74 | my $utf8string = $specialresourcebundle{$stringkey};
|
---|
| 75 | my $outputencoding = $specialoutputencoding;
|
---|
| 76 |
|
---|
| 77 | # Try the default resource bundle next
|
---|
| 78 | if (!defined($utf8string)) {
|
---|
[17533] | 79 | # Load the default resource bundle if it is not already loaded
|
---|
| 80 | &load_default_resource_bundle() if (!%defaultresourcebundle);
|
---|
[6945] | 81 |
|
---|
[17533] | 82 | $utf8string = $defaultresourcebundle{$stringkey};
|
---|
| 83 | $outputencoding = $defaultoutputencoding;
|
---|
[6925] | 84 | }
|
---|
[5613] | 85 |
|
---|
[6945] | 86 | # Try the English resource bundle last
|
---|
| 87 | if (!defined($utf8string)) {
|
---|
[17533] | 88 | # Load the English resource bundle if it is not already loaded
|
---|
| 89 | &load_english_resource_bundle() if (!%englishresourcebundle);
|
---|
[6934] | 90 |
|
---|
[17533] | 91 | $utf8string = $englishresourcebundle{$stringkey};
|
---|
| 92 | $outputencoding = $englishoutputencoding;
|
---|
[6945] | 93 | }
|
---|
[6934] | 94 |
|
---|
[6945] | 95 | # No matching string was found, so just return the key
|
---|
| 96 | if (!defined($utf8string)) {
|
---|
[17533] | 97 | return $stringkey;
|
---|
[6945] | 98 | }
|
---|
| 99 |
|
---|
| 100 | # Return the string matching the key
|
---|
[6993] | 101 | return $utf8string if (!defined($outputencoding) || $outputstringsinUTF8
|
---|
[17533] | 102 | || $outputencoding eq "utf8");
|
---|
[6945] | 103 |
|
---|
[6934] | 104 | # If an 8-bit output encoding has been defined, encode the string appropriately
|
---|
[9969] | 105 | my $encoded=unicode::unicode2singlebyte(&unicode::utf82unicode($utf8string), $outputencoding);
|
---|
| 106 |
|
---|
| 107 | # If we successfully encoded it, return it
|
---|
| 108 | if ($encoded) { return $encoded }
|
---|
| 109 |
|
---|
| 110 | # Otherwise, we can't convert to the requested encoding. return the utf8?
|
---|
| 111 | $specialoutputencoding='utf8';
|
---|
| 112 | return $utf8string;
|
---|
[5613] | 113 | }
|
---|
| 114 |
|
---|
| 115 |
|
---|
[6945] | 116 | sub load_language_specific_resource_bundle
|
---|
[5613] | 117 | {
|
---|
[6934] | 118 | my $language = shift(@_);
|
---|
[5613] | 119 |
|
---|
[6945] | 120 | # Read the specified resource bundle
|
---|
| 121 | my $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib");
|
---|
[11632] | 122 | my $resourcebundlename = "strings_" . $language . ".properties";
|
---|
[6945] | 123 | my $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
|
---|
| 124 |
|
---|
| 125 | %specialresourcebundle = &read_resource_bundle($resourcebundlefile);
|
---|
| 126 | return if (!%specialresourcebundle);
|
---|
| 127 |
|
---|
| 128 | # Read the output encoding to use from the resource bundle
|
---|
| 129 | if ($ENV{'GSDLOS'} =~ /windows/) {
|
---|
[17533] | 130 | $specialoutputencoding = $specialresourcebundle{"{OutputEncoding.windows}"};
|
---|
[5613] | 131 | }
|
---|
[6945] | 132 | else {
|
---|
[17533] | 133 | # see if there is an encoding set in the appropriate locale env var
|
---|
[9969] | 134 |
|
---|
[17533] | 135 | foreach my $envvar ('LC_ALL', 'LANG') {
|
---|
| 136 | if (!exists $ENV{$envvar}) { next }
|
---|
| 137 | my $locale=$ENV{$envvar};
|
---|
| 138 | if ($locale !~ /^\w+\.(.+)$/) { next }
|
---|
| 139 | my $enc=lc($1);
|
---|
| 140 | $enc =~ s/-/_/g;
|
---|
| 141 | if ($enc eq 'utf_8') { $enc='utf8' } # normalise to this name
|
---|
| 142 | $specialoutputencoding = $enc;
|
---|
| 143 | return;
|
---|
[6945] | 144 | }
|
---|
[17533] | 145 | $specialoutputencoding = $specialresourcebundle{"{OutputEncoding.unix}"};
|
---|
| 146 | }
|
---|
[6945] | 147 | }
|
---|
[5613] | 148 |
|
---|
[6945] | 149 |
|
---|
| 150 | sub load_default_resource_bundle
|
---|
| 151 | {
|
---|
| 152 | # Read the default resource bundle
|
---|
[6934] | 153 | my $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib");
|
---|
[11632] | 154 | my $resourcebundlename = "strings.properties";
|
---|
[6934] | 155 | my $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
|
---|
[5613] | 156 |
|
---|
[6945] | 157 | %defaultresourcebundle = &read_resource_bundle($resourcebundlefile);
|
---|
[10982] | 158 | if (!%defaultresourcebundle) {
|
---|
| 159 | # $! will still have the error value for the last failed syscall
|
---|
| 160 | print STDERR "$! $resourcebundlefile\n";
|
---|
[17533] | 161 | # set something so we don't bother trying to load it again
|
---|
| 162 | $defaultresourcebundle{0}=undef;
|
---|
[10982] | 163 | return;
|
---|
| 164 | }
|
---|
[6945] | 165 |
|
---|
| 166 | # Read the output encoding to use from the resource bundle
|
---|
| 167 | if ($ENV{'GSDLOS'} =~ /windows/) {
|
---|
[17533] | 168 | $defaultoutputencoding = $defaultresourcebundle{"{OutputEncoding.windows}"};
|
---|
[5613] | 169 | }
|
---|
[6945] | 170 | else {
|
---|
[17533] | 171 | $defaultoutputencoding = $defaultresourcebundle{"{OutputEncoding.unix}"};
|
---|
[6945] | 172 | }
|
---|
| 173 | }
|
---|
[5613] | 174 |
|
---|
[6945] | 175 |
|
---|
| 176 | sub load_english_resource_bundle
|
---|
| 177 | {
|
---|
| 178 | # Ensure the English resource bundle hasn't already been loaded
|
---|
[9362] | 179 | if (%specialresourcebundle && $specialresourcebundle{"{Language.code}"} eq "en") {
|
---|
[17533] | 180 | %englishresourcebundle = %specialresourcebundle;
|
---|
| 181 | $englishoutputencoding = $specialoutputencoding;
|
---|
[6945] | 182 | }
|
---|
[10982] | 183 |
|
---|
| 184 | if ($defaultresourcebundle{"{Language.code}"} &&
|
---|
[17533] | 185 | $defaultresourcebundle{"{Language.code}"} eq "en") {
|
---|
| 186 | %englishresourcebundle = %defaultresourcebundle;
|
---|
| 187 | $englishoutputencoding = $defaultoutputencoding;
|
---|
[6945] | 188 | }
|
---|
| 189 |
|
---|
| 190 | # Read the English resource bundle
|
---|
| 191 | my $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib");
|
---|
[11632] | 192 | my $resourcebundlename = "strings_en.properties";
|
---|
[6945] | 193 | my $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
|
---|
| 194 |
|
---|
| 195 | %englishresourcebundle = &read_resource_bundle($resourcebundlefile);
|
---|
| 196 | return if (!%englishresourcebundle);
|
---|
| 197 |
|
---|
| 198 | # Read the output encoding to use from the resource bundle
|
---|
| 199 | if ($ENV{'GSDLOS'} =~ /windows/) {
|
---|
[17533] | 200 | $englishoutputencoding = $englishresourcebundle{"{OutputEncoding.windows}"};
|
---|
[6945] | 201 | }
|
---|
| 202 | else {
|
---|
[17533] | 203 | $englishoutputencoding = $englishresourcebundle{"{OutputEncoding.unix}"};
|
---|
[6945] | 204 | }
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 |
|
---|
| 208 | sub read_resource_bundle
|
---|
| 209 | {
|
---|
| 210 | my ($resourcebundlefilepath) = shift(@_);
|
---|
| 211 |
|
---|
| 212 | # Return an empty hash if the specified resource bundle could not be read
|
---|
| 213 | return () if (!open(RESOURCE_BUNDLE, "<$resourcebundlefilepath"));
|
---|
| 214 |
|
---|
| 215 | # Load this resource bundle
|
---|
[6934] | 216 | my @resourcebundlelines = <RESOURCE_BUNDLE>;
|
---|
[5613] | 217 | close(RESOURCE_BUNDLE);
|
---|
| 218 |
|
---|
[6945] | 219 | # Parse the resource bundle
|
---|
| 220 | my %resourcebundle = ();
|
---|
[7828] | 221 | foreach my $line (@resourcebundlelines) {
|
---|
[17533] | 222 | # Remove any trailing whitespace
|
---|
| 223 | $line =~ s/(\s*)$//;
|
---|
[5613] | 224 |
|
---|
[17533] | 225 | # Ignore comments and empty lines
|
---|
| 226 | if ($line !~ /^\#/ && $line ne "") {
|
---|
| 227 | # Parse key (everything up to the first colon)
|
---|
| 228 | if ($line =~ m/^([^:]+):(.+)$/) {
|
---|
| 229 | my $linekey = "{" . $1 . "}";
|
---|
| 230 | my $linetext = $2;
|
---|
| 231 | $linetext =~ s/(\s*)\#\s+Updated\s+(\d?\d-\D\D\D-\d\d\d\d).*$//i;
|
---|
[5613] | 232 |
|
---|
[17533] | 233 | # Map key to text
|
---|
| 234 | $resourcebundle{$linekey} = $linetext;
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
[5613] | 237 | }
|
---|
[6934] | 238 |
|
---|
[6945] | 239 | return %resourcebundle;
|
---|
[5613] | 240 | }
|
---|
| 241 |
|
---|
| 242 |
|
---|
[6945] | 243 | sub output_strings_in_UTF8
|
---|
[6934] | 244 | {
|
---|
[6945] | 245 | $outputstringsinUTF8 = 1;
|
---|
[6934] | 246 | }
|
---|
| 247 |
|
---|
| 248 |
|
---|
[15007] | 249 | sub debug_unicode_string
|
---|
| 250 | {
|
---|
| 251 | join("",
|
---|
[17533] | 252 | map { $_ > 255 ? # if wide character...
|
---|
| 253 | sprintf("\\x{%04X}", $_) : # \x{...}
|
---|
| 254 | chr($_)
|
---|
| 255 | } unpack("U*", $_[0])); # unpack Unicode characters
|
---|
[15007] | 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
[5613] | 259 | 1;
|
---|