line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer2::Plugin::Locale::Wolowitz; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
482584
|
use 5.010; |
|
2
|
|
|
|
|
7
|
|
4
|
2
|
|
|
2
|
|
8
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
44
|
|
5
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
47
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
702
|
use Dancer2; |
|
2
|
|
|
|
|
412904
|
|
|
2
|
|
|
|
|
12
|
|
8
|
2
|
|
|
2
|
|
77485
|
use Dancer2::FileUtils; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
91
|
|
9
|
2
|
|
|
2
|
|
1635
|
use Dancer2::Plugin; |
|
2
|
|
|
|
|
4458
|
|
|
2
|
|
|
|
|
20
|
|
10
|
2
|
|
|
2
|
|
3593
|
use I18N::AcceptLanguage; |
|
2
|
|
|
|
|
2256
|
|
|
2
|
|
|
|
|
58
|
|
11
|
2
|
|
|
2
|
|
1471
|
use Locale::Wolowitz; |
|
2
|
|
|
|
|
53453
|
|
|
2
|
|
|
|
|
1333
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $wolowitz; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 NAME |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Dancer2::Plugin::Locale::Wolowitz - Dancer2's plugin for Locale::Wolowitz |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 DESCRIPTION |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
This plugin give you the L support. It's a blatant copy of |
24
|
|
|
|
|
|
|
L and should be a drop in replacement |
25
|
|
|
|
|
|
|
for Dancer2 projects. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 SYNOPSIS |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
use Dancer2; |
30
|
|
|
|
|
|
|
use Dancer2::Plugin::Locale::Wolowitz; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# in your templates |
33
|
|
|
|
|
|
|
get '/' => sub { |
34
|
|
|
|
|
|
|
template 'index'; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# or directly in code |
38
|
|
|
|
|
|
|
get '/logout' => sub { |
39
|
|
|
|
|
|
|
template 'logout', { |
40
|
|
|
|
|
|
|
bye => loc('Bye'); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
... meanwhile, in a nearby template file called index.tt |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
<% l('Welcome') %> |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 CONFIGURATION |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
plugins: |
51
|
|
|
|
|
|
|
Locale::Wolowitz: |
52
|
|
|
|
|
|
|
fallback: "en" |
53
|
|
|
|
|
|
|
locale_path_directory: "i18n" |
54
|
|
|
|
|
|
|
lang_session: "lang" |
55
|
|
|
|
|
|
|
lang_available: |
56
|
|
|
|
|
|
|
- de |
57
|
|
|
|
|
|
|
- en |
58
|
|
|
|
|
|
|
- id |
59
|
|
|
|
|
|
|
- nl |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=cut |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
on_plugin_import { |
65
|
|
|
|
|
|
|
my $dsl = shift; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
$dsl->app->add_hook( |
68
|
|
|
|
|
|
|
Dancer2::Core::Hook->new( |
69
|
|
|
|
|
|
|
name => 'before_template', |
70
|
|
|
|
|
|
|
code => sub { |
71
|
|
|
|
|
|
|
my $tokens = shift; |
72
|
|
|
|
|
|
|
$tokens->{l} = sub { _loc($dsl, @_); }; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
) |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
}; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 KEYWORDS |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 loc |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
The C keyword can be used in code to look up the correct translation. In |
83
|
|
|
|
|
|
|
templates you can use the C function |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=cut |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
register loc => \&_loc; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub _loc { |
90
|
0
|
|
|
0
|
|
|
my ($dsl, $str, $args) = @_; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
0
|
|
|
|
$wolowitz ||= Locale::Wolowitz->new(_path_directory_locale($dsl)); |
93
|
0
|
|
|
|
|
|
my $lang = _lang($dsl); |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
return $wolowitz->loc($str, $lang, @$args); |
96
|
|
|
|
|
|
|
}; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub _path_directory_locale { |
99
|
0
|
|
|
0
|
|
|
my $dsl = shift; |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
my $conf = $dsl->{app}{config}{plugins}{'Locale::Wolowitz'}; |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
0
|
|
|
|
my $dir = $conf->{locale_path_directory} // 'i18n'; |
104
|
0
|
0
|
|
|
|
|
unless (-d $dir) { |
105
|
0
|
|
|
|
|
|
$dir = Dancer2::FileUtils::path($dsl->setting('appdir'), $dir); |
106
|
|
|
|
|
|
|
} |
107
|
0
|
|
|
|
|
|
return $dir; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub _lang { |
111
|
0
|
|
|
0
|
|
|
my $dsl = shift; |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
my $conf = $dsl->{app}{config}{plugins}{'Locale::Wolowitz'}; |
114
|
0
|
|
0
|
|
|
|
my $lang_session = $conf->{lang_session} || 'lang'; |
115
|
|
|
|
|
|
|
|
116
|
0
|
0
|
|
|
|
|
if( $dsl->setting('session') ) { |
117
|
0
|
|
|
|
|
|
my $session_language = $dsl->session( $lang_session ); |
118
|
|
|
|
|
|
|
|
119
|
0
|
0
|
|
|
|
|
if( !$session_language ) { |
120
|
0
|
|
|
|
|
|
$session_language = _detect_lang_from_browser($dsl); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
return $session_language; |
124
|
|
|
|
|
|
|
} else { |
125
|
0
|
|
|
|
|
|
return _detect_lang_from_browser($dsl); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub _detect_lang_from_browser { |
130
|
0
|
|
|
0
|
|
|
my $dsl = shift; |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
my $conf = $dsl->{app}{config}{plugins}{'Locale::Wolowitz'}; |
133
|
0
|
|
0
|
|
|
|
my $acceptor = I18N::AcceptLanguage->new(defaultLanguage => $conf->{fallback} // ""); |
134
|
0
|
|
|
|
|
|
return $acceptor->accepts($dsl->request->accept_language, $conf->{lang_available}); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 AUTHOR |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Menno Blom, C<< >> |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 BUGS / CONTRIBUTING |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
This module is developed on Github at: |
144
|
|
|
|
|
|
|
L |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Many thanks go out to L for |
149
|
|
|
|
|
|
|
writing the Dancer 1 version of this plugin (L). |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
And obviously thanks to L for |
152
|
|
|
|
|
|
|
creating the main code we're using in this plugin! (L). |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Copyright 2014 Menno Blom. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
160
|
|
|
|
|
|
|
under the terms of the the Artistic License (2.0). You may obtain a |
161
|
|
|
|
|
|
|
copy of the full license at: |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
L |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Any use, modification, and distribution of the Standard or Modified |
166
|
|
|
|
|
|
|
Versions is governed by this Artistic License. By using, modifying or |
167
|
|
|
|
|
|
|
distributing the Package, you accept this license. Do not use, modify, |
168
|
|
|
|
|
|
|
or distribute the Package, if you do not accept this license. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
If your Modified Version has been derived from a Modified Version made |
171
|
|
|
|
|
|
|
by someone other than you, you are nevertheless required to ensure that |
172
|
|
|
|
|
|
|
your Modified Version complies with the requirements of this license. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
This license does not grant you the right to use any trademark, service |
175
|
|
|
|
|
|
|
mark, tradename, or logo of the Copyright Holder. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
This license includes the non-exclusive, worldwide, free-of-charge |
178
|
|
|
|
|
|
|
patent license to make, have made, use, offer to sell, sell, import and |
179
|
|
|
|
|
|
|
otherwise transfer the Package with respect to any patent claims |
180
|
|
|
|
|
|
|
licensable by the Copyright Holder that are necessarily infringed by the |
181
|
|
|
|
|
|
|
Package. If you institute patent litigation (including a cross-claim or |
182
|
|
|
|
|
|
|
counterclaim) against any party alleging that the Package constitutes |
183
|
|
|
|
|
|
|
direct or contributory patent infringement, then this Artistic License |
184
|
|
|
|
|
|
|
to you shall terminate on the date that such litigation is filed. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER |
187
|
|
|
|
|
|
|
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. |
188
|
|
|
|
|
|
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
189
|
|
|
|
|
|
|
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY |
190
|
|
|
|
|
|
|
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR |
191
|
|
|
|
|
|
|
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR |
192
|
|
|
|
|
|
|
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, |
193
|
|
|
|
|
|
|
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=cut |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
register_plugin for_versions => [ 2 ] ; |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
1; # End of Dancer2::Plugin::Locale::Wolowitz |