10 # get one value from a hash of multiple options
11 # if it isn't, we're done already:
12 ref $_[0] eq "HASH" or return $_[0];
13 # localize to most preferred language
14 defined $_[0]{$_} and return $_[0]{$_} for @{$this->{-langpref}};
19 return bless $_[1], $_[0]; # bless the l10n hash
25 # get setting (denoted by leading dash)
26 return wantarray ? @{$this->{$_}} : $this->{$_}->[0]
29 if $_ eq "-path" or $_ eq "-seperator";
30 # array ref used for passing arguments
31 ($_, @_) = @$_ if ref $_ eq "ARRAY";
32 # add default path unless specified
33 $_ = $this->{-path} . $this->{-seperator} . $_
34 if defined $this->{-seperator} and not /\Q$this->{-seperator}/;
35 # get localized string by identifier
36 if (exists $this->{$_}) {
37 $_ = $this->loc($this->{$_});
38 # adaptive string (code)
39 $_ = $_->(@_) if ref $_ eq "CODE";
41 #todo: else remove path
42 s/.*\Q$this->{-seperator}//s if defined $this->{-seperator};
44 # static output if no arguments given
45 return $_ unless @_; # unnecessary but faster for common case
47 return sprintf $_, @_;
51 my ($this, $option, $val) = @_;
52 if ($option eq "-langpref") {
53 # set order of languages (prefered language first)
54 $this->{$option} = $val;
56 elsif ($option eq "-seperator") {
57 $this->{-path} =~ s/\Q$this->{$option}/$val/g
58 if defined $this->{$option}; # replace old occurances
59 $this->{$option} = $val;
62 $this->{$option} = $val;
63 # $_[0]->{$_[1]} = $_[2];
67 # Same as found in Tie::StdHash
69 #todo: make path-aware
70 sub EXISTS { exists $_[0]->{$_[1]} }
71 sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
72 sub NEXTKEY { each %{$_[0]} }
81 Lirama::Loc3 - Localize strings
87 tie my %loc, "Lirama::Loc3", {
88 -langs => {en => 100, eo => 95},
91 en => "this is a test",
92 eo => "cxi tio estas testo",
96 $loc{-langpref} = [qw/nl en eo/]; # prefer I<nl> (dutch) texts
97 print $loc{test}; # "this is a test", since dutch is unavailable
101 Returns a text in the most preferred language available.
102 Mainly intended for translation of different strings on a website.
108 Shared so we only have to set one var to change all translations;
109 may yet be a very bad idea (does it work correctly in modperl?)
117 True if identifier is localized;
118 even though non-existing strings still return themselves.
124 L<Lirama::Loc3::Auto|Lirama::Loc3::Auto>
126 L<Locale::Maketext|Locale::Maketext>
130 Mischa POSLAWSKY <shiar@shiar.org>
132 Copyright 2005 Mischa POSLAWSKY. All rights reserved.