###########################################################################
#
# gsprintf.pm --
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 1999 New Zealand Digital Library Project
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################


package gsprintf;


use util;


sub gsprintf
{
    my ($handle, $text_string, @text_arguments) = @_;

    # Return unless the required arguments were supplied
    return unless (defined($handle) && defined($text_string));

    # Look up all the strings in the dictionary
    $text_string =~ s/(\{[^\}]+\})/&new_lookup_string($1)/eg;

    # Resolve the string arguments using sprintf, then write out to the handle
    print $handle sprintf($text_string, @text_arguments);
}


sub new_lookup_string
{
    local ($stringkey) = @_;

    # Load the default resource bundle
    local %resourcebundle = &load_resource_bundle("");

    # Return the text matching the key (or just the key, if no match was found)
    return $resourcebundle{$stringkey} || $stringkey;
}


my $cachedlanguage = "<none>";
my %cachedresourcebundle = ();

sub load_resource_bundle
{
    local $language = shift(@_);

    # If the desired resource bundle is the one cached, return it
    if ($language eq $cachedlanguage) {
	return %cachedresourcebundle;
    }

    # Open the appropriate resource bundle
    local $resourcebundlehome = &util::filename_cat("$ENV{'GSDLHOME'}", "perllib");
    local $resourcebundlename = "strings_" . $language . ".rb";
    local $resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);

    # If the specific resource bundle cannot be opened, use the generic (English) one
    if (!open(RESOURCE_BUNDLE, "<$resourcebundlefile")) {
	$resourcebundlename = "strings.rb";
	$resourcebundlefile = &util::filename_cat($resourcebundlehome, $resourcebundlename);
	open(RESOURCE_BUNDLE, "<$resourcebundlefile")
	    or die "Error: Could not open generic resource bundle $resourcebundlefile.\n";
    }

    local @resourcebundlelines = <RESOURCE_BUNDLE>;
    close(RESOURCE_BUNDLE);

    # Load and cache this resource bundle
    $cachedlanguage = $language;
    %cachedresourcebundle = ();
    foreach $line (@resourcebundlelines) {
	# Remove any trailing whitespace
	$line =~ s/(\s*)$//;

	# Ignore comments and empty lines
	if ($line !~ /^\#/ && $line ne "") {
	    # Parse key (everything up to the first colon)
	    $line =~ /^([^:]+):(.+)$/;
	    local $linekey = "{" . $1 . "}";
	    local $linetext = $2;

	    # Map key to text
	    $cachedresourcebundle{$linekey} = $linetext;
	}
    }

    return %cachedresourcebundle;
}


#  sub gsprintf
#  {
#      my ($handle, $verbosity_threshold, $text_string, @text_arguments) = @_;

#      # Return unless the required arguments were supplied
#      return unless (defined($handle) && defined($verbosity_threshold) && defined($text_string));

#      # Return if the verbosity threshold isn't met
#      return if (defined($verbosity) && $verbosity < $verbosity_threshold);

#      # Look up all the strings in the dictionary
#      $text_string =~ s/(\{[^\}]+\})/&new_lookup_string($1)/eg;

#      # Resolve the string arguments using sprintf, then write out to the handle
#      print $handle sprintf($text_string, @text_arguments);
#  }


sub lookup_string
{
    local ($language, $stringkey) = @_;

    $language = "" if !defined($language);

    # Load the appropriate resource bundle
    local %resourcebundle = &load_resource_bundle($language);

    # Return the text matching the key (or just the key, if no match was found)
    return $resourcebundle{$stringkey} || $stringkey;
}


1;


