line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CGI::Application::Plugin::I18N;
|
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
=head1 NAME
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
CGI::Application::Plugin::I18N - I18N and L10N methods for CGI::App
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Nothing is exported by default. You can specify a list of individual methods or
|
11
|
|
|
|
|
|
|
use one of the groups :std, :max or :min.
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use CGI::Application::Plugin::I18N qw( :std );
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Within your setup, cgiapp_init, cgiapp_prerun or specific runmode routine add
|
16
|
|
|
|
|
|
|
the line
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$self->i18n_config();
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Or
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
$self->i18n_config( %options );
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
%options are the same as for Locale::Maketext::Simple. If none are passed the
|
25
|
|
|
|
|
|
|
following default are used:-
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
%DEFAULT_OPTIONS = (
|
28
|
|
|
|
|
|
|
Path => "$RealBin/I18N",
|
29
|
|
|
|
|
|
|
Style => 'gettext',
|
30
|
|
|
|
|
|
|
Export => '_maketext',
|
31
|
|
|
|
|
|
|
Decode => 1,
|
32
|
|
|
|
|
|
|
Encoding => '',
|
33
|
|
|
|
|
|
|
);
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$RealBin being the folder from which the executed cgi script is running.
|
36
|
|
|
|
|
|
|
B
|
37
|
|
|
|
|
|
|
properly!>
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
For instance if you wanted to use maketext style markup in your lexicons you
|
40
|
|
|
|
|
|
|
would use the line:-
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$self->i18n_config( Style => 'maketext' );
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Then use the I method to localize text:-
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
print $self->localtext( 'Hello World!' );
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
This module is a wrapper around C by Audrey Tang.
|
51
|
|
|
|
|
|
|
It extends the C object with variour methods to control the
|
52
|
|
|
|
|
|
|
localization of text. A L is provided with the aim to fill in the gaps.
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 Methods
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head2 i18n_config
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
Runs the initial configuration of C and runs it's
|
59
|
|
|
|
|
|
|
import within your calling objects namespace (Your CGI::App class)
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head2 localtext_langs
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Sets the current language for localtext output. Usage:-
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
$self->localtext_langs( LIST );
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
LIST must consist of valid language tags as defined in RFC3066. See
|
68
|
|
|
|
|
|
|
C for more details.
|
69
|
|
|
|
|
|
|
If LIST is ommited then the method will attempt to figure out the users locale
|
70
|
|
|
|
|
|
|
using C.
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
This method will also return the list of language tags as an array reference.
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $langtags = $self->localtext_langs( LIST );
|
75
|
|
|
|
|
|
|
print @$langtags;
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head2 localtext_lang
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
This method returns the currently selected language. This is the tag that was
|
80
|
|
|
|
|
|
|
actually available for use, after searching through the localtext_langs list.
|
81
|
|
|
|
|
|
|
This is the name of the module used in your MyAPP::I18N::XXX namespace (where
|
82
|
|
|
|
|
|
|
XXX is the name of the lexicon used)
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my $lexicon = $self->localtext_lang;
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 localtext_lang_tag
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
This method returns the RFC3066 language tag for the currently selected
|
89
|
|
|
|
|
|
|
language. This differs from the above method which would most likely return
|
90
|
|
|
|
|
|
|
I for American English, whereas this method would return I.
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
my $langtag = $self->localtext_lang_tag;
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 localtext
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
This is the method that actually does the work.
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
print $self->localtext( 'Hello World!' );
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 loc
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
You can choose to import a shorter method called C that works the same way
|
103
|
|
|
|
|
|
|
as C. You need to specify this when you use the module:-
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
use CGI::Application::Plugin::I18N qw( loc );
|
106
|
|
|
|
|
|
|
print $self->loc( 'Hello World!' );
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head2 Export groups
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
:max exports:-
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
i18n_config localtext_langs localtext_lang localtext_lang_tag localtext loc
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
:std exports:-
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
i18n_config localtext_langs localtext_lang localtext_lang_tag localtext
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
:min exports:-
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
i18n_config localtext
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 FAQ
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 How does it all work?
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
I kept a blog on how I put this module together and all the material I looked
|
127
|
|
|
|
|
|
|
through in order to understand internationalization.
|
128
|
|
|
|
|
|
|
L
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 What is a Lexicon?
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Think of it as a kind of hash. Where the text you use (usually english) has a
|
133
|
|
|
|
|
|
|
corrosponding value in the local language. So the 'Hello world' under a German
|
134
|
|
|
|
|
|
|
lexicon would have the value 'Hallo welt'.
|
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 Is there some sort of guide?
|
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Yes I've written one. L
|
139
|
|
|
|
|
|
|
See Guide.pod which is part of this distribution. It'll walk you through what
|
140
|
|
|
|
|
|
|
you need to know, and how to make your lexicons.
|
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 Thanks to:-
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
L - The module this one was heavily based on
|
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
L - Making it possible
|
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
L - Doing all the hard work
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
L - Providing the framework
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
And all others I haven't yet mentioned.
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 Come join the bestest Perl group in the World!
|
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Bristol and Bath Perl moungers is renound for being the friendliest Perl group
|
157
|
|
|
|
|
|
|
in the world. You don't have to be from the UK to join, everyone is welcome on
|
158
|
|
|
|
|
|
|
the list:-
|
159
|
|
|
|
|
|
|
L
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 AUTHOR
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Lyle Hopkins ;)
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=cut
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
169
|
1
|
|
|
1
|
|
21755
|
use strict;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
170
|
1
|
|
|
1
|
|
6
|
use warnings;
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
171
|
1
|
|
|
1
|
|
5
|
use Carp;
|
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
91
|
|
172
|
|
|
|
|
|
|
|
173
|
1
|
|
|
1
|
|
935
|
use FindBin qw($RealBin);
|
|
1
|
|
|
|
|
1029
|
|
|
1
|
|
|
|
|
102
|
|
174
|
|
|
|
|
|
|
|
175
|
1
|
|
|
1
|
|
880
|
use I18N::LangTags ();
|
|
1
|
|
|
|
|
3807
|
|
|
1
|
|
|
|
|
29
|
|
176
|
1
|
|
|
1
|
|
995
|
use I18N::LangTags::Detect;
|
|
1
|
|
|
|
|
2343
|
|
|
1
|
|
|
|
|
50
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
require Locale::Maketext::Simple;
|
179
|
|
|
|
|
|
|
|
180
|
1
|
|
|
1
|
|
8
|
use vars qw ( $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $RealBin %DEFAULT_OPTIONS );
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
464
|
|
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
require Exporter;
|
183
|
|
|
|
|
|
|
@ISA = qw(Exporter);
|
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
@EXPORT = ();
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
@EXPORT_OK = qw(
|
188
|
|
|
|
|
|
|
i18n_config
|
189
|
|
|
|
|
|
|
localtext_langs
|
190
|
|
|
|
|
|
|
localtext_lang
|
191
|
|
|
|
|
|
|
localtext_lang_tag
|
192
|
|
|
|
|
|
|
localtext
|
193
|
|
|
|
|
|
|
loc
|
194
|
|
|
|
|
|
|
);
|
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
%EXPORT_TAGS = (
|
197
|
|
|
|
|
|
|
all => [ qw(i18n_config localtext_langs localtext_lang localtext_lang_tag localtext loc) ],
|
198
|
|
|
|
|
|
|
std => [ qw(i18n_config localtext_langs localtext_lang localtext_lang_tag localtext) ],
|
199
|
|
|
|
|
|
|
min => [ qw(i18n_config localtext) ],
|
200
|
|
|
|
|
|
|
);
|
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
$VERSION = '0.03';
|
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
%DEFAULT_OPTIONS = (
|
205
|
|
|
|
|
|
|
Path => "$RealBin/I18N",
|
206
|
|
|
|
|
|
|
Style => 'gettext',
|
207
|
|
|
|
|
|
|
Export => '_maketext',
|
208
|
|
|
|
|
|
|
Decode => 1,
|
209
|
|
|
|
|
|
|
Encoding => '', # When set to locale, .po and .mo files that include a Content-Type cause the software to die?
|
210
|
|
|
|
|
|
|
);
|
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
sub i18n_config {
|
214
|
|
|
|
|
|
|
|
215
|
0
|
|
|
0
|
1
|
|
my $self = shift;
|
216
|
0
|
|
0
|
|
|
|
my $class = ref $self || $self;
|
217
|
|
|
|
|
|
|
|
218
|
0
|
|
|
|
|
|
local %DEFAULT_OPTIONS = %DEFAULT_OPTIONS;
|
219
|
0
|
0
|
|
|
|
|
if ( @_ ) {
|
220
|
0
|
|
|
|
|
|
my %newoptions = @_;
|
221
|
0
|
|
|
|
|
|
my @valid_options = ("Class","Style","Export","Subclass","Decode","Encoding","Path");
|
222
|
0
|
|
|
|
|
|
foreach my $key (keys %newoptions) {
|
223
|
0
|
0
|
|
|
|
|
unless (grep (/^$key$/, @valid_options)) {
|
224
|
0
|
|
|
|
|
|
croak( "Invalid option: $key" );
|
225
|
|
|
|
|
|
|
}#unless
|
226
|
|
|
|
|
|
|
}#foreach
|
227
|
0
|
|
|
|
|
|
%DEFAULT_OPTIONS = (%DEFAULT_OPTIONS, %newoptions);
|
228
|
|
|
|
|
|
|
}#if
|
229
|
|
|
|
|
|
|
|
230
|
0
|
|
|
|
|
|
my $evalcode = qq~
|
231
|
|
|
|
|
|
|
package $class;
|
232
|
|
|
|
|
|
|
Locale::Maketext::Simple->import( \%CGI\::Application\::Plugin\::I18N\::DEFAULT_OPTIONS );
|
233
|
|
|
|
|
|
|
~;
|
234
|
0
|
|
|
|
|
|
eval $evalcode;
|
235
|
|
|
|
|
|
|
|
236
|
0
|
0
|
|
|
|
|
if ( $@ ) {
|
237
|
0
|
|
|
|
|
|
croak( qq~Couldn't initialize i18n, error "$@", code "$evalcode"~ );
|
238
|
|
|
|
|
|
|
}#if
|
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
}#sub
|
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
sub localtext_langs {
|
244
|
0
|
|
|
0
|
1
|
|
my $self = shift;
|
245
|
0
|
|
|
|
|
|
my @langs = @_;
|
246
|
0
|
0
|
|
|
|
|
if (@langs) {
|
247
|
0
|
|
|
|
|
|
$self->{__I18N_LANGS} = \@langs;
|
248
|
|
|
|
|
|
|
}#if
|
249
|
|
|
|
|
|
|
else {
|
250
|
|
|
|
|
|
|
### Get CGI query object
|
251
|
0
|
|
|
|
|
|
my $q = $self->query();
|
252
|
0
|
0
|
|
|
|
|
$self->{__I18N_LANGS} = [
|
253
|
|
|
|
|
|
|
I18N::LangTags::implicate_supers(
|
254
|
|
|
|
|
|
|
I18N::LangTags::Detect->http_accept_langs(
|
255
|
|
|
|
|
|
|
$q->http('Accept-Language')
|
256
|
|
|
|
|
|
|
)
|
257
|
|
|
|
|
|
|
),
|
258
|
|
|
|
|
|
|
'i-default'
|
259
|
|
|
|
|
|
|
] unless $self->{__I18N_LANGS};
|
260
|
|
|
|
|
|
|
}#else
|
261
|
1
|
|
|
1
|
|
6
|
no strict 'refs';
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
381
|
|
262
|
0
|
|
|
|
|
|
&{ ref($self) . '::_maketext_lang' }( @{ $self->{__I18N_LANGS} } );
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
263
|
0
|
|
|
|
|
|
return $self->{__I18N_LANGS};
|
264
|
|
|
|
|
|
|
}#sub
|
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
sub localtext_lang {
|
268
|
0
|
|
|
0
|
1
|
|
my $self = shift;
|
269
|
0
|
|
0
|
|
|
|
my $class = ref $self || $self;
|
270
|
|
|
|
|
|
|
|
271
|
0
|
|
|
|
|
|
my $lang = ref "$class\::I18N"->get_handle( @{ $self->localtext_langs } );
|
|
0
|
|
|
|
|
|
|
272
|
0
|
|
|
|
|
|
$lang =~ s/.*:://;
|
273
|
|
|
|
|
|
|
|
274
|
0
|
|
|
|
|
|
return $lang;
|
275
|
|
|
|
|
|
|
}#sub
|
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
sub localtext_lang_tag {
|
279
|
0
|
|
|
0
|
1
|
|
my $self = shift;
|
280
|
0
|
|
0
|
|
|
|
my $class = ref $self || $self;
|
281
|
|
|
|
|
|
|
|
282
|
0
|
|
|
|
|
|
return "$class\::I18N"->get_handle( @{ $self->localtext_langs } )->language_tag;
|
|
0
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
}#sub
|
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
sub localtext {
|
287
|
0
|
|
|
0
|
1
|
|
my $self = shift;
|
288
|
0
|
0
|
|
|
|
|
$self->localtext_langs unless $self->{__I18N_LANGS};
|
289
|
1
|
|
|
1
|
|
5
|
no strict 'refs';
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
121
|
|
290
|
0
|
0
|
|
|
|
|
return &{ ref($self) . '::_maketext' }( $_[0], @{ $_[1] } ) if ( ref $_[1] eq 'ARRAY' );
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
291
|
0
|
|
|
|
|
|
return &{ ref($self) . '::_maketext' }(@_);
|
|
0
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
}#sub
|
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
sub loc {
|
295
|
0
|
|
|
0
|
1
|
|
&localtext;
|
296
|
|
|
|
|
|
|
}#sub
|
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
1;
|