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