File Coverage

blib/lib/Rose/HTML/Object/Message/Localized.pm
Criterion Covered Total %
statement 49 59 83.0
branch 17 28 60.7
condition 1 6 16.6
subroutine 13 14 92.8
pod 4 5 80.0
total 84 112 75.0


line stmt bran cond sub pod time code
1              
2             use strict;
3 43     43   260  
  43         82  
  43         1078  
4             use Carp;
5 43     43   361 use Rose::HTML::Object::Message::Localizer;
  43         74  
  43         1937  
6 43     43   228  
  43         77  
  43         847  
7             use base 'Rose::HTML::Object::Message';
8 43     43   186  
  43         89  
  43         16962  
9             our $VERSION = '0.600';
10              
11             #our $Debug = 0;
12              
13             use overload
14             (
15             '""' => sub { shift->localized_text },
16 2192     2192   28644 'bool' => sub { 1 },
17 108     108   268 '0+' => sub { 1 },
18 0     0   0 fallback => 1,
19 43         314 );
20 43     43   252  
  43         78  
21             use Rose::Class::MakeMethods::Generic
22             (
23             inheritable_scalar =>
24 43         283 [
25             '_default_localizer',
26             'default_locale',
27             ],
28             );
29 43     43   3582  
  43         98  
30             __PACKAGE__->default_locale('en');
31              
32              
33 1     1 0 6 {
34             my($self) = shift;
35              
36             my $localizer = $self->localizer;
37 2198     2198 1 3158  
38             return $localizer->localize_message(
39 2198         3848 message => $self,
40             parent => $self->parent,
41 2198         4928 locale => $self->locale,
42             variant => $self->variant,
43             args => scalar $self->args);
44             }
45              
46             {
47             my($invocant) = shift;
48              
49             # Called as object method
50             if(my $class = ref $invocant)
51 2199     2199 1 2905 {
52             if(@_)
53             {
54 2199 50       4460 return $invocant->{'localizer'} = shift;
55             }
56 2199 50       4046  
57             my $localizer = $invocant->{'localizer'};
58 0         0  
59             unless($localizer)
60             {
61 2199         3340 if(my $parent = $invocant->parent)
62             {
63 2199 50       4006 if(my $localizer = $parent->localizer)
64             {
65 2199 100       4657 return $localizer;
66             }
67 2193 50       4715 }
68             else { return $class->default_localizer }
69 2193         76171 }
70              
71             return $localizer || $class->default_localizer;
72 6         12 }
73             else # Called as class method
74             {
75 0   0     0 if(@_)
76             {
77             return $invocant->default_localizer(shift);
78             }
79 0 0       0  
80             return $invocant->default_localizer;
81 0         0 }
82             }
83              
84 0         0 {
85             my($class) = shift;
86              
87             if(@_)
88             {
89             return $class->_default_localizer(@_);
90 7     7 1 11 }
91              
92 7 50       13 if(my $localizer = $class->_default_localizer)
93             {
94 0         0 return $localizer;
95             }
96              
97 7 100       15 return $class->_default_localizer($class->generic_object_class->localizer);
98             }
99 6         47  
100             {
101             my($invocant) = shift;
102 1         37  
103             # Called as object method
104             if(my $class = ref $invocant)
105             {
106             if(@_)
107 2202     2202 1 3209 {
108             return $invocant->{'locale'} = shift;
109             }
110 2202 50       4174  
111             my $locale = $invocant->{'locale'};
112 2202 100       3750  
113             unless($locale)
114 3         8 {
115             if(my $parent = $invocant->parent)
116             {
117 2199         3100 if(my $locale = $parent->locale)
118             {
119 2199 100       3571 return $locale;
120             }
121 2195 100       3789 }
122             else { return $class->default_locale }
123 2192 50       4300 }
124              
125 2192         7701 return $locale || $class->default_locale;
126             }
127             else # Called as class method
128 3         8 {
129             if(@_)
130             {
131 4   33     15 return $invocant->default_locale(shift);
132             }
133              
134             return $invocant->default_locale;
135 0 0         }
136             }
137 0            
138             1;
139              
140 0            
141             =encoding utf-8
142              
143             =head1 NAME
144              
145             Rose::HTML::Object::Message::Localized - Localized message object.
146              
147             =head1 SYNOPSIS
148              
149             use Rose::HTML::Form::Field::Integer;
150             Rose::HTML::Form::Field::Integer->load_all_messages;
151              
152             use Rose::HTML::Object::Messages qw(NUM_INVALID_INTEGER);
153              
154             $localizer = Rose::HTML::Object->default_localizer;
155              
156             $msg =
157             Rose::HTML::Object::Message::Localized->new(
158             localizer => $localizer,
159             id => NUM_INVALID_INTEGER,
160             args => { label => 'XYZ' });
161              
162             print $msg->localized_text; # XYZ must be an integer.
163              
164             $msg->locale('fr');
165              
166             print $msg->localized_text; # XYZ doit être un entier.
167              
168             =head1 DESCRIPTION
169              
170             L<Rose::HTML::Object::Message::Localized> objects encapsulate a localized text message with an integer L<id|/id> and an optional set of name/value pairs to be used to fill in any placeholders in the message text.
171              
172             This class inherits from L<Rose::HTML::Object::Message>. See the L<Rose::HTML::Object::Message> documentation for more information.
173              
174             =head1 OVERLOADING
175              
176             Stringification is overloaded to call the L<localized_text|/localized_text> method. In numeric and boolean contexts, L<Rose::HTML::Object::Message::Localized> objects always evaluate to true.
177              
178             =head1 CLASS METHODS
179              
180             =over 4
181              
182             =item B<default_locale [LOCALE]>
183              
184             Get or set the default L<locale|Rose::HTML::Object::Message::Localizer/LOCALES>. Defaults to C<en>.
185              
186             =item B<default_localizer [LOCALIZER]>
187              
188             Get or set the default L<Rose::HTML::Object::Message::Localizer>-derived localizer object. Defaults to the L<default_localizer|Rose::HTML::Object/default_localizer> of the generic object class for this HTML object class hierarchy (L<Rose::HTML::Object>, by default).
189              
190             =back
191              
192             =head1 CONSTRUCTOR
193              
194             =over 4
195              
196             =item B<new [ PARAMS | TEXT ]>
197              
198             Constructs a new L<Rose::HTML::Object::Message::Localized> object. If a single argument is passed, it is taken as the value for the L<text|Rose::HTML::Object::Message/text> parameter. Otherwise, PARAMS name/value pairs are expected. Any object method is a valid parameter name.
199              
200             =back
201              
202             =head1 OBJECT METHODS
203              
204             =over 4
205              
206             =item B<args [ PARAMS | HASHREF ]>
207              
208             Get or set the name/value pairs to be used to fill in any placeholders in the localized message text. To set, pass a list of name/value pairs or a reference to a hash of name/value pairs. Values must be strings, code references, or references to arrays of strings or code references. Code references are evaluated each time a message with placeholders is constructed.
209              
210             See the L<LOCALIZED TEXT|Rose::HTML::Object::Message::Localizer/"LOCALIZED TEXT"> section of the L<Rose::HTML::Object::Message::Localizer> documentation for more information on message text placeholders.
211              
212             =item B<id [INT]>
213              
214             Get or set the message's integer identifier.
215              
216             =item B<locale [LOCALE]>
217              
218             Get or set the L<locale string|Rose::HTML::Object::Message::Localizer/LOCALES> for this message. If no locale is set but a L<parent|/parent> is defined and has a locale, then the L<parent|/parent>'s C<locale()> is returned. If the L<parent|/parent> doesn't exist or has no locale set, the L<default_locale|/default_locale> is returned.
219              
220             =item B<localizer [LOCALIZER]>
221              
222             Get or set the L<Rose::HTML::Object::Message::Localizer>-derived object used to localize message text. If no localizer is set but a L<parent|/parent> is defined, then the L<parent|/parent>'s C<localizer()> is returned. Otherwise, the L<default_localizer|/default_localizer> is returned.
223              
224             =item B<localized_text>
225              
226             Asks the L<localizer|/localizer> to produce the localized version of the message text for the current L<locale|/locale> and L<args|/args>. The localized text is returned.
227              
228             =item B<parent [OBJECT]>
229              
230             Get or set a L<weakened|Scalar::Util/weaken> reference to a parent object. This parent must have a C<localizer()> method that returns a L<Rose::HTML::Object::Message::Localizer>-derived object and a C<locale()> method that returns a L<locale string|Rose::HTML::Object::Message::Localizer/LOCALES>.
231              
232             =back
233              
234             =head1 AUTHOR
235              
236             John C. Siracusa (siracusa@gmail.com)
237              
238             =head1 LICENSE
239              
240             Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.