File Coverage

blib/lib/Plack/I18N/Lexicon/Maketext.pm
Criterion Covered Total %
statement 54 56 96.4
branch 11 14 78.5
condition 5 8 62.5
subroutine 13 15 86.6
pod 2 2 100.0
total 85 95 89.4


line stmt bran cond sub pod time code
1             package Plack::I18N::Lexicon::Maketext;
2              
3 3     3   23691 use strict;
  3         6  
  3         101  
4 3     3   11 use warnings;
  3         4  
  3         77  
5              
6 3     3   11 use Carp qw(croak);
  3         5  
  3         160  
7 3     3   14 use List::Util qw(first);
  3         2  
  3         209  
8 3     3   517 use Locale::Maketext;
  3         8802  
  3         64  
9 3     3   1075 use Plack::I18N::Util qw(try_load_class);
  3         7  
  3         147  
10 3     3   354 use Plack::I18N::Handle;
  3         4  
  3         1046  
11              
12             sub new {
13 24     24 1 2952 my $class = shift;
14 24         54 my (%params) = @_;
15              
16 24         22 my $self = {};
17 24         43 bless $self, $class;
18              
19 24   66     297 $self->{i18n_class} = $params{i18n_class} || croak 'i18n_class required';
20 23         27 $self->{locale_dir} = $params{locale_dir};
21 23   100     43 $self->{default_language} = $params{default_language} || 'en';
22              
23 23         35 $self->_init_lexicon;
24              
25 23         871 return $self;
26             }
27              
28             sub _init_lexicon {
29 23     23   21 my $self = shift;
30              
31 23         22 my $i18n_class = $self->{i18n_class};
32              
33 23 100       46 if (!try_load_class($i18n_class)) {
34 3 50   3   14 eval <<"EOC" or croak $@;
  3     0   3  
  3         13  
  3         225  
  0         0  
35             package $i18n_class;
36             use parent 'Locale::Maketext';
37             sub _loaded {1}
38             1;
39             EOC
40             }
41              
42 23         48 my $default_i18n_class = "$i18n_class\::$self->{default_language}";
43 23 100       44 if (!try_load_class($default_i18n_class)) {
44 3 50   3   13 eval <<"EOC" or croak $@;
  3     0   2  
  3         13  
  3         208  
  0         0  
45             package $default_i18n_class;
46             use parent -norequire, '$i18n_class';
47             our %Lexicon = (_AUTO => 1);
48             sub _loaded {1}
49             1;
50             EOC
51             }
52             }
53              
54             sub detect_languages {
55 21     21 1 25 my $self = shift;
56              
57 21   33     47 my $path = $self->{locale_dir} || $INC{$self->{i18n_class}} || join '/',
58             'lib', split /::/, $self->{i18n_class};
59              
60 21 50       545 opendir(my $dh, $path) or croak "Can't opendir $path: $!";
61 21 100       187 my @files = grep { /\.p[om]$/ && -e "$path/$_" } sort readdir($dh);
  63         422  
62 21         137 closedir $dh;
63              
64 21         33 my @languages = @files;
65 21         100 s{\.pm$}{} for @languages;
66              
67             unshift @languages, $self->{default_language}
68 21 100   21   149 unless first { $_ eq $self->{default_language} } @languages;
  21         91  
69              
70 21         192 return @languages;
71             }
72              
73             1;
74             __END__