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 unicode;
|
---|
33 | use util;
|
---|
34 |
|
---|
35 | @gsprintf::EXPORT_OK = qw'gsprintf'; # functions we can export into namespace
|
---|
36 |
|
---|
37 |
|
---|
38 | # Language-specific resource bundle
|
---|
39 | my %specialresourcebundle = ();
|
---|
40 | our $specialoutputencoding; # our, so that it can be changed outside.
|
---|
41 |
|
---|
42 | # Default resource bundle
|
---|
43 | my %defaultresourcebundle;
|
---|
44 | my $defaultoutputencoding;
|
---|
45 |
|
---|
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 |
|
---|
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
|
---|
62 | $text_string =~ s/(\{[^\}]+\})/&lookup_string($1)/eg;
|
---|
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 |
|
---|
69 | sub lookup_string
|
---|
70 | {
|
---|
71 | my ($stringkey) = @_;
|
---|
72 | # Try the language-specific resource bundle first
|
---|
73 | my $utf8string = $specialresourcebundle{$stringkey};
|
---|
74 | my $outputencoding = $specialoutputencoding;
|
---|
75 |
|
---|
76 | # Try the default resource bundle next
|
---|
77 | if (!defined($utf8string)) {
|
---|
78 | # Load the default resource bundle if it is not already loaded
|
---|
79 | &load_default_resource_bundle() if (!%defaultresourcebundle);
|
---|
80 |
|
---|
81 | $utf8string = $defaultresourcebundle{$stringkey};
|
---|
82 | $outputencoding = $defaultoutputencoding;
|
---|
83 | }
|
---|
84 |
|
---|
85 | # Try the English resource bundle last
|
---|
86 | if (!defined($utf8string)) {
|
---|
87 | # Load the English resource bundle if it is not already loaded
|
---|
88 | &load_english_resource_bundle() if (!%englishresourcebundle);
|
---|
89 |
|
---|
90 | $utf8string = $englishresourcebundle{$stringkey};
|
---|
91 | $outputencoding = $englishoutputencoding;
|
---|
92 | }
|
---|
93 |
|
---|
94 | # No matching string was found, so just return the key
|
---|
95 | if (!defined($utf8string)) {
|
---|
96 | return $stringkey;
|
---|
97 | }
|
---|
98 |
|
---|
99 | # Return the string matching the key
|
---|
100 | return $utf8string if (!defined($outputencoding) || $outputstringsinUTF8
|
---|
101 | || $outputencoding eq "utf8");
|
---|
102 |
|
---|
103 | # If an 8-bit output encoding has been defined, encode the string appropriately
|
---|
104 | return &unicode::unicode2singlebyte(&unicode::utf82unicode($utf8string), $outputencoding);
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | sub load_language_specific_resource_bundle
|
---|
109 | {
|
---|
110 | my $language = shift(@_);
|
---|
111 |
|
---|
112 | # Read the specified resource bundle
|
---|
113 | my $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib");
|
---|
114 | my $resourcebundlename = "strings_" . $language . ".rb";
|
---|
115 | my $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
|
---|
116 |
|
---|
117 | %specialresourcebundle = &read_resource_bundle($resourcebundlefile);
|
---|
118 | return if (!%specialresourcebundle);
|
---|
119 |
|
---|
120 | # Read the output encoding to use from the resource bundle
|
---|
121 | if ($ENV{'GSDLOS'} =~ /windows/) {
|
---|
122 | $specialoutputencoding = $specialresourcebundle{"{OutputEncoding.windows}"};
|
---|
123 | }
|
---|
124 | else {
|
---|
125 | $specialoutputencoding = $specialresourcebundle{"{OutputEncoding.unix}"};
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | sub load_default_resource_bundle
|
---|
131 | {
|
---|
132 | # Read the default resource bundle
|
---|
133 | my $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib");
|
---|
134 | my $resourcebundlename = "strings.rb";
|
---|
135 | my $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
|
---|
136 |
|
---|
137 | %defaultresourcebundle = &read_resource_bundle($resourcebundlefile);
|
---|
138 | return if (!%defaultresourcebundle);
|
---|
139 |
|
---|
140 | # Read the output encoding to use from the resource bundle
|
---|
141 | if ($ENV{'GSDLOS'} =~ /windows/) {
|
---|
142 | $defaultoutputencoding = $defaultresourcebundle{"{OutputEncoding.windows}"};
|
---|
143 | }
|
---|
144 | else {
|
---|
145 | $defaultoutputencoding = $defaultresourcebundle{"{OutputEncoding.unix}"};
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | sub load_english_resource_bundle
|
---|
151 | {
|
---|
152 | # Ensure the English resource bundle hasn't already been loaded
|
---|
153 | if (%specialresourcebundle && $specialresourcebundle{"{Language.code}"} eq "en") {
|
---|
154 | %englishresourcebundle = %specialresourcebundle;
|
---|
155 | $englishoutputencoding = $specialoutputencoding;
|
---|
156 | }
|
---|
157 | if ($defaultresourcebundle{"{Language.code}"} eq "en") {
|
---|
158 | %englishresourcebundle = %defaultresourcebundle;
|
---|
159 | $englishoutputencoding = $defaultoutputencoding;
|
---|
160 | }
|
---|
161 |
|
---|
162 | # Read the English resource bundle
|
---|
163 | my $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib");
|
---|
164 | my $resourcebundlename = "strings_en.rb";
|
---|
165 | my $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
|
---|
166 |
|
---|
167 | %englishresourcebundle = &read_resource_bundle($resourcebundlefile);
|
---|
168 | return if (!%englishresourcebundle);
|
---|
169 |
|
---|
170 | # Read the output encoding to use from the resource bundle
|
---|
171 | if ($ENV{'GSDLOS'} =~ /windows/) {
|
---|
172 | $englishoutputencoding = $englishresourcebundle{"{OutputEncoding.windows}"};
|
---|
173 | }
|
---|
174 | else {
|
---|
175 | $englishoutputencoding = $englishresourcebundle{"{OutputEncoding.unix}"};
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | sub read_resource_bundle
|
---|
181 | {
|
---|
182 | my ($resourcebundlefilepath) = shift(@_);
|
---|
183 |
|
---|
184 | # Return an empty hash if the specified resource bundle could not be read
|
---|
185 | return () if (!open(RESOURCE_BUNDLE, "<$resourcebundlefilepath"));
|
---|
186 |
|
---|
187 | # Load this resource bundle
|
---|
188 | my @resourcebundlelines = <RESOURCE_BUNDLE>;
|
---|
189 | close(RESOURCE_BUNDLE);
|
---|
190 |
|
---|
191 | # Parse the resource bundle
|
---|
192 | my %resourcebundle = ();
|
---|
193 | foreach my $line (@resourcebundlelines) {
|
---|
194 | # Remove any trailing whitespace
|
---|
195 | $line =~ s/(\s*)$//;
|
---|
196 |
|
---|
197 | # Ignore comments and empty lines
|
---|
198 | if ($line !~ /^\#/ && $line ne "") {
|
---|
199 | # Parse key (everything up to the first colon)
|
---|
200 | $line =~ /^([^:]+):(.+)$/;
|
---|
201 | my $linekey = "{" . $1 . "}";
|
---|
202 | my $linetext = $2;
|
---|
203 |
|
---|
204 | # Map key to text
|
---|
205 | $resourcebundle{$linekey} = $linetext;
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | return %resourcebundle;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | sub output_strings_in_UTF8
|
---|
214 | {
|
---|
215 | $outputstringsinUTF8 = 1;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | 1;
|
---|