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   23471 use strict;
  3         6  
  3         114  
4 3     3   13 use warnings;
  3         5  
  3         102  
5              
6 3     3   12 use Carp qw(croak);
  3         4  
  3         186  
7 3     3   14 use List::Util qw(first);
  3         5  
  3         247  
8 3     3   498 use Locale::Maketext;
  3         8580  
  3         78  
9 3     3   1326 use Plack::I18N::Util qw(try_load_class);
  3         7  
  3         174  
10 3     3   344 use Plack::I18N::Handle;
  3         6  
  3         1427  
11              
12             sub new {
13 24     24 1 3953 my $class = shift;
14 24         59 my (%params) = @_;
15              
16 24         33 my $self = {};
17 24         46 bless $self, $class;
18              
19 24   66     268 $self->{i18n_class} = $params{i18n_class} || croak 'i18n_class required';
20 23         32 $self->{locale_dir} = $params{locale_dir};
21 23   100     52 $self->{default_language} = $params{default_language} || 'en';
22              
23 23         43 $self->_init_lexicon;
24              
25 23         1368 return $self;
26             }
27              
28             sub _init_lexicon {
29 23     23   23 my $self = shift;
30              
31 23         35 my $i18n_class = $self->{i18n_class};
32              
33 23 100       55 if (!try_load_class($i18n_class)) {
34 3 50   3   17 eval <<"EOC" or croak $@;
  3     0   4  
  3         15  
  3         278  
  0         0  
35             package $i18n_class;
36             use parent 'Locale::Maketext';
37             sub _loaded {1}
38             1;
39             EOC
40             }
41              
42 23         70 my $default_i18n_class = "$i18n_class\::$self->{default_language}";
43 23 100       54 if (!try_load_class($default_i18n_class)) {
44 3 50   3   14 eval <<"EOC" or croak $@;
  3     0   6  
  3         15  
  3         243  
  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 30 my $self = shift;
56              
57 21   33     63 my $path = $self->{locale_dir} || $INC{$self->{i18n_class}} || join '/',
58             'lib', split /::/, $self->{i18n_class};
59              
60 21 50       708 opendir(my $dh, $path) or croak "Can't opendir $path: $!";
61 21 100       178 my @files = grep { /\.p[om]$/ && -e "$path/$_" } readdir($dh);
  63         490  
62 21         158 closedir $dh;
63              
64 21         37 my @languages = @files;
65 21         125 s{\.pm$}{} for @languages;
66              
67             unshift @languages, $self->{default_language}
68 21 100   21   169 unless first { $_ eq $self->{default_language} } @languages;
  21         103  
69              
70 21         236 return @languages;
71             }
72              
73             1;
74             __END__