Lirama::Loc2
[perl/loc/.git] / Lirama / Loc2.pm
diff --git a/Lirama/Loc2.pm b/Lirama/Loc2.pm
new file mode 100644 (file)
index 0000000..0ea0410
--- /dev/null
@@ -0,0 +1,119 @@
+package Lirama::Loc2;
+
+use strict;
+use warnings;
+use utf8; #todo: does it even matter?
+
+our $VERSION = '2.00';
+
+require Exporter;
+our @ISA       = qw(Exporter);
+our @EXPORT    = qw(langpref);
+our @EXPORT_OK = qw(loc);
+
+our @langpref;# = qw(en eo nl sv el hu de fr ru kr);
+
+sub langpref {
+       @langpref = @_ if @_;  # set order of languages (prefered language first)
+       return wantarray ? @langpref : $langpref[0];
+}
+
+sub loc($) {
+       my $this = shift;
+       # get one value from a hash of multiple options
+       # if it isn't, we're done already:
+       ref $_[0] eq "HASH" or return $_[0];
+       # localize to most preferred language
+       defined $_[0]{$_} and return $_[0]{$_} for @langpref;
+}
+
+sub TIEHASH {
+       return bless $_[1], $_[0];  # bless the l10n hash
+}
+
+sub FETCH {
+       my $this = shift;
+                       # custom expand: get preferred language from given hash
+               #       return $this->loc($_[0]) if ref $_[0] eq "HASH";  # deprecated in favor of loc()
+       # array ref used for passing arguments
+       @_ = @{$_[0]} if ref $_[0] eq "ARRAY";
+       # get localized string by identifier
+       local $_ = shift;
+                       #todo: shouldn't occur - find out where this is done, then fix and remove this check
+               #       defined $_ or return '';
+       $_ = $this->loc($this->{$_}) if exists $this->{$_};
+#      $_ = ref $this->{$_} eq "HASH" ? $this->loc($this->{$_}) : $this->{$_} if exists $this->{$_};
+       # static output if no arguments given
+       return $_ unless @_;  # unnecessary but faster for common case
+       # adaptive string (code)
+       $_ = $_->(@_) if ref $_ eq "CODE";
+       # dynamic output
+       return sprintf $_, @_;
+}
+
+# Same as found in Tie::StdHash
+
+sub EXISTS   { exists $_[0]->{$_[1]} }
+sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
+sub NEXTKEY  { each %{$_[0]} }
+
+1;
+
+__END__
+
+
+=head1 NAME
+
+Lirama::Loc - Localize strings
+
+=head1 SYNOPSIS
+
+       use Lirama::Loc;
+
+       langpref(qw/nl en eo/);  # prefer I<nl> (dutch) texts
+
+       tie my %loc, "Lirama::Loc", {
+               test => {
+                       en => "this is a test",
+                       eo => "cxi tio estas testo",
+               },
+       };
+
+       print $loc{test};  # "this is a test", since dutch is unavailable
+
+=head1 DESCRIPTION
+
+Returns a text in the most preferred language available.
+Mainly intended for translation of different strings on a website.
+
+=over 4
+
+=item C<langpref>
+
+Shared so we only have to set one var to change all translations;
+may yet be a very bad idea (does it work correctly in modperl?)
+
+=item C<tie>
+
+=item C<loc>
+
+=item C<exists>
+
+True if identifier is localized; non-existing strings still return
+themselves, so in standard meaning everything would exist.
+
+=back
+
+=head1 SEE ALSO
+
+L<Lirama::Loc::Auto|Lirama::Loc::Auto>
+
+L<Locale::Maketext|Locale::Maketext>
+
+=head1 AUTHOR
+
+Mischa POSLAWSKY <shiar@shiar.org>
+
+Copyright 2005 Mischa POSLAWSKY. All rights reserved.
+
+=cut