File Coverage

blib/lib/Log/Report/Translator/POT.pm
Criterion Covered Total %
statement 63 67 94.0
branch 14 26 53.8
condition 4 14 28.5
subroutine 16 17 94.1
pod 5 6 83.3
total 102 130 78.4


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution Log-Report-Lexicon version 1.15.
2             # The POD got stripped from this file by OODoc version 3.05.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2007-2025 by Mark Overmeer.
6              
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10              
11             #oodist: *** DO NOT USE THIS VERSION FOR PRODUCTION ***
12             #oodist: This file contains OODoc-style documentation which will get stripped
13             #oodist: during its release in the distribution. You can use this file for
14             #oodist: testing, however the code of this development version may be broken!
15             #oorestyle: old style disclaimer to be removed.
16              
17             # This code is part of distribution Log-Report-Lexicon. Meta-POD processed
18             # with OODoc into POD and HTML manual-pages. See README.md
19             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
20              
21             package Log::Report::Translator::POT;{
22             our $VERSION = '1.15';
23             }
24              
25 2     2   441354 use base 'Log::Report::Translator';
  2         5  
  2         794  
26              
27 2     2   105152 use warnings;
  2         4  
  2         99  
28 2     2   18 use strict;
  2         4  
  2         57  
29              
30 2     2   10 use Log::Report 'log-report-lexicon';
  2         4  
  2         10  
31              
32 2     2   1326 use Log::Report::Lexicon::Index;
  2         4  
  2         61  
33 2     2   613 use Log::Report::Lexicon::POTcompact;
  2         7  
  2         78  
34              
35 2     2   11 use POSIX qw/:locale_h/;
  2         4  
  2         15  
36 2     2   420 use Scalar::Util qw/blessed/;
  2         4  
  2         89  
37 2     2   50 use File::Spec ();
  2         4  
  2         79  
38              
39             my %lexicons;
40             sub _fn_to_lexdir($);
41              
42             # Work-around for missing LC_MESSAGES on old Perls and Windows
43 2     2   8 { no warnings;
  2         3  
  2         1904  
44             eval "&LC_MESSAGES";
45             *LC_MESSAGES = sub(){5} if $@;
46             }
47              
48             #--------------------
49              
50              
51             sub new(@)
52 1     1 1 1406 { my $class = shift;
53             # Caller cannot wait until init()
54 1         11 $class->SUPER::new(callerfn => (caller)[1], @_);
55             }
56              
57             sub init($)
58 1     1 0 10 { my ($self, $args) = @_;
59 1         6 $self->SUPER::init($args);
60              
61             my $lex = delete $args->{lexicons} || delete $args->{lexicon} ||
62 1   0     6 (ref $self eq __PACKAGE__ ? [] : _fn_to_lexdir $args->{callerfn});
63              
64 1 50 50     9 +($Log::Report::Lexicon::Index::VERSION || 999) >= 1.00
65             or error __x"You have to upgrade Log::Report::Lexicon to at least 1.00";
66              
67 1         3 my @lex;
68 1 50       4 foreach my $dir (ref $lex eq 'ARRAY' ? @$lex : $lex)
69             { # lexicon indexes are shared
70 1   33     10 my $l = $lexicons{$dir} ||= Log::Report::Lexicon::Index->new($dir);
71 1         5 $l->index; # index the files now
72 1         3 push @lex, $l;
73             }
74 1         34 $self->{LRTP_lexicons} = \@lex;
75 1         4 $self->{LRTP_charset} = $args->{charset};
76 1         8 $self;
77             }
78              
79             sub _fn_to_lexdir($)
80 0     0   0 { my $fn = shift;
81 0         0 $fn =~ s/\.pm$//;
82 0         0 File::Spec->catdir($fn, 'messages');
83             }
84              
85             #--------------------
86              
87 13     13 1 536 sub lexicons() { @{ $_[0]->{LRTP_lexicons}} }
  13         32  
88              
89              
90 12     12 1 43 sub charset() { $_[0]->{LRTP_charset} }
91              
92             #--------------------
93              
94             sub translate($;$$)
95 1     1 1 638 { my ($self, $msg, $lang, $ctxt) = @_;
96             #!!! do not debug with $msg in a print: recursion
97              
98 1         2 my $domain = $msg->{_domain};
99 1 50       3 my $dname = blessed $domain ? $domain->name : $domain;
100              
101 1 50 33     5 my $locale = $lang || setlocale(LC_MESSAGES)
102             or return $self->SUPER::translate($msg, $lang, $ctxt);
103              
104             my $pot
105             = exists $self->{LRTP_pots}{$dname}{$locale}
106 1 50       3 ? $self->{LRTP_pots}{$dname}{$locale}
107             : $self->load($dname, $locale);
108              
109 1 50       7 ($pot ? $pot->msgstr($msg->{_msgid}, $msg->{_count}, $ctxt) : undef)
    50          
110             || $self->SUPER::translate($msg, $lang, $ctxt);
111             }
112              
113             sub load($$)
114 12     12 1 8359 { my ($self, $dname, $locale) = @_;
115              
116 12         25 foreach my $lex ($self->lexicons)
117 12         36 { my $fn = $lex->find($dname, $locale);
118              
119 12 50 33     45 !$fn && $lex->list($dname)
120             and last; # there are tables for dname, but not our lang
121              
122 12 50       20 $fn or next;
123              
124 12         48 my ($ext) = lc($fn) =~ m/\.(\w+)$/;
125 12 50       28 my $class
    100          
126             = $ext eq 'mo' ? 'Log::Report::Lexicon::MOTcompact'
127             : $ext eq 'po' ? 'Log::Report::Lexicon::POTcompact'
128             : error __x"unknown translation table extension '{ext}' in {filename}", ext => $ext, filename => $fn;
129              
130 12 50       46 info __x"read table {filename} as {class} for {dname} in {locale}", filename => $fn, class => $class, dname => $dname, locale => $locale
131             if $dname ne 'log-report'; # avoid recursion
132              
133 12 50       1682 eval "require $class" or panic $@;
134              
135 12         47 return $self->{LRTP_pots}{$dname}{$locale} = $class->read($fn, charset => $self->charset);
136             }
137              
138 0           $self->{LRTP_pots}{$dname}{$locale} = undef;
139             }
140              
141             1;