Ignore:
Timestamp:
2004-03-07T15:18:16+13:00 (20 years ago)
Author:
mdewsnip
Message:

Updated the resource bundle handling code some more. Strings are first looked for in a language specific resource bundle (if specified). If not found there, the default resource bundle is checked. If still not found, the English resource bundle is checked. These resource bundles are loaded on an as-needed basis.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/perllib/gsprintf.pm

    r6934 r6945  
    3232
    3333
    34 my $loadedlanguage = "<none>";
    35 my %loadedresourcebundle = ();
    36 my $outputencoding;
     34# Language-specific resource bundle
     35my %specialresourcebundle = ();
     36my $specialoutputencoding;
     37
     38# Default resource bundle
     39my %defaultresourcebundle;
     40my $defaultoutputencoding;
     41
     42# English resource bundle
     43my %englishresourcebundle;
     44my $englishoutputencoding;
     45
     46# Ignore the OutputEncoding strings in the resource bundles and output all text in UTF-8
     47my $outputstringsinUTF8 = 0;
    3748
    3849
     
    5667    my ($stringkey) = @_;
    5768
    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);
    6998
    7099    # 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_bundle
     100    return &unicode::unicode2singlebyte(&unicode::utf82unicode($utf8string), $outputencoding);
     101}
     102
     103
     104sub load_language_specific_resource_bundle
    76105{
    77106    my $language = shift(@_);
    78107
    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
    85109    my $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib");
    86110    my $resourcebundlename = "strings_" . $language . ".rb";
    87111    my $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
    88112
    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
     126sub 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
     146sub 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
     176sub 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
    97184    my @resourcebundlelines = <RESOURCE_BUNDLE>;
    98185    close(RESOURCE_BUNDLE);
    99186
    100     # Load this resource bundle
    101     $loadedlanguage = $language;
    102     %loadedresourcebundle = ();
     187    # Parse the resource bundle
     188    my %resourcebundle = ();
    103189    foreach $line (@resourcebundlelines) {
    104190    # Remove any trailing whitespace
     
    113199
    114200        # Map key to text
    115         $loadedresourcebundle{$linekey} = $linetext;
     201        $resourcebundle{$linekey} = $linetext;
    116202    }
    117203    }
    118204
    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
     209sub output_strings_in_UTF8
     210{
     211    $outputstringsinUTF8 = 1;
    138212}
    139213
Note: See TracChangeset for help on using the changeset viewer.