Changeset 18981


Ignore:
Timestamp:
2009-04-15T16:32:53+12:00 (15 years ago)
Author:
davidb
Message:

Code now looks for resource bundles in extension folders as well

File:
1 edited

Legend:

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

    r17533 r18981  
    5858    # Return unless the required arguments were supplied
    5959    return unless (defined($handle) && defined($text_string));
    60 
     60   
    6161    # Look up all the strings in the dictionary
    6262    $text_string =~ s/(\{[^\}]+\})/&lookup_string($1)/eg;
     
    7777    # Try the default resource bundle next
    7878    if (!defined($utf8string)) {
    79     # Load the default resource bundle if it is not already loaded
    80     &load_default_resource_bundle() if (!%defaultresourcebundle);
    81 
    82     $utf8string = $defaultresourcebundle{$stringkey};
    83     $outputencoding = $defaultoutputencoding;
    84     }
    85 
     79    # Load the default resource bundle if it is not already loaded
     80    &load_default_resource_bundle() if (!%defaultresourcebundle);
     81   
     82    $utf8string = $defaultresourcebundle{$stringkey};
     83    $outputencoding = $defaultoutputencoding;
     84    }
     85   
    8686    # Try the English resource bundle last
    8787    if (!defined($utf8string)) {
    88     # Load the English resource bundle if it is not already loaded
    89     &load_english_resource_bundle() if (!%englishresourcebundle);
    90 
    91     $utf8string = $englishresourcebundle{$stringkey};
    92     $outputencoding = $englishoutputencoding;
    93     }
    94 
     88    # Load the English resource bundle if it is not already loaded
     89    &load_english_resource_bundle() if (!%englishresourcebundle);
     90   
     91    $utf8string = $englishresourcebundle{$stringkey};
     92    $outputencoding = $englishoutputencoding;
     93    }
     94   
    9595    # No matching string was found, so just return the key
    9696    if (!defined($utf8string)) {
    97     return $stringkey;
    98     }
    99 
     97    return $stringkey;
     98    }
     99   
    100100    # Return the string matching the key
    101101    return $utf8string if (!defined($outputencoding) || $outputstringsinUTF8
    102                || $outputencoding eq "utf8");
    103 
     102               || $outputencoding eq "utf8");
     103   
    104104    # If an 8-bit output encoding has been defined, encode the string appropriately
    105105    my $encoded=unicode::unicode2singlebyte(&unicode::utf82unicode($utf8string), $outputencoding);
     
    107107    # If we successfully encoded it, return it
    108108    if ($encoded) { return $encoded }
    109 
     109   
    110110    # Otherwise, we can't convert to the requested encoding. return the utf8?
    111111    $specialoutputencoding='utf8';
     
    117117{
    118118    my $language = shift(@_);
    119 
     119   
    120120    # Read the specified resource bundle
    121     my $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib");
    122121    my $resourcebundlename = "strings_" . $language . ".properties";
    123     my $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
    124 
    125     %specialresourcebundle = &read_resource_bundle($resourcebundlefile);
     122   
     123    %specialresourcebundle
     124    = &read_resource_bundle_and_extensions($ENV{'GSDLHOME'},"perllib",$resourcebundlename);
    126125    return if (!%specialresourcebundle);
    127 
     126   
    128127    # Read the output encoding to use from the resource bundle
    129128    if ($ENV{'GSDLOS'} =~ /windows/) {
    130     $specialoutputencoding = $specialresourcebundle{"{OutputEncoding.windows}"};
     129    $specialoutputencoding = $specialresourcebundle{"{OutputEncoding.windows}"};
    131130    }
    132131    else {
    133     # see if there is an encoding set in the appropriate locale env var
    134 
    135     foreach my $envvar ('LC_ALL', 'LANG') {
    136         if (!exists $ENV{$envvar}) { next }
    137         my $locale=$ENV{$envvar};
    138         if ($locale !~ /^\w+\.(.+)$/) { next }
    139         my $enc=lc($1);
    140         $enc =~ s/-/_/g;
    141         if ($enc eq 'utf_8') { $enc='utf8' } # normalise to this name
    142         $specialoutputencoding = $enc;
    143         return;
    144     }
    145     $specialoutputencoding = $specialresourcebundle{"{OutputEncoding.unix}"};
     132    # see if there is an encoding set in the appropriate locale env var
     133   
     134    foreach my $envvar ('LC_ALL', 'LANG') {
     135        if (!exists $ENV{$envvar}) { next }
     136        my $locale=$ENV{$envvar};
     137        if ($locale !~ /^\w+\.(.+)$/) { next }
     138        my $enc=lc($1);
     139        $enc =~ s/-/_/g;
     140        if ($enc eq 'utf_8') { $enc='utf8' } # normalise to this name
     141        $specialoutputencoding = $enc;
     142        return;
     143    }
     144    $specialoutputencoding = $specialresourcebundle{"{OutputEncoding.unix}"};
    146145    }
    147146}
     
    151150{
    152151    # Read the default resource bundle
    153     my $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib");
    154152    my $resourcebundlename = "strings.properties";
    155     my $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
    156 
    157     %defaultresourcebundle = &read_resource_bundle($resourcebundlefile);
     153
     154    %defaultresourcebundle
     155    = &read_resource_bundle_and_extensions($ENV{'GSDLHOME'},"perllib",$resourcebundlename);
    158156    if (!%defaultresourcebundle) {
    159157        # $! will still have the error value for the last failed syscall
    160         print STDERR "$! $resourcebundlefile\n";
    161     # set something so we don't bother trying to load it again
    162     $defaultresourcebundle{0}=undef;
     158        print STDERR "$! $resourcebundlename\n";
     159    # set something so we don't bother trying to load it again
     160    $defaultresourcebundle{0}=undef;
    163161        return;
    164162    }
    165 
     163   
    166164    # Read the output encoding to use from the resource bundle
    167165    if ($ENV{'GSDLOS'} =~ /windows/) {
    168     $defaultoutputencoding = $defaultresourcebundle{"{OutputEncoding.windows}"};
     166    $defaultoutputencoding = $defaultresourcebundle{"{OutputEncoding.windows}"};
    169167    }
    170168    else {
    171     $defaultoutputencoding = $defaultresourcebundle{"{OutputEncoding.unix}"};
     169    $defaultoutputencoding = $defaultresourcebundle{"{OutputEncoding.unix}"};
    172170    }
    173171}
     
    178176    # Ensure the English resource bundle hasn't already been loaded
    179177    if (%specialresourcebundle && $specialresourcebundle{"{Language.code}"} eq "en") {
    180     %englishresourcebundle = %specialresourcebundle;
    181     $englishoutputencoding = $specialoutputencoding;
     178    %englishresourcebundle = %specialresourcebundle;
     179    $englishoutputencoding = $specialoutputencoding;
    182180    }
    183181   
    184182    if ($defaultresourcebundle{"{Language.code}"} &&
    185183        $defaultresourcebundle{"{Language.code}"} eq "en") {
    186     %englishresourcebundle = %defaultresourcebundle;
    187     $englishoutputencoding = $defaultoutputencoding;
    188     }
    189 
     184    %englishresourcebundle = %defaultresourcebundle;
     185    $englishoutputencoding = $defaultoutputencoding;
     186    }
     187   
    190188    # Read the English resource bundle
    191     my $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib");
    192189    my $resourcebundlename = "strings_en.properties";
    193     my $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
    194 
    195     %englishresourcebundle = &read_resource_bundle($resourcebundlefile);
     190
     191    %englishresourcebundle
     192    = &read_resource_bundle_and_extensions($ENV{'GSDLHOME'},"perllib",$resourcebundlename);
    196193    return if (!%englishresourcebundle);
    197 
     194   
    198195    # Read the output encoding to use from the resource bundle
    199196    if ($ENV{'GSDLOS'} =~ /windows/) {
    200     $englishoutputencoding = $englishresourcebundle{"{OutputEncoding.windows}"};
     197    $englishoutputencoding = $englishresourcebundle{"{OutputEncoding.windows}"};
    201198    }
    202199    else {
    203     $englishoutputencoding = $englishresourcebundle{"{OutputEncoding.unix}"};
    204     }
     200    $englishoutputencoding = $englishresourcebundle{"{OutputEncoding.unix}"};
     201    }
     202}
     203
     204
     205sub read_resource_bundle_and_extensions
     206{
     207    my ($bundle_base,$primary_dir,$resourcename) = @_;
     208
     209    my $primary_resourcebundlefile
     210    = &util::filename_cat($bundle_base,$primary_dir,$resourcename);
     211
     212    my $resourcebundle = read_resource_bundle($primary_resourcebundlefile);
     213    return if (!defined $resourcebundle);
     214
     215    my @extensions = split(/:/,$ENV{'GSDLEXTS'});
     216    foreach my $e (@extensions) {   
     217    my $ext_base
     218        = &util::filename_cat($bundle_base,"ext",$e);
     219
     220    my $ext_resourcebundlefile
     221        = &util::filename_cat($ext_base,$primary_dir,$resourcename);
     222
     223    # can ignore return value (will be same reference to $resourcebundle)
     224    read_resource_bundle($ext_resourcebundlefile,$resourcebundle);
     225    }
     226   
     227    return %$resourcebundle;
    205228}
    206229
     
    208231sub read_resource_bundle
    209232{
    210     my ($resourcebundlefilepath) = shift(@_);
    211 
    212     # Return an empty hash if the specified resource bundle could not be read
    213     return () if (!open(RESOURCE_BUNDLE, "<$resourcebundlefilepath"));
    214 
     233    my ($resourcebundlefilepath,$resourcebundle) = @_;
     234
     235    if (!open(RESOURCE_BUNDLE, "<$resourcebundlefilepath")) {
     236    # When called for the first time (primary resource), $resourcebundle
     237    # is not defined (=undef). If the file does not exist, then we return
     238    # this 'undef' to signal it was not found
     239    # For an extension resource bundle, if it does not exist this
     240    # is not so serious (in fact quite likely) => return what we
     241    # have built up so far
     242   
     243    return $resourcebundle;
     244    }
     245
     246    if (!defined $resourcebundle) {
     247    # resource files exists, so exect some content to be stored
     248    $resourcebundle = {};
     249    }
     250   
    215251    # Load this resource bundle
    216252    my @resourcebundlelines = <RESOURCE_BUNDLE>;
     
    218254
    219255    # Parse the resource bundle
    220     my %resourcebundle = ();
     256
    221257    foreach my $line (@resourcebundlelines) {
    222258        # Remove any trailing whitespace
     
    232268
    233269                # Map key to text
    234                 $resourcebundle{$linekey} = $linetext;
     270                $resourcebundle->{$linekey} = $linetext;
    235271            }
    236272        }
    237273    }
    238274
    239     return %resourcebundle;
     275    return $resourcebundle;
    240276}
    241277
Note: See TracChangeset for help on using the changeset viewer.