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   1611 use Moo;
  2         4  
  2         11  
3 2     2   1488 use Module::Pluggable::Object;
  2         12859  
  2         61  
4 2     2   1051 use Encode ();
  2         17853  
  2         41  
5 2     2   11 use Data::Localize;
  2         3  
  2         69  
6              
7             BEGIN {
8 2     2   720 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 9 my $self = shift;
59 1         1 unshift @{ $self->_namespaces }, @_;
  1         6  
60             }
61              
62             sub namespaces {
63 12     12 1 16 my $self = shift;
64 12         13 return @{ $self->_namespaces };
  12         53  
65             }
66              
67             sub get_lexicon {
68 6     6 1 10 my ($self, $lang, $id) = @_;
69              
70 6         13 $lang =~ s/-/_/g;
71              
72 6         13 my $LOADED = $self->_loaded_classes;
73 6         11 my $FAILED = $self->_failed_classes;
74 6         13 foreach my $namespace ($self->namespaces) {
75 6         15 my $klass = "$namespace\::$lang";
76              
77 6 50       15 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         8 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   13 no strict 'refs';
  2         3  
  2         690  
91 6         9 my $first_load = 0;
92 6 50       12 if (! $LOADED->{$klass}) {
93 6 100 66     8 if (%{"$klass\::Lexicon"} && %{"$klass\::"}) {
  6         41  
  4         19  
94 4         5 if (Data::Localize::DEBUG) {
95             debugf("get_lexicon - class %s already loaded", $klass);
96             }
97             } else {
98 2         3 if (Data::Localize::DEBUG) {
99             debugf("get_lexicon - loading %s", $klass);
100             }
101              
102 2         6 my $code =
103             "\n" .
104             "#line " . __LINE__ . ' "' . __FILE__ . '"' . "\n" .
105             "require $klass;"
106             ;
107 2         60 eval($code);
108 2 50       104 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         9 if (Data::Localize::DEBUG) {
117             debugf("get_lexicon - setting %s to already loaded", $klass);
118             }
119 6         35 $LOADED->{$klass}++;
120 6         15 $first_load = 1;
121             }
122              
123 6         9 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         21 my $h = \%{ "$klass\::Lexicon" };
  6         21  
130 6 50       15 if ($first_load) {
131 6         9 my %t;
132 6         25 while (my($k, $v) = each %$h) {
133 6 100       23 if ( ! Encode::is_utf8($k) ) {
134 3         13 $k = Encode::decode_utf8($k);
135             }
136 6 100       141 if ( ! Encode::is_utf8($v) ) {
137 2         5 $v = Encode::decode_utf8($v);
138             }
139 6         48 $t{$k} = $v;
140             }
141 6         12 %$h = ();
142 6         21 %$h = %t;
143             }
144 6         25 return $h->{$id};
145            
146             }
147 0           return ();
148             }
149              
150             1;
151              
152             __END__