File Coverage

blib/lib/Data/Localize/Gettext.pm
Criterion Covered Total %
statement 52 52 100.0
branch 3 6 50.0
condition 1 2 50.0
subroutine 15 15 100.0
pod 3 4 75.0
total 74 79 93.6


line stmt bran cond sub pod time code
1             package Data::Localize::Gettext;
2 1     1   1159 use utf8;
  1         2  
  1         5  
3 1     1   24 use Moo;
  1         2  
  1         4  
4 1     1   285 use Module::Load ();
  1         2  
  1         12  
5 1     1   4 use Carp ();
  1         1  
  1         17  
6 1     1   383 use Data::Localize::Gettext::Parser;
  1         2  
  1         40  
7 1     1   6 use File::Temp ();
  1         1  
  1         19  
8 1     1   4 use Data::Localize;
  1         1  
  1         17  
9 1     1   364 use Data::Localize::Storage::Hash;
  1         2  
  1         112  
10              
11             BEGIN {
12 1     1   635 if (Data::Localize::DEBUG) {
13             require Data::Localize::Log;
14             Data::Localize::Log->import;
15             }
16             }
17              
18             extends 'Data::Localize::Localizer';
19             with 'Data::Localize::Trait::WithStorage';
20              
21             has encoding => (
22             is => 'ro',
23             default => sub { 'utf-8' },
24             );
25              
26             has paths => (
27             is => 'ro',
28             trigger => sub {
29             my $self = shift;
30             if ($self->initialized) {
31             $self->load_from_path($_) for @{$_[0]};
32             }
33             },
34             );
35              
36             has use_fuzzy => (
37             is => 'ro',
38             default => sub { 0 },
39             );
40              
41             has keep_empty => (
42             is => 'ro',
43             default => sub { 0 },
44             );
45              
46             has _parser => (
47             is => 'rw',
48             lazy => 1,
49             builder => "_build__parser",
50             isa => sub { $_[0]->isa('Data::Localize::Gettext::Parser') },
51             );
52              
53             around register => sub {
54             my ($next, $self, $loc) = @_;
55             $self->$next($loc);
56             $loc->add_localizer_map('*', $self);
57             };
58              
59             sub _build__parser {
60 3     3   520 my $self = shift;
61 3         67 return Data::Localize::Gettext::Parser->new(
62             use_fuzzy => $self->use_fuzzy(),
63             keep_empty => $self->keep_empty(),
64             encoding => $self->encoding(),
65             );
66             }
67              
68             after BUILD => sub {
69             my $self = shift;
70             my $paths = $self->paths;
71             foreach my $path (@$paths) {
72             $self->load_from_path($path);
73             }
74             };
75              
76             sub BUILDARGS {
77 3     3 0 4168 my ($class, %args) = @_;
78              
79 3         8 my $path = delete $args{path};
80 3 50       8 if ($path) {
81 3   50     22 $args{paths} ||= [];
82 3         2 push @{$args{paths}}, $path;
  3         8  
83             }
84 3         22 $class->SUPER::BUILDARGS(%args);
85             }
86              
87             sub _build_formatter {
88 2     2   802 Module::Load::load( 'Data::Localize::Format::Gettext' );
89 2         71 return Data::Localize::Format::Gettext->new();
90             }
91              
92             sub add_path {
93 1     1 1 9 my $self = shift;
94 1         1 push @{$self->paths}, @_;
  1         3  
95 1         4 $self->load_from_path($_) for @_;
96             }
97              
98             sub load_from_path {
99 4     4 1 7 my ($self, $path) = @_;
100              
101 4 50       9 return unless $path;
102              
103 4         3 if (Data::Localize::DEBUG) {
104             debugf("load_from_path - loading from glob(%s)", $path);
105             }
106              
107 4         208 foreach my $x (glob($path)) {
108 4 50       43 $self->load_from_file($x) if -f $x;
109             }
110             }
111              
112             sub load_from_file {
113 4     4 1 7 my ($self, $file) = @_;
114              
115 4         4 if (Data::Localize::DEBUG) {
116             debugf("load_from_file - loading from file %s", $file);
117             }
118              
119 4         76 my $lexicon = $self->_parser->parse_file($file);
120              
121 4         136 my $lang = File::Basename::basename($file);
122 4         14 $lang =~ s/\.[mp]o$//;
123              
124 4         3 if (Data::Localize::DEBUG) {
125             debugf("load_from_file - registering %d keys", scalar keys %{$lexicon});
126             }
127              
128             # This needs to be merged
129 4         14 $self->merge_lexicon($lang, $lexicon);
130             }
131              
132             1;
133              
134             __END__