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 util;
|
---|
31 |
|
---|
32 |
|
---|
33 | sub gsprintf
|
---|
34 | {
|
---|
35 | my ($handle, $text_string, @text_arguments) = @_;
|
---|
36 |
|
---|
37 | # Return unless the required arguments were supplied
|
---|
38 | return unless (defined($handle) && defined($text_string));
|
---|
39 |
|
---|
40 | # Look up all the strings in the dictionary
|
---|
41 | $text_string =~ s/(\{[^\}]+\})/&new_lookup_string($1)/eg;
|
---|
42 |
|
---|
43 | # Resolve the string arguments using sprintf, then write out to the handle
|
---|
44 | print $handle sprintf($text_string, @text_arguments);
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | sub new_lookup_string
|
---|
49 | {
|
---|
50 | local ($stringkey) = @_;
|
---|
51 |
|
---|
52 | # Load the default resource bundle
|
---|
53 | local %resourcebundle = &load_resource_bundle("");
|
---|
54 |
|
---|
55 | # Return the text matching the key (or just the key, if no match was found)
|
---|
56 | return $resourcebundle{$stringkey} || $stringkey;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | my $cachedlanguage = "<none>";
|
---|
61 | my %cachedresourcebundle = ();
|
---|
62 |
|
---|
63 | sub load_resource_bundle
|
---|
64 | {
|
---|
65 | local $language = shift(@_);
|
---|
66 |
|
---|
67 | # If the desired resource bundle is the one cached, return it
|
---|
68 | if ($language eq $cachedlanguage) {
|
---|
69 | return %cachedresourcebundle;
|
---|
70 | }
|
---|
71 |
|
---|
72 | # Open the appropriate resource bundle
|
---|
73 | local $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib");
|
---|
74 | local $resourcebundlename = "strings_" . $language . ".rb";
|
---|
75 | local $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
|
---|
76 |
|
---|
77 | # If the specific resource bundle cannot be opened, use the generic (English) one
|
---|
78 | if (!open(RESOURCE_BUNDLE, "<$resourcebundlefile")) {
|
---|
79 | $resourcebundlename = "strings.rb";
|
---|
80 | $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
|
---|
81 | open(RESOURCE_BUNDLE, "<$resourcebundlefile")
|
---|
82 | or die "Error: Could not open generic resource bundle $resourcebundlefile.\n";
|
---|
83 | }
|
---|
84 |
|
---|
85 | local @resourcebundlelines = <RESOURCE_BUNDLE>;
|
---|
86 | close(RESOURCE_BUNDLE);
|
---|
87 |
|
---|
88 | # Load and cache this resource bundle
|
---|
89 | $cachedlanguage = $language;
|
---|
90 | %cachedresourcebundle = ();
|
---|
91 | foreach $line (@resourcebundlelines) {
|
---|
92 | # Remove any trailing whitespace
|
---|
93 | $line =~ s/(\s*)$//;
|
---|
94 |
|
---|
95 | # Ignore comments and empty lines
|
---|
96 | if ($line !~ /^\#/ && $line ne "") {
|
---|
97 | # Parse key (everything up to the first colon)
|
---|
98 | $line =~ /^([^:]+):(.+)$/;
|
---|
99 | local $linekey = "{" . $1 . "}";
|
---|
100 | local $linetext = $2;
|
---|
101 |
|
---|
102 | # Map key to text
|
---|
103 | $cachedresourcebundle{$linekey} = $linetext;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | return %cachedresourcebundle;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | # sub gsprintf
|
---|
112 | # {
|
---|
113 | # my ($handle, $verbosity_threshold, $text_string, @text_arguments) = @_;
|
---|
114 |
|
---|
115 | # # Return unless the required arguments were supplied
|
---|
116 | # return unless (defined($handle) && defined($verbosity_threshold) && defined($text_string));
|
---|
117 |
|
---|
118 | # # Return if the verbosity threshold isn't met
|
---|
119 | # return if (defined($verbosity) && $verbosity < $verbosity_threshold);
|
---|
120 |
|
---|
121 | # # Look up all the strings in the dictionary
|
---|
122 | # $text_string =~ s/(\{[^\}]+\})/&new_lookup_string($1)/eg;
|
---|
123 |
|
---|
124 | # # Resolve the string arguments using sprintf, then write out to the handle
|
---|
125 | # print $handle sprintf($text_string, @text_arguments);
|
---|
126 | # }
|
---|
127 |
|
---|
128 |
|
---|
129 | sub lookup_string
|
---|
130 | {
|
---|
131 | local ($language, $stringkey) = @_;
|
---|
132 |
|
---|
133 | $language = "" if !defined($language);
|
---|
134 |
|
---|
135 | # Load the appropriate resource bundle
|
---|
136 | local %resourcebundle = &load_resource_bundle($language);
|
---|
137 |
|
---|
138 | # Return the text matching the key (or just the key, if no match was found)
|
---|
139 | return $resourcebundle{$stringkey} || $stringkey;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | 1;
|
---|
144 |
|
---|
145 |
|
---|