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   4223 use utf8;
  6         29  
  6         28  
3 6     6   155 use Moo;
  6         8  
  6         28  
4 6     6   1391 use Carp ();
  6         7  
  6         97  
5 6     6   18 use Data::Localize;
  6         8  
  6         89  
6 6     6   2108 use Encode ();
  6         29692  
  6         261  
7              
8             BEGIN {
9 6     6   1658 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   2378 Module::Load::load('Data::Localize::Format::Maketext');
37 7         211 return Data::Localize::Format::Maketext->new();
38             }
39              
40             sub BUILD {
41 15     15 0 5510 $_[0]->initialized(1);
42             }
43              
44             sub register {
45 5     5 1 14 my ($self, $loc) = @_;
46 5 50       51 if ($self->_localizer) {
47 0         0 Carp::confess("Localizer $self is already attached to another Data::Localize object ($loc)");
48             }
49 5         1274 $self->_localizer( $loc );
50             }
51              
52             sub localize_for {
53 29     29 1 2062 my ($self, %args) = @_;
54 29         57 my ($lang, $id, $args) = @args{ qw(lang id args) };
55              
56 29 100       345 my $value = $self->get_lexicon($lang, $id) or return ();
57 23         25 if (Data::Localize::DEBUG()) {
58             debugf("localize_for - '%s' -> '%s'",
59             $id,
60             defined($value) ? Encode::encode_utf8($value) : '(null)'
61             );
62             }
63 23 50       39 if ( $value ) {
64 23         407 return $self->format_string($lang, $value, @$args);
65             }
66 0           return ();
67             }
68              
69             1;
70              
71             __END__