line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::TagHelpersI18N; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: TagHelpers with I18N support |
4
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
2335
|
use strict; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
86
|
|
6
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
112
|
|
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
18
|
use Mojolicious::Plugin::TagHelpers; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
53
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
11
|
|
|
|
|
|
|
|
12
|
3
|
|
|
3
|
|
131
|
use Mojo::Collection; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
127
|
|
13
|
3
|
|
|
3
|
|
18
|
use Mojo::Util qw(deprecated xml_escape); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
163
|
|
14
|
3
|
|
|
3
|
|
19
|
use Scalar::Util 'blessed'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
145
|
|
15
|
3
|
|
|
3
|
|
2071
|
use Unicode::Collate; |
|
3
|
|
|
|
|
25330
|
|
|
3
|
|
|
|
|
114
|
|
16
|
|
|
|
|
|
|
|
17
|
3
|
|
|
3
|
|
28
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
31
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub register { |
20
|
3
|
|
|
3
|
1
|
27574
|
my ($self, $app, $config) = @_; |
21
|
|
|
|
|
|
|
|
22
|
3
|
|
|
|
|
26
|
my $collate = Unicode::Collate->new; |
23
|
|
|
|
|
|
|
$app->helper( select_field => sub { |
24
|
15
|
|
|
15
|
|
218862
|
_select_field(@_, collate => $collate ) |
25
|
3
|
|
|
|
|
138961
|
} ); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
$app->attr( |
28
|
3
|
|
100
|
|
|
291
|
translation_method => $config->{method} || 'l' |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _prepare { |
33
|
18
|
|
|
18
|
|
63
|
my ($self, $options, %attr) = @_; |
34
|
|
|
|
|
|
|
|
35
|
18
|
|
|
|
|
33
|
my %translated; |
36
|
18
|
|
|
|
|
36
|
my $index = 0; |
37
|
|
|
|
|
|
|
|
38
|
18
|
100
|
|
|
|
71
|
$options = [$options] if ref $options ne 'ARRAY'; |
39
|
|
|
|
|
|
|
|
40
|
18
|
|
|
|
|
35
|
for my $option ( @{$options} ) { |
|
18
|
|
|
|
|
50
|
|
41
|
32
|
|
|
|
|
64
|
my $ref = ref $option; |
42
|
|
|
|
|
|
|
|
43
|
32
|
100
|
|
|
|
96
|
if ( !$ref ) { |
|
|
100
|
|
|
|
|
|
44
|
22
|
|
|
|
|
55
|
$option = [$option => $option]; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
elsif ( $ref eq 'HASH') { |
47
|
2
|
|
|
|
|
10
|
deprecated |
48
|
|
|
|
|
|
|
'hash references are DEPRECATED in favor of Mojo::Collection objects'; |
49
|
|
|
|
|
|
|
|
50
|
2
|
|
|
|
|
717
|
my @values = map{ [ $_ => $option->{$_} ] } sort keys %{$option}; |
|
4
|
|
|
|
|
16
|
|
|
2
|
|
|
|
|
14
|
|
51
|
2
|
|
|
|
|
19
|
$option = Mojo::Collection->new( '' => [ @values ] ); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
32
|
100
|
|
|
|
110
|
if ( !$attr{no_translation} ) { |
55
|
26
|
|
|
|
|
78
|
my $method = $self->app->translation_method; |
56
|
26
|
|
|
|
|
275
|
$option->[0] = $self->$method( $option->[0] ); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
32
|
|
|
|
|
2400
|
push @{ $translated{ $option->[0] } }, $index; |
|
32
|
|
|
|
|
105
|
|
60
|
|
|
|
|
|
|
|
61
|
32
|
|
|
|
|
82
|
$index++; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
18
|
100
|
|
|
|
55
|
if ( $attr{sort} ) { |
65
|
3
|
|
|
|
|
19
|
my @sorted = $attr{collate}->sort( keys %translated ); |
66
|
3
|
|
|
|
|
1560
|
my @sorted_options = map{ @{$options}[ @{ $translated{$_} } ] }@sorted; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
20
|
|
|
6
|
|
|
|
|
11
|
|
67
|
|
|
|
|
|
|
|
68
|
3
|
|
|
|
|
16
|
return \@sorted_options; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
15
|
|
|
|
|
59
|
return $options; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _select_field { |
75
|
15
|
|
|
15
|
|
73
|
my ($self, $name, $options, %attrs) = (shift, shift, shift, @_); |
76
|
|
|
|
|
|
|
|
77
|
15
|
|
|
|
|
32
|
my %values = map { $_ => 1 } @{ $self->every_param($name) }; |
|
1
|
|
|
|
|
65
|
|
|
15
|
|
|
|
|
59
|
|
78
|
|
|
|
|
|
|
|
79
|
15
|
|
|
|
|
1566
|
my %opts; |
80
|
15
|
|
|
|
|
51
|
my @fields = qw(no_translation sort collate); |
81
|
15
|
|
|
|
|
77
|
@opts{@fields} = delete @attrs{@fields}; |
82
|
|
|
|
|
|
|
|
83
|
15
|
|
|
|
|
108
|
$options = _prepare( $self, $options, %opts ); |
84
|
|
|
|
|
|
|
|
85
|
15
|
|
|
|
|
39
|
my $groups = ''; |
86
|
15
|
|
|
|
|
36
|
for my $group (@$options) { |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# "optgroup" tag |
89
|
26
|
100
|
100
|
|
|
1419
|
if (blessed $group && $group->isa('Mojo::Collection')) { |
90
|
3
|
|
|
|
|
10
|
my ($label, $values) = splice @$group, 0, 2; |
91
|
3
|
|
|
|
|
11
|
my $prepared = _prepare( $self, $values, %opts ); |
92
|
3
|
|
|
|
|
11
|
my $content = join '', map { Mojolicious::Plugin::TagHelpers::_option(\%values, $_) } @$prepared; |
|
6
|
|
|
|
|
277
|
|
93
|
3
|
|
|
3
|
|
241
|
$groups .= Mojolicious::Plugin::TagHelpers::_tag('optgroup', label => $label, @$group, sub {$content}); |
|
3
|
|
|
|
|
33
|
|
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# "option" tag |
97
|
23
|
|
|
|
|
84
|
else { $groups .= Mojolicious::Plugin::TagHelpers::_option(\%values, $group) } |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
return Mojolicious::Plugin::TagHelpers::_validation( |
101
|
15
|
|
|
15
|
|
15573
|
$self, $name, 'select', %attrs, name => $name, sub {$groups}, |
102
|
15
|
|
|
|
|
1181
|
); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
1; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
__END__ |