| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Template::Multilingual; |
|
2
|
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
33603
|
use strict; |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
113
|
|
|
4
|
3
|
|
|
3
|
|
16
|
use base qw(Template); |
|
|
3
|
|
|
|
|
4
|
|
|
|
3
|
|
|
|
|
3455
|
|
|
5
|
3
|
|
|
3
|
|
139179
|
use Template::Multilingual::Parser; |
|
|
3
|
|
|
|
|
12
|
|
|
|
3
|
|
|
|
|
1234
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '1.00'; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub _init |
|
10
|
|
|
|
|
|
|
{ |
|
11
|
4
|
|
|
4
|
|
3621
|
my ($self, $options) = @_; |
|
12
|
|
|
|
|
|
|
|
|
13
|
4
|
|
|
|
|
27
|
$self->{LANGUAGE_VAR} = $options->{LANGUAGE_VAR}; |
|
14
|
4
|
|
100
|
|
|
35
|
$options->{LANGUAGE_VAR} ||= 'language'; |
|
15
|
4
|
|
|
|
|
29
|
$options->{PARSER} = Template::Multilingual::Parser->new($options); |
|
16
|
4
|
|
|
|
|
13
|
$self->{PARSER} = $options->{PARSER}; |
|
17
|
4
|
|
|
|
|
31
|
$self->SUPER::_init($options) |
|
18
|
|
|
|
|
|
|
} |
|
19
|
|
|
|
|
|
|
sub language |
|
20
|
|
|
|
|
|
|
{ |
|
21
|
29
|
|
|
29
|
1
|
121873
|
my $self = shift; |
|
22
|
29
|
100
|
|
|
|
212
|
@_ ? $self->{language} = shift |
|
23
|
|
|
|
|
|
|
: $self->{language}; |
|
24
|
|
|
|
|
|
|
} |
|
25
|
|
|
|
|
|
|
sub process |
|
26
|
|
|
|
|
|
|
{ |
|
27
|
30
|
|
|
30
|
1
|
28737
|
my ($self, $filename, $vars, @args) = @_; |
|
28
|
30
|
100
|
|
|
|
957
|
unless ($self->{LANGUAGE_VAR}) { |
|
29
|
16
|
|
50
|
|
|
55
|
$vars ||= {}; |
|
30
|
16
|
|
|
|
|
63
|
$vars->{language} = $self->{language} |
|
31
|
|
|
|
|
|
|
} |
|
32
|
30
|
|
|
|
|
323
|
$self->SUPER::process($filename, $vars, @args); |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 NAME |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Template::Multilingual - Multilingual templates for Template Toolkit |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
This subclass of Template Toolkit's C class supports multilingual |
|
42
|
|
|
|
|
|
|
templates: templates that contain text in several languages. |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Hello! |
|
46
|
|
|
|
|
|
|
Bonjour ! |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Specify the language to use when processing a template: |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
use Template::Multilingual; |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $template = Template::Multilingual->new(); |
|
54
|
|
|
|
|
|
|
$template->language('en'); |
|
55
|
|
|
|
|
|
|
$template->process('example.ttml'); |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
You can also provide the name of the template variable that will |
|
58
|
|
|
|
|
|
|
hold the language: |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $template = Template::Multilingual->new(LANGUAGE_VAR => 'foo'); |
|
61
|
|
|
|
|
|
|
$template->process('example.ttml', { foo => 'en' }); |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 METHODS |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head2 new(\%params) |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
The new() constructor creates and returns a reference to a new |
|
68
|
|
|
|
|
|
|
template object. A reference to a hash may be supplied as a |
|
69
|
|
|
|
|
|
|
parameter to provide configuration values. |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Configuration values are all valid C superclass options, |
|
72
|
|
|
|
|
|
|
and one specific to this class: |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=over |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=item LANGUAGE_VAR |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
The LANGUAGE_VAR option can be used to set the name of the template |
|
79
|
|
|
|
|
|
|
variable which contains the current language. |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
my $parser = Template::Multilingual->new({ |
|
82
|
|
|
|
|
|
|
LANGUAGE_VAR => 'global.language', |
|
83
|
|
|
|
|
|
|
}); |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
If this option is set, your code is responsible for setting the |
|
86
|
|
|
|
|
|
|
variable's value to the current language when processing the |
|
87
|
|
|
|
|
|
|
template. Calling C will have no effect. |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
If this option is not set, it defaults to I. |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=back |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 language($lcode) |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Specify the language to be used when processing the template. Any string that |
|
96
|
|
|
|
|
|
|
matches C<\w+> is fine, but we suggest sticking to ISO-639 which provides |
|
97
|
|
|
|
|
|
|
2-letter codes for common languages and 3-letter codes for many others. |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 process |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Used exactly as the original Template Toolkit C method. |
|
102
|
|
|
|
|
|
|
Be sure to call C before calling C. |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 LANGUAGE SUBTAG HANDLING |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
This module supports language subtags to express variants, e.g. "en_US" or "en-US". |
|
107
|
|
|
|
|
|
|
Here are the rules used for language matching: |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=over |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=item * |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Exact match: the current language is found in the template |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
language template output |
|
116
|
|
|
|
|
|
|
fr foobar foo |
|
117
|
|
|
|
|
|
|
fr_CA foobar bar |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=item * |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Fallback to the primary language |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
language template output |
|
124
|
|
|
|
|
|
|
fr_CA foobar foo |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=item * |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Fallback to first (in alphabetical order) other variant of the primary language |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
language template output |
|
131
|
|
|
|
|
|
|
fr foobar bar |
|
132
|
|
|
|
|
|
|
fr_CA foobar bar |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=back |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 AUTHOR |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Eric Cholet, C<< >> |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 BUGS |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Multilingual text sections cannot be used inside TT directives. |
|
143
|
|
|
|
|
|
|
The following is illegal and will trigger a TT syntax error: |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
[% title = "BonjourHello" %] |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Use this instead: |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
[% title = BLOCK %]BonjourHello[% END %] |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
The TAG_STYLE, START_TAG and END_TAG directives are supported, but the |
|
153
|
|
|
|
|
|
|
TAGS directive is not. |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
|
156
|
|
|
|
|
|
|
C, or through the web interface at |
|
157
|
|
|
|
|
|
|
L. |
|
158
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
|
159
|
|
|
|
|
|
|
your bug as I make changes. |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
If you are already using your own C subclass, you may find it |
|
164
|
|
|
|
|
|
|
easier to use L instead. |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
ISO 639-2 Codes for the Representation of Names of Languages: |
|
167
|
|
|
|
|
|
|
http://www.loc.gov/standards/iso639-2/langcodes.html |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Copyright 2009 Eric Cholet, All Rights Reserved. |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
174
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
1; # End of Template::Multilingual |