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

Last change on this file since 31494 was 31419, checked in by kjdon, 7 years ago

new arg to lookup_string: pass in a 1 if you want the output to be perl internal (unicode-aware) strings, rather than utf8. We want this for eg when we get the 'this document has no text' message, its going into doc_obj which is using perl strings not utf8. Strangely, the few places where we are calling lookup_string for this purpose, it is already passing in an extra 1 arg. do do do do...

  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 KB
RevLine 
[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]25use strict;
26no strict 'refs';
[5613]27
28package gsprintf;
[7828]29require Exporter;
30@gsprintf::ISA=qw(Exporter);
[5613]31
[31419]32use Encode;
33
[6934]34use unicode;
[5613]35use util;
[27306]36use 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
42my %specialresourcebundle = ();
[6993]43our $specialoutputencoding; # our, so that it can be changed outside.
[6925]44
[6945]45# Default resource bundle
46my %defaultresourcebundle;
47my $defaultoutputencoding;
[6925]48
[6945]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;
[25499]55my $freetext_xml_mode = 0;
[6945]56
57
[25499]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
[5613]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));
[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]93sub 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]151sub 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
184sub 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
217sub 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]248sub 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]289sub 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]337sub set_print_freetext_for_xml
338{
339 $freetext_xml_mode = 1;
340}
341
342sub set_print_xml_tags
343{
344 $freetext_xml_mode = 0;
345}
346
[6945]347sub output_strings_in_UTF8
[6934]348{
[6945]349 $outputstringsinUTF8 = 1;
[6934]350}
351
352
[15007]353sub 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]3631;
Note: See TracBrowser for help on using the repository browser.