Changeset 6945 for trunk/gsdl/perllib/gsprintf.pm
- Timestamp:
- 2004-03-07T15:18:16+13:00 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl/perllib/gsprintf.pm
r6934 r6945 32 32 33 33 34 my $loadedlanguage = "<none>"; 35 my %loadedresourcebundle = (); 36 my $outputencoding; 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; 37 48 38 49 … … 56 67 my ($stringkey) = @_; 57 68 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 # 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); 69 98 70 99 # 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 75 sub load_ resource_bundle100 return &unicode::unicode2singlebyte(&unicode::utf82unicode($utf8string), $outputencoding); 101 } 102 103 104 sub load_language_specific_resource_bundle 76 105 { 77 106 my $language = shift(@_); 78 107 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 108 # Read the specified resource bundle 85 109 my $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib"); 86 110 my $resourcebundlename = "strings_" . $language . ".rb"; 87 111 my $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename); 88 112 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 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 97 184 my @resourcebundlelines = <RESOURCE_BUNDLE>; 98 185 close(RESOURCE_BUNDLE); 99 186 100 # Load this resource bundle 101 $loadedlanguage = $language; 102 %loadedresourcebundle = (); 187 # Parse the resource bundle 188 my %resourcebundle = (); 103 189 foreach $line (@resourcebundlelines) { 104 190 # Remove any trailing whitespace … … 113 199 114 200 # Map key to text 115 $ loadedresourcebundle{$linekey} = $linetext;201 $resourcebundle{$linekey} = $linetext; 116 202 } 117 203 } 118 204 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 129 sub get_output_encoding 130 { 131 return $outputencoding; 132 } 133 134 135 sub set_output_encoding 136 { 137 $outputencoding = shift(@_); 205 return %resourcebundle; 206 } 207 208 209 sub output_strings_in_UTF8 210 { 211 $outputstringsinUTF8 = 1; 138 212 } 139 213
Note:
See TracChangeset
for help on using the changeset viewer.