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