File Coverage

lib/Log/Report/Template/Textdomain.pm
Criterion Covered Total %
statement 48 53 90.5
branch 8 12 66.6
condition 4 8 50.0
subroutine 14 15 93.3
pod 4 6 66.6
total 78 94 82.9


line stmt bran cond sub pod time code
1             # Copyrights 2017-2018 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             # This code is part of distribution Log-Report-Template. Meta-POD processed
6             # with OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package Log::Report::Template::Textdomain;
10 7     7   38 use vars '$VERSION';
  7         12  
  7         290  
11             $VERSION = '0.13';
12              
13 7     7   31 use base 'Log::Report::Domain';
  7         13  
  7         513  
14              
15 7     7   34 use warnings;
  7         13  
  7         146  
16 7     7   27 use strict;
  7         12  
  7         153  
17              
18 7     7   31 use Log::Report 'log-report-template';
  7         10  
  7         30  
19              
20 7     7   1316 use Log::Report::Message ();
  7         13  
  7         3362  
21              
22              
23             sub init($)
24 7     7 0 54 { my ($self, $args) = @_;
25 7         32 $self->SUPER::init($args);
26              
27 7 100       106 if(my $only = $args->{only_in_directory})
28 2 50       9 { my @only = ref $only eq 'ARRAY' ? @$only : $only;
29 2         10 my $dirs = join '|', map "\Q$_\E", @only;
30 2         37 $self->{LRTT_only_in} = qr!^(?:$dirs)(?:$|/)!;
31             }
32              
33 7   100     28 $self->{LRTT_function} = $args->{translation_function} || 'loc';
34 7         13 my $lexicon = $self->{LRTT_lexicon} = $args->{lexicon};
35 7         19 $self;
36             }
37              
38             #----------------
39              
40 14     14 1 589 sub function() { shift->{LRTT_function} }
41              
42              
43 2     2 1 14 sub lexicon() { shift->{LRTT_lexicon} }
44              
45              
46             sub expectedIn($)
47 3     3 1 8 { my ($self, $fn) = @_;
48 3 100       11 my $only = $self->{LRTT_only_in} or return 1;
49 2         14 $fn =~ $only;
50             }
51              
52             #----------------
53              
54             sub translationFunction($)
55 6     6 1 33 { my ($self, $service) = @_;
56 6         11 my $lang = 'NL';
57              
58             # Prepare as much and fast as possible, because it gets called often!
59             sub { # called with ($msgid, \%params)
60 4     4   31622 $_[1]->{_stash} = $service->{CONTEXT}{STASH};
61 4         28 Log::Report::Message->fromTemplateToolkit($self, @_)->toString($lang);
62 6         33 };
63             }
64              
65             sub translationFilter()
66 6     6 0 9 { my $self = shift;
67 6         15 my $domain = $self->name;
68 6         17 my $lang = 'NL';
69              
70             # Prepare as much and fast as possible, because it gets called often!
71             # A TT filter can be either static or dynamic. Dynamic filters need to
72             # implement a "a factory for static filters": a sub which produces a
73             # sub which does the real work.
74             sub {
75 0     0   0 my $context = shift;
76 0 0 0     0 my $pairs = pop if @_ && ref $_[-1] eq 'HASH';
77             sub { # called with $msgid (template container content) only, the
78             # parameters are caught when the factory produces this sub.
79 0         0 $pairs->{_stash} = $context->{STASH};
80 0         0 Log::Report::Message->fromTemplateToolkit($self, $_[0], $pairs)
81             ->toString($lang);
82             }
83 6         45 };
  0         0  
84             }
85              
86             sub _reportMissingKey($$)
87 2     2   303 { my ($self, $sp, $key, $args) = @_;
88              
89             # Try to grab the value from the stash. That's a major advantange
90             # of TT over plain Perl: we have access to the variable namespace.
91              
92 2         3 my $stash = $args->{_stash};
93 2 50       8 if($stash)
94 2         14 { my $value = $stash->get($key);
95 2 100 66     21 return $value if defined $value && length $value;
96             }
97              
98             warning
99             __x"Missing key '{key}' in format '{format}', in {use //template}"
100             , key => $key, format => $args->{_format}
101 1         7 , use => $stash->{template}{name};
102              
103 1         301 undef;
104             }
105              
106             1;