File Coverage

blib/lib/Data/Localize/Namespace.pm
Criterion Covered Total %
statement 57 62 91.9
branch 10 14 71.4
condition 2 3 66.6
subroutine 9 9 100.0
pod 3 3 100.0
total 81 91 89.0


line stmt bran cond sub pod time code
1             package Data::Localize::Namespace;
2 2     2   1624 use Moo;
  2         3  
  2         11  
3 2     2   1426 use Module::Pluggable::Object;
  2         11903  
  2         50  
4 2     2   1100 use Encode ();
  2         15005  
  2         40  
5 2     2   10 use Data::Localize;
  2         2  
  2         71  
6              
7             BEGIN {
8 2     2   713 if (Data::Localize::DEBUG) {
9             require Data::Localize::Log;
10             Data::Localize::Log->import;
11             }
12             }
13              
14             extends 'Data::Localize::Localizer';
15              
16             has _namespaces => (
17             is => 'rw',
18             default => sub { [] },
19             init_arg => 'namespaces',
20             );
21              
22             has _loaded_classes => (
23             is => 'ro',
24             default => sub { +{} }
25             );
26              
27             has _failed_classes => (
28             is => 'ro',
29             default => sub { +{} }
30             );
31              
32             around register => sub {
33             my ($next, $self, $loc) = @_;
34              
35             $self->$next($loc);
36             my $finder = Module::Pluggable::Object->new(
37             'require' => 1,
38             search_path => [ $self->namespaces ]
39             );
40              
41             # find the languages that we currently support
42             my $re = join('|', $self->namespaces);
43             foreach my $plugin ($finder->plugins) {
44             $plugin =~ s/^(?:$re):://;
45             $plugin =~ s/::/_/g;
46             if (Data::Localize::DEBUG) {
47             debugf("register - Registering for language %s -> $self",
48             $plugin,
49             $self
50             );
51             }
52             $loc->add_localizer_map($plugin, $self);
53             }
54             $loc->add_localizer_map('*', $self);
55             };
56              
57             sub add_namespaces {
58 1     1 1 8 my $self = shift;
59 1         1 unshift @{ $self->_namespaces }, @_;
  1         5  
60             }
61              
62             sub namespaces {
63 12     12 1 14 my $self = shift;
64 12         10 return @{ $self->_namespaces };
  12         49  
65             }
66              
67             sub get_lexicon {
68 6     6 1 7 my ($self, $lang, $id) = @_;
69              
70 6         14 $lang =~ s/-/_/g;
71              
72 6         11 my $LOADED = $self->_loaded_classes;
73 6         13 my $FAILED = $self->_failed_classes;
74 6         11 foreach my $namespace ($self->namespaces) {
75 6         14 my $klass = "$namespace\::$lang";
76              
77 6 50       13 if ($FAILED->{ $klass }) {
78 0         0 if (Data::Localize::DEBUG) {
79             debugf("get_lexicon - Already attempted loading %s and failed. Skipping...", $klass);
80             }
81 0         0 next;
82             }
83              
84 6         5 if (Data::Localize::DEBUG) {
85             debugf("get_lexicon - Trying %s", $klass);
86             }
87              
88             # Catch the very weird case where is_class_loaded() returns true
89             # but the class really hasn't been loaded yet.
90 2     2   11 no strict 'refs';
  2         3  
  2         641  
91 6         8 my $first_load = 0;
92 6 50       14 if (! $LOADED->{$klass}) {
93 6 100 66     7 if (%{"$klass\::Lexicon"} && %{"$klass\::"}) {
  6         43  
  4         26  
94 4         5 if (Data::Localize::DEBUG) {
95             debugf("get_lexicon - class %s already loaded", $klass);
96             }
97             } else {
98 2         2 if (Data::Localize::DEBUG) {
99             debugf("get_lexicon - loading %s", $klass);
100             }
101              
102 2         5 my $code =
103             "\n" .
104             "#line " . __LINE__ . ' "' . __FILE__ . '"' . "\n" .
105             "require $klass;"
106             ;
107 2         60 eval($code);
108 2 50       90 if ($@) {
109 0         0 if (Data::Localize::DEBUG) {
110             debugf("get_lexicon - Failed to load %s: %s", $klass, $@);
111             $FAILED->{$klass}++;
112             }
113 0         0 next;
114             }
115             }
116 6         7 if (Data::Localize::DEBUG) {
117             debugf("get_lexicon - setting %s to already loaded", $klass);
118             }
119 6         11 $LOADED->{$klass}++;
120 6         7 $first_load = 1;
121             }
122              
123 6         5 if (Data::Localize::DEBUG) {
124             debugf("get_lexicon - returning lexicon from %s (%d lexicons)",
125             $klass,
126             scalar keys %{"$klass\::Lexicon"},
127             );
128             }
129 6         7 my $h = \%{ "$klass\::Lexicon" };
  6         17  
130 6 50       11 if ($first_load) {
131 6         6 my %t;
132 6         21 while (my($k, $v) = each %$h) {
133 6 100       26 if ( ! Encode::is_utf8($k) ) {
134 3         14 $k = Encode::decode_utf8($k);
135             }
136 6 100       99 if ( ! Encode::is_utf8($v) ) {
137 2         5 $v = Encode::decode_utf8($v);
138             }
139 6         45 $t{$k} = $v;
140             }
141 6         11 %$h = ();
142 6         14 %$h = %t;
143             }
144 6         29 return $h->{$id};
145            
146             }
147 0           return ();
148             }
149              
150             1;
151              
152             __END__