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