source: main/trunk/greenstone2/perllib/gsprintf.pm@ 32159

Last change on this file since 32159 was 31898, checked in by ak19, 7 years ago

With Kathy's commit 31896 doing away with the need for the recently introduced gsprintf_multiline() by moving the regex replacements of backslash-n (and backslash-t) with newline and tab into gsprint::lookup_string, can now shift WebDownload calls to gsprintf_multine to use regular gsprintf. Putting back the original gsprintf method as gsprintf_multiline is no longer needed.

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