[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] | 25 | use strict;
|
---|
| 26 | no strict 'refs';
|
---|
[5613] | 27 |
|
---|
| 28 | package gsprintf;
|
---|
[7828] | 29 | require Exporter;
|
---|
| 30 | @gsprintf::ISA=qw(Exporter);
|
---|
[5613] | 31 |
|
---|
[6934] | 32 | use unicode;
|
---|
[5613] | 33 | use util;
|
---|
| 34 |
|
---|
[7828] | 35 | @gsprintf::EXPORT_OK = qw'gsprintf'; # functions we can export into namespace
|
---|
[5613] | 36 |
|
---|
[7828] | 37 |
|
---|
[6945] | 38 | # Language-specific resource bundle
|
---|
| 39 | my %specialresourcebundle = ();
|
---|
[6993] | 40 | our $specialoutputencoding; # our, so that it can be changed outside.
|
---|
[6925] | 41 |
|
---|
[6945] | 42 | # Default resource bundle
|
---|
| 43 | my %defaultresourcebundle;
|
---|
| 44 | my $defaultoutputencoding;
|
---|
[6925] | 45 |
|
---|
[6945] | 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 |
|
---|
[5613] | 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
|
---|
[6925] | 62 | $text_string =~ s/(\{[^\}]+\})/&lookup_string($1)/eg;
|
---|
[5613] | 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 |
|
---|
[6920] | 69 | sub lookup_string
|
---|
[5613] | 70 | {
|
---|
[6934] | 71 | my ($stringkey) = @_;
|
---|
[6945] | 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;
|
---|
[6925] | 83 | }
|
---|
[5613] | 84 |
|
---|
[6945] | 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);
|
---|
[6934] | 89 |
|
---|
[6945] | 90 | $utf8string = $englishresourcebundle{$stringkey};
|
---|
| 91 | $outputencoding = $englishoutputencoding;
|
---|
| 92 | }
|
---|
[6934] | 93 |
|
---|
[6945] | 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
|
---|
[6993] | 100 | return $utf8string if (!defined($outputencoding) || $outputstringsinUTF8
|
---|
| 101 | || $outputencoding eq "utf8");
|
---|
[6945] | 102 |
|
---|
[6934] | 103 | # If an 8-bit output encoding has been defined, encode the string appropriately
|
---|
[6945] | 104 | return &unicode::unicode2singlebyte(&unicode::utf82unicode($utf8string), $outputencoding);
|
---|
[5613] | 105 | }
|
---|
| 106 |
|
---|
| 107 |
|
---|
[6945] | 108 | sub load_language_specific_resource_bundle
|
---|
[5613] | 109 | {
|
---|
[6934] | 110 | my $language = shift(@_);
|
---|
[5613] | 111 |
|
---|
[6945] | 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}"};
|
---|
[5613] | 123 | }
|
---|
[6945] | 124 | else {
|
---|
| 125 | $specialoutputencoding = $specialresourcebundle{"{OutputEncoding.unix}"};
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
[5613] | 128 |
|
---|
[6945] | 129 |
|
---|
| 130 | sub load_default_resource_bundle
|
---|
| 131 | {
|
---|
| 132 | # Read the default resource bundle
|
---|
[6934] | 133 | my $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib");
|
---|
[6945] | 134 | my $resourcebundlename = "strings.rb";
|
---|
[6934] | 135 | my $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
|
---|
[5613] | 136 |
|
---|
[6945] | 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}"};
|
---|
[5613] | 143 | }
|
---|
[6945] | 144 | else {
|
---|
| 145 | $defaultoutputencoding = $defaultresourcebundle{"{OutputEncoding.unix}"};
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
[5613] | 148 |
|
---|
[6945] | 149 |
|
---|
| 150 | sub load_english_resource_bundle
|
---|
| 151 | {
|
---|
| 152 | # Ensure the English resource bundle hasn't already been loaded
|
---|
[9362] | 153 | if (%specialresourcebundle && $specialresourcebundle{"{Language.code}"} eq "en") {
|
---|
[7828] | 154 | %englishresourcebundle = %specialresourcebundle;
|
---|
[6945] | 155 | $englishoutputencoding = $specialoutputencoding;
|
---|
| 156 | }
|
---|
| 157 | if ($defaultresourcebundle{"{Language.code}"} eq "en") {
|
---|
[7828] | 158 | %englishresourcebundle = %defaultresourcebundle;
|
---|
[6945] | 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
|
---|
[6934] | 188 | my @resourcebundlelines = <RESOURCE_BUNDLE>;
|
---|
[5613] | 189 | close(RESOURCE_BUNDLE);
|
---|
| 190 |
|
---|
[6945] | 191 | # Parse the resource bundle
|
---|
| 192 | my %resourcebundle = ();
|
---|
[7828] | 193 | foreach my $line (@resourcebundlelines) {
|
---|
[5613] | 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 =~ /^([^:]+):(.+)$/;
|
---|
[6934] | 201 | my $linekey = "{" . $1 . "}";
|
---|
| 202 | my $linetext = $2;
|
---|
[5613] | 203 |
|
---|
| 204 | # Map key to text
|
---|
[6945] | 205 | $resourcebundle{$linekey} = $linetext;
|
---|
[5613] | 206 | }
|
---|
| 207 | }
|
---|
[6934] | 208 |
|
---|
[6945] | 209 | return %resourcebundle;
|
---|
[5613] | 210 | }
|
---|
| 211 |
|
---|
| 212 |
|
---|
[6945] | 213 | sub output_strings_in_UTF8
|
---|
[6934] | 214 | {
|
---|
[6945] | 215 | $outputstringsinUTF8 = 1;
|
---|
[6934] | 216 | }
|
---|
| 217 |
|
---|
| 218 |
|
---|
[5613] | 219 | 1;
|
---|