Lirama::Loc3
[perl/loc/.git] / Lirama / Loc3.pm
1 package Lirama::Loc3;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '3.00';
7
8 sub loc($) {
9         my $this = shift;
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}};
15 } # loc
16
17 sub TIEHASH {
18         #todo: set -path
19         return bless $_[1], $_[0];  # bless the l10n hash
20 }
21
22 sub FETCH {
23         my $this = shift;
24         # get setting (denoted by leading dash)
25         return wantarray ? @{$this->{$_[0]}} : $this->{$_[0]}->[0]
26                 if $_[0] eq "-langpref";
27         return $this->{$_[0]}
28                 if $_[0] eq "-path";
29         # array ref used for passing arguments
30         @_ = @{$_[0]} if ref $_[0] eq "ARRAY";
31         # get localized string by identifier
32         local $_ = shift;
33         # add default path unless specified
34         $_ = $this->{-path} . '_' . $_ unless /_/;
35                         #todo: shouldn't occur - find out where this is done, then fix and remove this check
36                 #       defined $_ or return '';
37         $_ = $this->loc($this->{$_}) if exists $this->{$_};
38         #todo: else remove path
39         # adaptive string (code)
40         $_ = $_->(@_) if ref $_ eq "CODE";
41         # static output if no arguments given
42         return $_ unless @_;  # unnecessary but faster for common case
43         # dynamic output
44         return sprintf $_, @_;
45 } # FETCH
46
47 sub STORE {
48         my ($this, $option, $val) = @_;
49         if ($option eq "-langpref") {
50                 # set order of languages (prefered language first)
51                 $this->{$option} = $val;
52         } # -langpref
53         else {
54                 $this->{$option} = $val;
55 #               $_[0]->{$_[1]} = $_[2];
56         }
57 } # STORE
58
59 # Same as found in Tie::StdHash
60
61 #todo: make path-aware
62 sub EXISTS   { exists $_[0]->{$_[1]} }
63 sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
64 sub NEXTKEY  { each %{$_[0]} }
65
66 1;
67
68 __END__
69
70
71 =head1 NAME
72
73 Lirama::Loc3 - Localize strings
74
75 =head1 SYNOPSIS
76
77         use Lirama::Loc3;
78
79         tie my %loc, "Lirama::Loc3", {
80                 _test => {
81                         en => "this is a test",
82                         eo => "cxi tio estas testo",
83                 },
84         };
85
86         $loc{-langpref} = [qw/nl en eo/];  # prefer I<nl> (dutch) texts
87         print $loc{test};  # "this is a test", since dutch is unavailable
88
89 =head1 DESCRIPTION
90
91 Returns a text in the most preferred language available.
92 Mainly intended for translation of different strings on a website.
93
94 =over 4
95
96 =item C<langpref>
97
98 Shared so we only have to set one var to change all translations;
99 may yet be a very bad idea (does it work correctly in modperl?)
100
101 =item C<tie>
102
103 =item C<loc>
104
105 =item C<exists>
106
107 True if identifier is localized;
108 even though non-existing strings still return themselves.
109
110 =back
111
112 =head1 SEE ALSO
113
114 L<Lirama::Loc3::Auto|Lirama::Loc3::Auto>
115
116 L<Locale::Maketext|Locale::Maketext>
117
118 =head1 AUTHOR
119
120 Mischa POSLAWSKY <shiar@shiar.org>
121
122 Copyright 2005 Mischa POSLAWSKY. All rights reserved.
123
124 =cut