line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Locale::MakePhrase::BackingStore; |
2
|
|
|
|
|
|
|
our $VERSION = 0.2; |
3
|
|
|
|
|
|
|
our $DEBUG = 0; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Locale::MakePhrase::BackingStore - base-class of the backing store |
8
|
|
|
|
|
|
|
functionality. |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 DESCRIPTION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
This is a base-class, of storage-specific implementations for the |
13
|
|
|
|
|
|
|
L module. |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
The backing-store may choose to implement seperate files for each |
16
|
|
|
|
|
|
|
language, or a single file for all languages. It may choose to |
17
|
|
|
|
|
|
|
implement database lookup... and so on. |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
This base class implements a generic implementation, which can be used |
20
|
|
|
|
|
|
|
as a starting point. You should also look at Locale::MakePhrase::BackingStore::Esome moduleE |
21
|
|
|
|
|
|
|
for more examples. |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
L implements the following backing stores: |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=over 2 |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=item * |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Single file for all languages (see backing store: |
30
|
|
|
|
|
|
|
L) |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=item * |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
Files stored within a directory (see backing store: |
35
|
|
|
|
|
|
|
L) |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=item * |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
Generic database table (see backing store: |
40
|
|
|
|
|
|
|
L) |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=item * |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
PostgreSQL database table (see backing store: |
45
|
|
|
|
|
|
|
L) |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=back |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Alternatively, you could implement an application specific backing |
50
|
|
|
|
|
|
|
store by doing the following: |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=over 3 |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=item 1. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Make a package which derives from this class. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=item 2. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Implement the init() method, retrieving any options that may have |
61
|
|
|
|
|
|
|
been supplied to the constructor. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item 3. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Overload the get_rules() method, returning a list-reference of |
66
|
|
|
|
|
|
|
L objects, from the translations |
67
|
|
|
|
|
|
|
available from your backing store. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=back |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
For an implementation which uses a text file, this could mean that you |
72
|
|
|
|
|
|
|
would load the text file if it has changed, constructing the rule |
73
|
|
|
|
|
|
|
objects during the load, then return a list-reference of objects which |
74
|
|
|
|
|
|
|
match the request. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
For a database implementation, you would need to query the database |
77
|
|
|
|
|
|
|
for translations which match the request, then construct rule objects |
78
|
|
|
|
|
|
|
from those translations. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 API |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
The following methods are implemented: |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=cut |
85
|
|
|
|
|
|
|
|
86
|
9
|
|
|
9
|
|
1497
|
{ no warnings; require v5.8.0; } |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
415
|
|
87
|
9
|
|
|
9
|
|
45
|
use strict; |
|
9
|
|
|
|
|
29
|
|
|
9
|
|
|
|
|
261
|
|
88
|
9
|
|
|
9
|
|
42
|
use warnings; |
|
9
|
|
|
|
|
15
|
|
|
9
|
|
|
|
|
204
|
|
89
|
9
|
|
|
9
|
|
49
|
use utf8; |
|
9
|
|
|
|
|
13
|
|
|
9
|
|
|
|
|
64
|
|
90
|
9
|
|
|
9
|
|
216
|
use base qw(); |
|
9
|
|
|
|
|
16
|
|
|
9
|
|
|
|
|
169
|
|
91
|
9
|
|
|
9
|
|
4141
|
use Locale::MakePhrase::LanguageRule; |
|
9
|
|
|
|
|
20
|
|
|
9
|
|
|
|
|
277
|
|
92
|
9
|
|
|
9
|
|
57
|
use Locale::MakePhrase::Utils qw(die_from_caller); |
|
9
|
|
|
|
|
13
|
|
|
9
|
|
|
|
|
4609
|
|
93
|
|
|
|
|
|
|
local $Data::Dumper::Indent = 1 if $DEBUG; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
#-------------------------------------------------------------------------- |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head2 new() |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Construct a backing store instance; arguments are passed to the init() |
100
|
|
|
|
|
|
|
method. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=cut |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub new { |
105
|
1
|
|
|
1
|
1
|
2
|
my $proto = shift; |
106
|
1
|
|
33
|
|
|
7
|
my $class = ref($proto) || $proto; |
107
|
1
|
|
|
|
|
3
|
my $self = bless {}, $class; |
108
|
1
|
|
|
|
|
5
|
return $self->init(@_); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
#-------------------------------------------------------------------------- |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 $self init([...]) |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Allow sub-class to control construction. The sub-class must return |
116
|
|
|
|
|
|
|
itself, so as to make construction succeed. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=cut |
119
|
|
|
|
|
|
|
|
120
|
7
|
|
|
7
|
1
|
35
|
sub init { shift } |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
#-------------------------------------------------------------------------- |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 \@rule_objs get_rules($context,$key,\@languages) |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Returns a list-reference of rule objects which have just been |
127
|
|
|
|
|
|
|
retrieved from the storage mechanism. The objects will have been |
128
|
|
|
|
|
|
|
based on the values of the $context (which is a stringified version of |
129
|
|
|
|
|
|
|
whatever get passed to C or a value of undef), the |
130
|
|
|
|
|
|
|
$key (which is your application text string) and the language tags |
131
|
|
|
|
|
|
|
that L determined for this instance. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Since this is a base class, you need to supply a real implementation, |
134
|
|
|
|
|
|
|
although you can still use L with this minimal |
135
|
|
|
|
|
|
|
implementation, so as to allow you to continue application development. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub get_rules { |
140
|
0
|
|
|
0
|
1
|
0
|
my ($self,$context,$key,$languages) = @_; |
141
|
0
|
|
|
|
|
0
|
my $language = shift @{$languages}; |
|
0
|
|
|
|
|
0
|
|
142
|
0
|
|
|
|
|
0
|
my $rule = new Locale::MakePhrase::LanguageRule( |
143
|
|
|
|
|
|
|
language => $language, |
144
|
|
|
|
|
|
|
translation => "~[$language~] -> $key", |
145
|
|
|
|
|
|
|
); |
146
|
0
|
|
|
|
|
0
|
my @translations; |
147
|
0
|
|
|
|
|
0
|
push @translations, $rule; |
148
|
0
|
0
|
|
|
|
0
|
print STDERR "Found translations:\n", Dumper(@translations) if $DEBUG; |
149
|
0
|
|
|
|
|
0
|
return \@translations; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
#-------------------------------------------------------------------------- |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 $rule_obj make_rule() |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
This is a helper routine for making a LanguageRule object. ie: you |
157
|
|
|
|
|
|
|
would use it like this, within your get_rules() method: |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub get_rules { |
160
|
|
|
|
|
|
|
... |
161
|
|
|
|
|
|
|
my $rule_obj = $self->make_rule( |
162
|
|
|
|
|
|
|
key => $key, |
163
|
|
|
|
|
|
|
language => $lang, |
164
|
|
|
|
|
|
|
expression => $expression, |
165
|
|
|
|
|
|
|
priority => $priority, |
166
|
|
|
|
|
|
|
translation => $translation, |
167
|
|
|
|
|
|
|
); |
168
|
|
|
|
|
|
|
... |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Thus, it takes a hash or hash_ref with the options: C, |
172
|
|
|
|
|
|
|
C, C, C and C |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=cut |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub make_rule { |
177
|
53
|
|
|
53
|
1
|
67
|
my $self = shift; |
178
|
53
|
|
|
|
|
60
|
my %args; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# allow multiple forms of argument passing |
181
|
53
|
50
|
33
|
|
|
378
|
if (@_ == 1 and ref($_[0] eq 'HASH')) { |
|
|
50
|
33
|
|
|
|
|
|
|
0
|
|
|
|
|
|
182
|
0
|
|
|
|
|
0
|
%args = %{$_[0]}; |
|
0
|
|
|
|
|
0
|
|
183
|
|
|
|
|
|
|
} elsif (@_ > 1 and not(@_ % 2)) { |
184
|
53
|
|
|
|
|
284
|
%args = @_; |
185
|
|
|
|
|
|
|
} elsif (@_ == 5) { |
186
|
0
|
|
|
|
|
0
|
$args{key} = shift; |
187
|
0
|
|
|
|
|
0
|
$args{language} = shift; |
188
|
0
|
|
|
|
|
0
|
$args{expression} = shift; |
189
|
0
|
|
|
|
|
0
|
$args{priority} = shift; |
190
|
0
|
|
|
|
|
0
|
$args{translation} = shift; |
191
|
|
|
|
|
|
|
} else { |
192
|
0
|
|
|
|
|
0
|
die("Invalid arguments passed to make_rule()"); |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
# Validate arguments |
196
|
53
|
50
|
33
|
|
|
257
|
die_from_caller("Bad rule definition") unless ($args{language} and defined $args{translation}); |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
# make the rule |
199
|
53
|
|
|
|
|
281
|
return new Locale::MakePhrase::LanguageRule(%args); |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
1; |
203
|
|
|
|
|
|
|
__END__ |