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