source: trunk/gsdl/perllib/gsprintf.pm@ 6940

Last change on this file since 6940 was 6934, checked in by mdewsnip, 20 years ago

Hopefully the last piece of the multilingual output functionality: uses the output encoding specified in the resource bundle. (Most times the output won't be wanted in UTF-8 -- rather an 8-bit encoding such as ISO 8859-1 suitable for terminals).

  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
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
27package gsprintf;
28
29
30use unicode;
31use util;
32
33
34my $loadedlanguage = "<none>";
35my %loadedresourcebundle = ();
36my $outputencoding;
37
38
39sub gsprintf
40{
41 my ($handle, $text_string, @text_arguments) = @_;
42
43 # Return unless the required arguments were supplied
44 return unless (defined($handle) && defined($text_string));
45
46 # Look up all the strings in the dictionary
47 $text_string =~ s/(\{[^\}]+\})/&lookup_string($1)/eg;
48
49 # Resolve the string arguments using sprintf, then write out to the handle
50 print $handle sprintf($text_string, @text_arguments);
51}
52
53
54sub lookup_string
55{
56 my ($stringkey) = @_;
57
58 # Load the default resource bundle if one isn't already loaded
59 if ($loadedlanguage eq "<none>") {
60 &load_resource_bundle("");
61 }
62
63 # Return just the key if there is no string matching the key
64 return $stringkey if (!defined($loadedresourcebundle{$stringkey}));
65
66 # Otherwise return the string matching the key
67 my $utf8_string = $loadedresourcebundle{$stringkey};
68 return $utf8_string if (!defined($outputencoding) || $outputencoding eq "UTF-8");
69
70 # If an 8-bit output encoding has been defined, encode the string appropriately
71 return &unicode::unicode2singlebyte(&unicode::utf82unicode($utf8_string), $outputencoding);
72}
73
74
75sub load_resource_bundle
76{
77 my $language = shift(@_);
78
79 # If the desired resource bundle is the one already loaded, no action is necessary
80 if ($language eq $loadedlanguage) {
81 return;
82 }
83
84 # Open the appropriate resource bundle
85 my $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib");
86 my $resourcebundlename = "strings_" . $language . ".rb";
87 my $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
88
89 # If the specific resource bundle cannot be opened, use the generic (English) one
90 if (!open(RESOURCE_BUNDLE, "<$resourcebundlefile")) {
91 $resourcebundlename = "strings.rb";
92 $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
93 open(RESOURCE_BUNDLE, "<$resourcebundlefile")
94 or die "Error: Could not open generic resource bundle $resourcebundlefile.\n";
95 }
96
97 my @resourcebundlelines = <RESOURCE_BUNDLE>;
98 close(RESOURCE_BUNDLE);
99
100 # Load this resource bundle
101 $loadedlanguage = $language;
102 %loadedresourcebundle = ();
103 foreach $line (@resourcebundlelines) {
104 # Remove any trailing whitespace
105 $line =~ s/(\s*)$//;
106
107 # Ignore comments and empty lines
108 if ($line !~ /^\#/ && $line ne "") {
109 # Parse key (everything up to the first colon)
110 $line =~ /^([^:]+):(.+)$/;
111 my $linekey = "{" . $1 . "}";
112 my $linetext = $2;
113
114 # Map key to text
115 $loadedresourcebundle{$linekey} = $linetext;
116 }
117 }
118
119 # Read the output encoding to use from the resource bundle
120 if ($ENV{'GSDLOS'} =~ /windows/) {
121 $outputencoding = $loadedresourcebundle{"{OutputEncoding.windows}"};
122 }
123 else {
124 $outputencoding = $loadedresourcebundle{"{OutputEncoding.unix}"};
125 }
126}
127
128
129sub get_output_encoding
130{
131 return $outputencoding;
132}
133
134
135sub set_output_encoding
136{
137 $outputencoding = shift(@_);
138}
139
140
1411;
Note: See TracBrowser for help on using the repository browser.