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