File Coverage

blib/lib/Log/Report/Domain.pm
Criterion Covered Total %
statement 50 94 53.1
branch 8 42 19.0
condition 7 18 38.8
subroutine 13 20 65.0
pod 9 10 90.0
total 87 184 47.2


line stmt bran cond sub pod time code
1             # Copyrights 2007-2017 by [Mark Overmeer].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5 13     13   74 use warnings;
  13         25  
  13         385  
6 13     13   65 use strict;
  13         24  
  13         312  
7              
8             package Log::Report::Domain;
9 13     13   56 use vars '$VERSION';
  13         25  
  13         520  
10             $VERSION = '1.21';
11              
12 13     13   69 use base 'Log::Report::Minimal::Domain';
  13         27  
  13         5012  
13              
14 13     13   6891 use Log::Report 'log-report';
  13         32  
  13         68  
15 13     13   142 use Log::Report::Util qw/parse_locale/;
  13         25  
  13         763  
16 13     13   79 use Scalar::Util qw/blessed/;
  13         25  
  13         518  
17              
18 13     13   4054 use Log::Report::Translator;
  13         33  
  13         11060  
19              
20              
21             sub init($)
22 7     7 0 68 { my ($self, $args) = @_;
23 7         43 $self->SUPER::init($args);
24 7         68 $self->{LRD_ctxt_def} = {};
25 7         30 $self;
26             }
27              
28             #----------------
29              
30 10     10 1 24 sub nativeLanguage() {shift->{LRD_native}}
31 94     94 1 271 sub translator() {shift->{LRD_transl}}
32 0     0 1 0 sub contextRules() {shift->{LRD_ctxt_rules}}
33              
34             #----------------
35              
36             sub configure(%)
37 7     7 1 25 { my ($self, %args) = @_;
38              
39 7 50       31 if(my $config = delete $args{config})
40 0         0 { my $set = $self->readConfig($config);
41 0         0 %args = (%$set, %args);
42             }
43              
44             # 'formatter' is mainly handled by the base-class, but documented here.
45 7   50     38 my $format = $args{formatter} || 'PRINTI';
46 7 50       41 $args{formatter} = $format = {} if $format eq 'PRINTI';
47              
48 7 50       30 if(ref $format eq 'HASH')
49 7     0   37 { $format->{missing_key} = sub {$self->_reportMissingKey(@_)};
  0         0  
50             }
51              
52 7         85 $self->SUPER::configure(%args);
53              
54 7   33     578 my $transl = $args{translator} || Log::Report::Translator->new;
55 7 50       29 $transl = Log::Report::Translator->new(%$transl)
56             if ref $transl eq 'HASH';
57              
58 7 50 33     100 !blessed $transl || $transl->isa('Log::Report::Translator')
59             or panic "translator must be a Log::Report::Translator object";
60 7         18 $self->{LRD_transl} = $transl;
61              
62             my $native = $self->{LRD_native}
63 7   50     42 = $args{native_language} || 'en_US';
64              
65 7         31 my ($lang) = parse_locale $native;
66 7 50       332 defined $lang
67             or error __x"the native_language '{locale}' is not a valid locale"
68             , locale => $native;
69              
70 7 50       31 if(my $cr = $args{context_rules})
71 0         0 { my $tc = 'Log::Report::Translator::Context';
72 0 0       0 eval "require $tc"; panic $@ if $@;
  0         0  
73 0 0       0 if(blessed $cr)
    0          
74 0 0       0 { $cr->isa($tc) or panic "context_rules must be a $tc" }
75             elsif(ref $cr eq 'HASH')
76 0         0 { $cr = Log::Report::Translator::Context->new(rules => $cr) }
77             else
78 0         0 { panic "context_rules expects object or hash, not {have}", have=>$cr;
79             }
80              
81 0         0 $self->{LRD_ctxt_rules} = $cr;
82             }
83              
84 7         30 $self;
85             }
86              
87             sub _reportMissingKey($$)
88 0     0   0 { my ($self, $sp, $key, $args) = @_;
89              
90             warning
91             __x"Missing key '{key}' in format '{format}', file {use}"
92             , key => $key, format => $args->{_format}
93 0         0 , use => $args->{_use};
94              
95 0         0 undef;
96             }
97              
98              
99             sub setContext(@)
100 0     0 1 0 { my $self = shift;
101 0 0       0 my $cr = $self->contextRules # ignore context if no rules given
102             or error __x"you need to configure context_rules before setContext";
103              
104 0         0 $self->{LRD_ctxt_def} = $cr->needDecode(set => @_);
105             }
106              
107              
108             sub updateContext(@)
109 0     0 1 0 { my $self = shift;
110 0 0       0 my $cr = $self->contextRules # ignore context if no rules given
111             or return;
112              
113 0         0 my $rules = $cr->needDecode(update => @_);
114 0   0     0 my $r = $self->{LRD_ctxt_def} ||= {};
115 0         0 @{$r}{keys %$r} = values %$r;
  0         0  
116 0         0 $r;
117             }
118              
119              
120 0     0 1 0 sub defaultContext() { shift->{LRD_ctxt_def} }
121              
122              
123             sub readConfig($)
124 0     0 1 0 { my ($self, $fn) = @_;
125 0         0 my $config;
126              
127 0 0       0 if($fn =~ m/\.pl$/i)
    0          
128 0         0 { $config = do $fn;
129             }
130             elsif($fn =~ m/\.json$/i)
131 0 0       0 { eval "require JSON"; panic $@ if $@;
  0         0  
132 0 0       0 open my($fh), '<:encoding(utf8)', $fn
133             or fault __x"cannot open JSON file for context at {fn}"
134             , fn => $fn;
135 0         0 local $/;
136 0         0 $config = JSON->utf8->decode(<$fh>);
137             }
138             else
139 0         0 { error __x"unsupported context file type for {fn}", fn => $fn;
140             }
141              
142 0         0 $config;
143             }
144              
145             #-------------------
146              
147             sub translate($$)
148 87     87 1 177 { my ($self, $msg, $lang) = @_;
149 87   66     182 my $tr = $self->translator || $self->configure->translator;
150 87         209 my $msgid = $msg->msgid;
151              
152             # fast route when certainly no context is involved
153 87 50 33     368 return $tr->translate($msg, $lang) || $msgid
154             if index($msgid, '<') == -1;
155              
156 0           my $msgctxt;
157 0 0         if(my $rules = $self->contextRules)
158 0           { ($msgid, $msgctxt)
159             = $rules->ctxtFor($msg, $lang, $self->defaultContext);
160             }
161             else
162 0           { 1 while $msgid =~
163 0 0         s/\{([^}]*)\<\w+([^}]*)\}/length "$1$2" ? "{$1$2}" : ''/e;
164             }
165              
166             # This is ugly, horrible and worse... but I do not want to mutulate
167             # the message neither to clone it for performance. We do need to get
168             # rit of {<}
169 0           local $msg->{_msgid} = $msgid;
170 0 0         $tr->translate($msg, $lang, $msgctxt) || $msgid;
171             }
172              
173             1;
174              
175             __END__