Changeset 6945 for trunk


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.

Location:
trunk/gsdl
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/bin/script/buildcol.pl

    r6926 r6945  
    238238    }
    239239
    240     # Load the appropriate resource bundle for the language specified
    241     # If $language is not defined, the default resource bundle will be loaded
    242     &gsprintf::load_resource_bundle($language);
     240    # If $language has been specified, load the appropriate resource bundle
     241    # (Otherwise, the default resource bundle will be loaded automatically)
     242    if ($language) {
     243    &gsprintf::load_language_specific_resource_bundle($language);
     244    }
    243245
    244246    if ($xml) {
  • trunk/gsdl/bin/script/classinfo.pl

    r6932 r6945  
    9999    }
    100100
    101     # Load the appropriate resource bundle for the language specified
    102     # If $language is not defined, the default resource bundle will be loaded
    103     &gsprintf::load_resource_bundle($language);
     101    # If $language has been specified, load the appropriate resource bundle
     102    # (Otherwise, the default resource bundle will be loaded automatically)
     103    if ($language) {
     104    &gsprintf::load_language_specific_resource_bundle($language);
     105    }
    104106
    105107    # Get classifier
  • trunk/gsdl/bin/script/exportcol.pl

    r6926 r6945  
    8888    }
    8989
    90     # Load the appropriate resource bundle for the language specified
    91     # If $language is not defined, the default resource bundle will be loaded
    92     &gsprintf::load_resource_bundle($language);
     90    # If $language has been specified, load the appropriate resource bundle
     91    # (Otherwise, the default resource bundle will be loaded automatically)
     92    if ($language) {
     93    &gsprintf::load_language_specific_resource_bundle($language);
     94    }
    9395
    9496    if ($xml) {
  • trunk/gsdl/bin/script/import.pl

    r6926 r6945  
    248248    }
    249249
    250     # Load the appropriate resource bundle for the language specified
    251     # If $language is not defined, the default resource bundle will be loaded
    252     &gsprintf::load_resource_bundle($language);
     250    # If $language has been specified, load the appropriate resource bundle
     251    # (Otherwise, the default resource bundle will be loaded automatically)
     252    if ($language) {
     253    &gsprintf::load_language_specific_resource_bundle($language);
     254    }
    253255
    254256    if ($xml) {
  • trunk/gsdl/bin/script/mkcol.pl

    r6926 r6945  
    211211    &parse_args (\@ARGV);
    212212
    213     # Load the appropriate resource bundle for the language specified
    214     # If $language is not defined, the default resource bundle will be loaded
    215     &gsprintf::load_resource_bundle($language);
     213    # If $language has been specified, load the appropriate resource bundle
     214    # (Otherwise, the default resource bundle will be loaded automatically)
     215    if ($language) {
     216    &gsprintf::load_language_specific_resource_bundle($language);
     217    }
    216218
    217219    if ($xml) {
  • trunk/gsdl/bin/script/pluginfo.pl

    r6932 r6945  
    8282    }
    8383
    84     # Load the appropriate resource bundle for the language specified
    85     # If $language is not defined, the default resource bundle will be loaded
    86     &gsprintf::load_resource_bundle($language);
     84    # If $language has been specified, load the appropriate resource bundle
     85    # (Otherwise, the default resource bundle will be loaded automatically)
     86    if ($language) {
     87    &gsprintf::load_language_specific_resource_bundle($language);
     88    }
    8789
    8890    my $plugin = shift (@ARGV);
  • trunk/gsdl/perllib/classify/BasClas.pm

    r6932 r6945  
    9494    local $self = shift(@_);
    9595
     96    # XML output is always in UTF-8
     97    &gsprintf::output_strings_in_UTF8;
     98
    9699    &PrintUsage::print_xml_header();
    97100    $self->print_xml();
  • 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
  • trunk/gsdl/perllib/plugins/BasPlug.pm

    r6932 r6945  
    156156    local $self = shift(@_);
    157157
     158    # XML output is always in UTF-8
     159    &gsprintf::output_strings_in_UTF8;
     160
    158161    &PrintUsage::print_xml_header();
    159162    $self->print_xml();
  • trunk/gsdl/perllib/printusage.pm

    r6934 r6945  
    4242
    4343    # XML output is always in UTF-8
    44     my $currentencoding = &gsprintf::get_output_encoding();
    45     &gsprintf::set_output_encoding("UTF-8");
     44    &gsprintf::output_strings_in_UTF8;
    4645
    4746    &print_xml_header();
     
    5655    &gsprintf(STDERR, "  </Arguments>\n");
    5756    &gsprintf(STDERR, "</Info>\n");
    58 
    59     # Put the output encoding back to what it was
    60     &gsprintf::set_output_encoding($currentencoding);
    6157}
    6258
Note: See TracChangeset for help on using the changeset viewer.