line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of Dancer-Plugin-Locale-Wolowitz |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is copyright (c) 2014 by Natal Ngétal. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This is free software; you can redistribute it and/or modify it under |
7
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
package Dancer::Plugin::Locale::Wolowitz; |
10
|
|
|
|
|
|
|
{ |
11
|
|
|
|
|
|
|
$Dancer::Plugin::Locale::Wolowitz::VERSION = '0.140120'; |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
|
14
|
2
|
|
|
2
|
|
374613
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
73
|
|
15
|
2
|
|
|
2
|
|
13
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
53
|
|
16
|
|
|
|
|
|
|
|
17
|
2
|
|
|
2
|
|
323
|
use 5.010; |
|
2
|
|
|
|
|
13
|
|
|
2
|
|
|
|
|
105
|
|
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
2
|
|
1476
|
use Dancer ':syntax'; |
|
2
|
|
|
|
|
355552
|
|
|
2
|
|
|
|
|
20
|
|
20
|
2
|
|
|
2
|
|
2784
|
use Dancer::Plugin; |
|
2
|
|
|
|
|
3136
|
|
|
2
|
|
|
|
|
192
|
|
21
|
2
|
|
|
2
|
|
17
|
use Dancer::Exception qw(:all); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
262
|
|
22
|
|
|
|
|
|
|
|
23
|
2
|
|
|
2
|
|
1812
|
use Locale::Wolowitz; |
|
2
|
|
|
|
|
34896
|
|
|
2
|
|
|
|
|
1241
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
#ABSTRACT: Intenationalization for Dancer |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $w; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
#Register exception |
30
|
|
|
|
|
|
|
register_exception('DirectoryNotFound', |
31
|
|
|
|
|
|
|
message_pattern => "Not found directory: %s" |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
add_hook( |
36
|
|
|
|
|
|
|
before_template => sub { |
37
|
|
|
|
|
|
|
my $tokens = shift; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
$tokens->{l} = sub { _loc(@_); }; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
register loc => sub { |
44
|
6
|
|
|
6
|
|
19537
|
_loc(@_); |
45
|
|
|
|
|
|
|
}; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _loc { |
48
|
6
|
|
|
6
|
|
15
|
my ( $str, $args ) = @_; |
49
|
|
|
|
|
|
|
|
50
|
6
|
100
|
|
|
|
24
|
$w = Locale::Wolowitz->new(_path_directory_locale()) unless defined($w); |
51
|
6
|
|
|
|
|
321
|
my $lang = _lang(); |
52
|
|
|
|
|
|
|
|
53
|
6
|
100
|
|
|
|
38
|
!$args and return $w->loc($str, $lang); |
54
|
|
|
|
|
|
|
|
55
|
2
|
|
|
|
|
7
|
my $msg = $w->loc($str, $lang, map($w->loc($_, $lang), @{$args})); |
|
2
|
|
|
|
|
12
|
|
56
|
|
|
|
|
|
|
|
57
|
2
|
|
|
|
|
81
|
return $msg; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub _path_directory_locale { |
61
|
1
|
|
|
1
|
|
6
|
my $settings = plugin_setting; |
62
|
1
|
|
33
|
|
|
41
|
my $path = $settings->{locale_path_directory} |
63
|
|
|
|
|
|
|
// Dancer::FileUtils::path(setting('appdir'), 'i18n'); |
64
|
|
|
|
|
|
|
|
65
|
1
|
50
|
|
|
|
154
|
if ( ! -d $path ) { |
66
|
0
|
|
|
|
|
0
|
raise DirectoryNotFound => $path; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
1
|
|
|
|
|
13
|
return $path; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub _lang { |
73
|
6
|
|
|
6
|
|
64
|
my $settings = plugin_setting; |
74
|
6
|
|
50
|
|
|
130
|
my $lang_session = $settings->{lang_session} || 'lang'; |
75
|
6
|
|
|
|
|
7
|
my $lang; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# don't force the user to store lang in a session |
78
|
6
|
50
|
|
|
|
21
|
if ( setting('session') ) { |
79
|
6
|
|
|
|
|
190
|
my $session_language = session $lang_session; |
80
|
|
|
|
|
|
|
|
81
|
6
|
50
|
|
|
|
1190
|
if ( !$session_language ) { |
82
|
0
|
|
|
|
|
0
|
$lang = _detect_lang_from_browser(); |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
0
|
session $lang_session => $lang; |
85
|
0
|
|
|
|
|
0
|
return $lang; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
else { |
88
|
6
|
|
|
|
|
17
|
return $session_language; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
$lang = _detect_lang_from_browser(); |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
return $lang; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub _detect_lang_from_browser { |
98
|
0
|
|
|
0
|
|
|
my $lang = request->accept_language; |
99
|
|
|
|
|
|
|
|
100
|
0
|
0
|
|
|
|
|
return unless $lang; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
$lang =~ s/-\w+//g; |
103
|
0
|
0
|
|
|
|
|
$lang = (split(/,\s*/,$lang))[0] if $lang =~ /,/; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
return $lang; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
register_plugin; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
1; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
__END__ |