File Coverage

blib/lib/Plack/I18N/Lexicon/Gettext.pm
Criterion Covered Total %
statement 57 57 100.0
branch 6 10 60.0
condition 5 8 62.5
subroutine 15 15 100.0
pod 2 2 100.0
total 85 92 92.3


line stmt bran cond sub pod time code
1             package Plack::I18N::Lexicon::Gettext;
2              
3 1     1   23101 use strict;
  1         2  
  1         31  
4 1     1   4 use warnings;
  1         1  
  1         26  
5              
6 1     1   4 use Carp qw(croak);
  1         2  
  1         49  
7 1     1   5 use List::Util qw(first);
  1         1  
  1         82  
8 1     1   549 use Locale::Maketext;
  1         8653  
  1         34  
9 1     1   370 use Plack::I18N::Util qw(try_load_class);
  1         3  
  1         65  
10 1     1   380 use Plack::I18N::Handle;
  1         2  
  1         296  
11              
12             sub new {
13 4     4 1 7283 my $class = shift;
14 4         15 my (%params) = @_;
15              
16 4         8 my $self = {};
17 4         10 bless $self, $class;
18              
19 4   66     211 $self->{i18n_class} = $params{i18n_class} || croak 'i18n_class required';
20 3   50     15 $self->{default_language} = $params{default_language} || 'en';
21              
22 3   66     140 $self->{locale_dir} = $params{locale_dir} || croak 'locale_dir required';
23              
24 2         6 $self->_init_lexicon;
25              
26 2         17 return $self;
27             }
28              
29             sub _init_lexicon {
30 2     2   3 my $self = shift;
31              
32 2         4 my $i18n_class = $self->{i18n_class};
33              
34 2 50       9 if (!try_load_class($i18n_class)) {
35 1 50   1   6 eval <<"EOC" || croak $@;
  1     1   1  
  1     1   9  
  1     1   95  
  1         2  
  1         10  
  1         7  
  1         2  
  1         5  
  1         98  
  1         2  
  1         8  
  2         188  
36             package $i18n_class;
37             use parent 'Locale::Maketext';
38             use Locale::Maketext::Lexicon {
39             '*' => [Gettext => "$self->{locale_dir}/*.po"],
40             _auto => 1,
41             _decode => 1,
42             _preload => 1
43             };
44             1;
45             EOC
46             }
47             }
48              
49             sub detect_languages {
50 1     1 1 6 my $self = shift;
51              
52 1         3 my $path = $self->{locale_dir};
53              
54 1 50       40 opendir(my $dh, $path) or croak "Can't opendir $path: $!";
55 1 100       12 my @files = grep { /\.p[om]$/ && -e "$path/$_" } readdir($dh);
  4         31  
56 1         15 closedir $dh;
57              
58 1         2 my @languages = @files;
59 1         8 s{\.p[om]$}{} for @languages;
60              
61             unshift @languages, $self->{default_language}
62 1 50   1   24 unless first { $_ eq $self->{default_language} } @languages;
  1         8  
63              
64 1         11 return @languages;
65             }
66              
67             1;
68             __END__