File Coverage

blib/lib/Locale/MakePhrase/LanguageRule.pm
Criterion Covered Total %
statement 41 50 82.0
branch 11 18 61.1
condition 3 9 33.3
subroutine 11 12 91.6
pod 6 6 100.0
total 72 95 75.7


line stmt bran cond sub pod time code
1             package Locale::MakePhrase::LanguageRule;
2             our $VERSION = 0.2;
3             our $DEBUG = 0;
4              
5             =head1 NAME
6              
7             Locale::MakePhrase::LanguageRule - rule instance for a given translation.
8              
9             =head1 DESCRIPTION
10              
11             This is a container for the currently translated phrase. Its main
12             purpose is to validate the translation rule so that L
13             doesn't need to do it.
14              
15             When implementing custom backing stores, you will need to construct
16             these per translation that can be returned.
17              
18             =head1 API
19              
20             The following methods are available:
21              
22             =cut
23              
24 9     9   13873 use strict;
  9         15  
  9         315  
25 9     9   49 use warnings;
  9         15  
  9         221  
26 9     9   1852 use utf8;
  9         34  
  9         42  
27 9     9   2301 use Data::Dumper;
  9         18033  
  9         511  
28 9     9   230 use base qw();
  9         14  
  9         197  
29 9     9   4623 use Locale::MakePhrase::Utils qw(die_from_caller);
  9         20  
  9         5447  
30             local $Data::Dumper::Indent = 1 if $DEBUG;
31              
32             #--------------------------------------------------------------------------
33              
34             =head2 new()
35              
36             Construct an instance of the language rule. Takes a hash or hashref
37             with the following options:
38              
39             =over 2
40              
41             =item C
42              
43             The input phrase used to generate this translation. (The backing store
44             uses it as a search criteria, hence the name 'key'.)
45              
46             =item C
47              
48             The language tag that is associated with this specific translation.
49              
50             Since L will ask the backing store for all
51             possible translations of a phrase based on the language tags that it
52             resolved during construction, the translation specific language tag
53             is stored with the actual translation.
54              
55             =item C
56              
57             The rule expression that will be evaluated when program arguments are
58             supplied when trying to translate a phrase.
59              
60             =item C
61              
62             When figuring out which rule to apply, L
63             will sort the rules so that the highest priority rules get evaluated
64             first.
65              
66             =item C
67              
68             This the text that will be output; it can contain placeholders for
69             program argument substitution.
70              
71             =back
72              
73             =cut
74              
75             sub new {
76 65     65 1 549 my $proto = shift;
77 65   33     263 my $class = ref($proto) || $proto;
78 65         186 my $self = bless {}, $class;
79              
80             # allow either classic style argument passing, or hash-style argument passing
81 65         89 my %args;
82 65 50 33     462 if (@_ == 1 and ref($_[0]) eq 'HASH') {
    50 33        
    0          
83 0         0 %args = %{$_[0]};
  0         0  
84             } elsif (@_ > 1 and not(@_ % 2)) {
85 65         266 %args = @_;
86             } elsif (@_ == 5) {
87 0         0 $args{key} = shift;
88 0         0 $args{language} = shift;
89 0         0 $args{expression} = shift;
90 0         0 $args{priority} = shift;
91 0         0 $args{translation} = shift;
92             } else {
93 0         0 die_from_caller("Invalid arguments passed to new()");
94             }
95 65 50       137 print STDERR "Arguments to ". $class .": ". Dumper(\%args) if $DEBUG > 5;
96 65         180 $self->{key} = $args{key};
97 65         126 $self->{language} = $args{language};
98 65         132 $self->{expression} = $args{expression};
99 65         101 $self->{priority} = $args{priority};
100 65         107 $self->{translation} = $args{translation};
101              
102             # validate this rule to make sure that it can be used
103 65 100       190 $self->{key} = $self->{translation} unless $self->{key};
104 65 50       148 die_from_caller("Missing language for this rule") unless $self->{language};
105 65 100       162 $self->{expression} = "" unless $self->{expression};
106 65 100       144 $self->{priority} = 0 unless $self->{priority};
107 65 50       135 die_from_caller("Missing translation for this rule") unless (defined $self->{translation});
108              
109             # lc and change - to _
110 65         111 $self->{language} =~ tr<-A-Z><_a-z>;
111              
112 65         327 return $self;
113             }
114              
115             #--------------------------------------------------------------------------
116             # Accessor methods
117              
118             =head2 $string key()
119              
120             Returns the phrase used as the key for translation lookup.
121              
122             =cut
123              
124 0     0 1 0 sub key { shift->{key} }
125              
126             =head2 $string language()
127              
128             Returns the language tag for this translated text.
129              
130             =cut
131              
132 54     54 1 244 sub language { shift->{language} }
133              
134             =head2 $string expression()
135              
136             Returns the expression that will be evaluated for this phrase.
137              
138             =cut
139              
140 50     50 1 158 sub expression { shift->{expression} }
141              
142             =head2 $integer priority()
143              
144             Return the priority of this phrase.
145              
146             =cut
147              
148 10     10 1 34 sub priority { shift->{priority} }
149              
150             =head2 $string translation()
151              
152             Returns the output phrase that matches the input key, for the given
153             language.
154              
155             =cut
156              
157 26     26 1 71 sub translation { shift->{translation} }
158              
159             1;
160             __END__