File Coverage

blib/lib/Log/Report/Translator/POT.pm
Criterion Covered Total %
statement 62 66 93.9
branch 13 24 54.1
condition 4 14 28.5
subroutine 16 17 94.1
pod 5 6 83.3
total 100 127 78.7


line stmt bran cond sub pod time code
1             # Copyrights 2007-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-Lexicon. 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::Translator::POT;
10 2     2   1827 use vars '$VERSION';
  2         3  
  2         100  
11             $VERSION = '1.11';
12              
13 2     2   9 use base 'Log::Report::Translator';
  2         3  
  2         469  
14              
15 2     2   89869 use warnings;
  2         3  
  2         39  
16 2     2   6 use strict;
  2         4  
  2         50  
17              
18 2     2   8 use Log::Report 'log-report-lexicon';
  2         3  
  2         8  
19              
20 2     2   820 use Log::Report::Lexicon::Index;
  2         3  
  2         52  
21 2     2   375 use Log::Report::Lexicon::POTcompact;
  2         3  
  2         45  
22              
23 2     2   8 use POSIX qw/:locale_h/;
  2         4  
  2         7  
24 2     2   249 use File::Spec ();
  2         4  
  2         52  
25              
26             my %lexicons;
27             sub _fn_to_lexdir($);
28              
29             # Work-around for missing LC_MESSAGES on old Perls and Windows
30 2     2   8 { no warnings;
  2         4  
  2         1260  
31             eval "&LC_MESSAGES";
32             *LC_MESSAGES = sub(){5} if $@;
33             }
34              
35              
36             sub new(@)
37 1     1 1 974 { my $class = shift;
38             # Caller cannot wait until init()
39 1         8 $class->SUPER::new(callerfn => (caller)[1], @_);
40             }
41              
42             sub init($)
43 1     1 0 6 { my ($self, $args) = @_;
44 1         5 $self->SUPER::init($args);
45              
46             my $lex = delete $args->{lexicons} || delete $args->{lexicon}
47 1   0     5 || (ref $self eq __PACKAGE__ ? [] : _fn_to_lexdir $args->{callerfn});
48              
49 1 50 50     5 error __x"You have to upgrade Log::Report::Lexicon to at least 1.00"
50             if +($Log::Report::Lexicon::Index::VERSION || 999) < 1.00;
51              
52 1         2 my @lex;
53 1 50       3 foreach my $dir (ref $lex eq 'ARRAY' ? @$lex : $lex)
54             { # lexicon indexes are shared
55 1   33     7 my $l = $lexicons{$dir} ||= Log::Report::Lexicon::Index->new($dir);
56 1         3 $l->index; # index the files now
57 1         3 push @lex, $l;
58             }
59 1         6 $self->{LRTP_lexicons} = \@lex;
60 1         2 $self->{LRTP_charset} = $args->{charset};
61 1         4 $self;
62             }
63              
64             sub _fn_to_lexdir($)
65 0     0   0 { my $fn = shift;
66 0         0 $fn =~ s/\.pm$//;
67 0         0 File::Spec->catdir($fn, 'messages');
68             }
69              
70             #------------
71              
72 13     13 1 328 sub lexicons() { @{shift->{LRTP_lexicons}} }
  13         28  
73              
74              
75 12     12 1 33 sub charset() { shift->{LRTP_charset} }
76              
77             #------------
78              
79             sub translate($;$$)
80 1     1 1 413 { my ($self, $msg, $lang, $ctxt) = @_;
81              
82 1         2 my $domain = $msg->{_domain};
83 1 50 33     14 my $locale = $lang || setlocale(LC_MESSAGES)
84             or return $self->SUPER::translate($msg, $lang, $ctxt);
85              
86             my $pot
87             = exists $self->{LRTP_pots}{$domain}{$locale}
88 1 50       4 ? $self->{LRTP_pots}{$domain}{$locale}
89             : $self->load($domain, $locale);
90              
91 1 50       6 ($pot ? $pot->msgstr($msg->{_msgid}, $msg->{_count}, $ctxt) : undef)
    50          
92             || $self->SUPER::translate($msg, $lang, $ctxt);
93             }
94              
95             sub load($$)
96 12     12 1 4724 { my ($self, $domain, $locale) = @_;
97              
98 12         20 foreach my $lex ($self->lexicons)
99 12         26 { my $fn = $lex->find($domain, $locale);
100              
101 12 50 33     26 !$fn && $lex->list($domain)
102             and last; # there are tables for domain, but not our lang
103              
104 12 50       16 $fn or next;
105              
106 12         44 my ($ext) = lc($fn) =~ m/\.(\w+)$/;
107 12 50       26 my $class
    100          
108             = $ext eq 'mo' ? 'Log::Report::Lexicon::MOTcompact'
109             : $ext eq 'po' ? 'Log::Report::Lexicon::POTcompact'
110             : error __x"unknown translation table extension '{ext}' in {filename}"
111             , ext => $ext, filename => $fn;
112              
113 12 50       34 info __x"read table {filename} as {class} for {domain} in {locale}"
114             , filename => $fn, class => $class, domain => $domain
115             , locale => $locale
116             if $domain ne 'log-report'; # avoid recursion
117              
118 12 50       1085 eval "require $class" or panic $@;
119            
120 12         35 return $self->{LRTP_pots}{$domain}{$locale}
121             = $class->read($fn, charset => $self->charset);
122             }
123              
124 0           $self->{LRTP_pots}{$domain}{$locale} = undef;
125             }
126              
127             1;