line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer2::Plugin::Locale::Wolowitz; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
441310
|
use 5.010; |
|
2
|
|
|
|
|
5
|
|
4
|
2
|
|
|
2
|
|
7
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
29
|
|
5
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
40
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
376
|
use Dancer2::FileUtils; |
|
2
|
|
|
|
|
519
|
|
|
2
|
|
|
|
|
56
|
|
8
|
2
|
|
|
2
|
|
857
|
use Dancer2::Plugin; |
|
2
|
|
|
|
|
136712
|
|
|
2
|
|
|
|
|
11
|
|
9
|
2
|
|
|
2
|
|
21671
|
use I18N::AcceptLanguage; |
|
2
|
|
|
|
|
1926
|
|
|
2
|
|
|
|
|
48
|
|
10
|
2
|
|
|
2
|
|
745
|
use Locale::Wolowitz; |
|
2
|
|
|
|
|
7864
|
|
|
2
|
|
|
|
|
670
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $wolowitz; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Dancer2::Plugin::Locale::Wolowitz - Dancer2's plugin for Locale::Wolowitz |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 DESCRIPTION |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
This plugin give you the L support. It's a blatant copy of |
23
|
|
|
|
|
|
|
L and should be a drop in replacement |
24
|
|
|
|
|
|
|
for Dancer2 projects. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSIS |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
use Dancer2; |
29
|
|
|
|
|
|
|
use Dancer2::Plugin::Locale::Wolowitz; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# in your templates |
32
|
|
|
|
|
|
|
get '/' => sub { |
33
|
|
|
|
|
|
|
template 'index'; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# or directly in code |
37
|
|
|
|
|
|
|
get '/logout' => sub { |
38
|
|
|
|
|
|
|
template 'logout', { |
39
|
|
|
|
|
|
|
bye => loc('Bye'); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
... meanwhile, in a nearby template file called index.tt |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
<% l('Welcome') %> |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 CONFIGURATION |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
plugins: |
50
|
|
|
|
|
|
|
Locale::Wolowitz: |
51
|
|
|
|
|
|
|
fallback: "en" |
52
|
|
|
|
|
|
|
locale_path_directory: "i18n" |
53
|
|
|
|
|
|
|
lang_session: "lang" |
54
|
|
|
|
|
|
|
lang_available: |
55
|
|
|
|
|
|
|
- de |
56
|
|
|
|
|
|
|
- en |
57
|
|
|
|
|
|
|
- id |
58
|
|
|
|
|
|
|
- nl |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
on_plugin_import { |
64
|
|
|
|
|
|
|
my $dsl = shift; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
$dsl->app->add_hook( |
67
|
|
|
|
|
|
|
Dancer2::Core::Hook->new( |
68
|
|
|
|
|
|
|
name => 'before_template_render', |
69
|
|
|
|
|
|
|
code => sub { |
70
|
|
|
|
|
|
|
my $tokens = shift; |
71
|
|
|
|
|
|
|
$tokens->{l} = sub { _loc($dsl, @_); }; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
) |
74
|
|
|
|
|
|
|
); |
75
|
|
|
|
|
|
|
}; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 KEYWORDS |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head2 loc |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
The C keyword can be used in code to look up the correct translation. In |
82
|
|
|
|
|
|
|
templates you can use the C function |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=cut |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
register loc => \&_loc; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub _loc { |
89
|
16
|
|
|
16
|
|
166128
|
my ($dsl, $str, $args, $force_lang) = @_; |
90
|
|
|
|
|
|
|
|
91
|
16
|
|
66
|
|
|
47
|
$wolowitz ||= Locale::Wolowitz->new(_path_directory_locale($dsl)); |
92
|
16
|
|
66
|
|
|
336
|
my $lang = $force_lang || _lang($dsl); |
93
|
|
|
|
|
|
|
|
94
|
16
|
|
|
|
|
271
|
return $wolowitz->loc($str, $lang, @$args); |
95
|
|
|
|
|
|
|
}; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub _path_directory_locale { |
98
|
1
|
|
|
1
|
|
2
|
my $dsl = shift; |
99
|
|
|
|
|
|
|
|
100
|
1
|
|
|
|
|
4
|
my $conf = $dsl->{app}{config}{plugins}{'Locale::Wolowitz'}; |
101
|
|
|
|
|
|
|
|
102
|
1
|
|
50
|
|
|
7
|
my $dir = $conf->{locale_path_directory} // 'i18n'; |
103
|
1
|
50
|
|
|
|
34
|
unless (-d $dir) { |
104
|
1
|
|
|
|
|
17
|
$dir = Dancer2::FileUtils::path($dsl->app->setting('appdir'), $dir); |
105
|
|
|
|
|
|
|
} |
106
|
1
|
|
|
|
|
120
|
return $dir; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub _lang { |
110
|
14
|
|
|
14
|
|
17
|
my $dsl = shift; |
111
|
|
|
|
|
|
|
|
112
|
14
|
|
|
|
|
34
|
my $conf = $dsl->{app}{config}{plugins}{'Locale::Wolowitz'}; |
113
|
14
|
|
50
|
|
|
52
|
my $lang_session = $conf->{lang_session} || 'lang'; |
114
|
|
|
|
|
|
|
|
115
|
14
|
100
|
|
|
|
49
|
if( $dsl->app->has_session ) { |
116
|
12
|
|
|
|
|
1711
|
my $session_language = $dsl->app->session->read( $lang_session ); |
117
|
|
|
|
|
|
|
|
118
|
12
|
50
|
|
|
|
1439
|
if( !$session_language ) { |
119
|
0
|
|
|
|
|
0
|
$session_language = _detect_lang_from_browser($dsl); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
12
|
|
|
|
|
33
|
return $session_language; |
123
|
|
|
|
|
|
|
} else { |
124
|
2
|
|
|
|
|
82
|
return _detect_lang_from_browser($dsl); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub _detect_lang_from_browser { |
129
|
2
|
|
|
2
|
|
2
|
my $dsl = shift; |
130
|
|
|
|
|
|
|
|
131
|
2
|
|
|
|
|
5
|
my $conf = $dsl->{app}{config}{plugins}{'Locale::Wolowitz'}; |
132
|
2
|
|
50
|
|
|
16
|
my $acceptor = I18N::AcceptLanguage->new(defaultLanguage => $conf->{fallback} // ""); |
133
|
2
|
|
|
|
|
91
|
return $acceptor->accepts($dsl->app->request->accept_language, $conf->{lang_available}); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 AUTHOR |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Menno Blom, C<< >> |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 BUGS / CONTRIBUTING |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
This module is developed on Github at: |
143
|
|
|
|
|
|
|
L |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Many thanks go out to L for |
148
|
|
|
|
|
|
|
writing the Dancer 1 version of this plugin (L). |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
And obviously thanks to L for |
151
|
|
|
|
|
|
|
creating the main code we're using in this plugin! (L). |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 COPYRIGHT |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Copyright 2014- Menno Blom |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 LICENSE |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
160
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
register_plugin; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
1; # End of Dancer2::Plugin::Locale::Wolowitz |