File Coverage

blib/lib/Data/Localize/Localizer.pm
Criterion Covered Total %
statement 28 30 93.3
branch 4 6 66.6
condition n/a
subroutine 10 10 100.0
pod 2 3 66.6
total 44 49 89.8


line stmt bran cond sub pod time code
1             package Data::Localize::Localizer;
2 6     6   4188 use utf8;
  6         43  
  6         32  
3 6     6   205 use Moo;
  6         10  
  6         37  
4 6     6   1719 use Carp ();
  6         12  
  6         109  
5 6     6   26 use Data::Localize;
  6         10  
  6         111  
6 6     6   1941 use Encode ();
  6         40212  
  6         260  
7              
8             BEGIN {
9 6     6   1670 if (Data::Localize::DEBUG) {
10             require Data::Localize::Log;
11             Data::Localize::Log->import
12             }
13             }
14              
15             has _localizer => (
16             is => 'rw',
17             weak_ref => 1,
18             );
19              
20             has initialized => (
21             is => 'rw',
22             default => 0,
23             init_arg => undef,
24             );
25              
26             has formatter => (
27             is => 'rw',
28             lazy => 1,
29             isa => sub { $_[0]->isa('Data::Localize::Format') },
30             builder => "_build_formatter",
31             required => 1,
32             handles => { format_string => 'format' },
33             );
34              
35             sub _build_formatter {
36 7     7   182 Module::Load::load('Data::Localize::Format::Maketext');
37 7         219 return Data::Localize::Format::Maketext->new();
38             }
39              
40             sub BUILD {
41 15     15 0 18201 $_[0]->initialized(1);
42             }
43              
44             sub register {
45 5     5 1 26 my ($self, $loc) = @_;
46 5 50       78 if ($self->_localizer) {
47 0         0 Carp::confess("Localizer $self is already attached to another Data::Localize object ($loc)");
48             }
49 5         95 $self->_localizer( $loc );
50             }
51              
52             sub localize_for {
53 33     33 1 2263 my ($self, %args) = @_;
54 33         123 my ($lang, $id, $args) = @args{ qw(lang id args) };
55              
56 33 100       372 my $value = $self->get_lexicon($lang, $id) or return ();
57 27         34 if (Data::Localize::DEBUG()) {
58             debugf("localize_for - '%s' -> '%s'",
59             $id,
60             defined($value) ? Encode::encode_utf8($value) : '(null)'
61             );
62             }
63 27 50       62 if ( $value ) {
64 27         456 return $self->format_string($lang, $value, @$args);
65             }
66 0           return ();
67             }
68              
69             1;
70              
71             __END__