File Coverage

blib/lib/Log/Report/Translator/POT.pm
Criterion Covered Total %
statement 30 66 45.4
branch 0 24 0.0
condition 0 16 0.0
subroutine 10 17 58.8
pod 5 6 83.3
total 45 129 34.8


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