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