| 1 | # ============================================================================ | ||||||||||||||||||||||||||||
| 2 | package CatalystX::I18N::TraitFor::ViewTT; | ||||||||||||||||||||||||||||
| 3 | # ============================================================================ | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | requires qw(template render); | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Template::Stash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | around render => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 14 | 1 | my $orig = shift; | |||||||||||||||||||||||||||
| 15 | 1 | my ( $self,$c,$template,$args ) = @_; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # local $Template::Stash::HASH_OPS; | ||||||||||||||||||||||||||||
| 18 | # local $Template::Stash::LIST_OPS; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | if ($c->can('i18n_collator')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 1 | my $collator = $c->i18n_collator; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | $Template::Stash::HASH_OPS->{'lsort'} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 24 | 0 | my ($hash) = @_; | |||||||||||||||||||||||||||
| 25 | 0 | return [ $collator->sort(keys %$hash) ]; | |||||||||||||||||||||||||||
| 26 | 1 | }; | |||||||||||||||||||||||||||
| 27 | 0 | ▶ | $Template::Stash::LIST_OPS->{'lsort'} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 28 | 0 | my ($list) = @_; | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | return $list | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | unless scalar @$list > 1; | ||||||||||||||||||||||||||||
| 31 | 0 | return [ $collator->sort(@$list) ]; | |||||||||||||||||||||||||||
| 32 | 1 | }; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | return $self->$orig($c,$template,$args); | |||||||||||||||||||||||||||
| 36 | 4 | }; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 3 | ▶ | around new => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=24.1
| |||||||||||||||||||||||||||||
| 39 | 3 | my $orig = shift; | |||||||||||||||||||||||||||
| 40 | 3 | my ( $self,$app,$config ) = @_; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 3 | ▶ | $config->{CATALYST_VAR} ||= '_c'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"CATALYST_VAR"} ||= "_c"
MC/DC0%0/1
$config->{"CATALYST_VAR"} ||= "_c"
$config->{"CATALYST_VAR"}
Truth table50%1/2
A
A= $config->{"CATALYST_VAR"}
| |||||||||||||||||||||||||||||
| 43 | 3 | ▶ | $config->{FILTERS} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"FILTERS"} ||= {}
MC/DC0%0/1
$config->{"FILTERS"} ||= {}
$config->{"FILTERS"}
Truth table50%1/2
A
A= $config->{"FILTERS"}
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 3 | ▶ | if ($app->can('i18n_numberformat')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 3 | ▶ | $config->{FILTERS}{number} ||= [ \&_i18n_numberformat_factory, 1 ]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"FILTERS"}{"number"} ||= [\&CatalystX::I18N::TraitFor::ViewTT::_i18n_numberformat_factory, 1]
MC/DC0%0/1
$config->{"FILTERS"}{"number"} ||= [\&CatalystX::I18N::TraitFor::ViewTT::_i18n_numberformat_factory, 1]
$config->{"FILTERS"}{"number"}
Truth table50%1/2
A
A= $config->{"FILTERS"}{"number"}
| |||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 3 | ▶ | if ($app->can('maketext')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 3 | ▶ | $config->{FILTERS}{maketext} ||= [ \&_i18n_maketext_factory, 1 ]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"FILTERS"}{"maketext"} ||= [\&CatalystX::I18N::TraitFor::ViewTT::_i18n_maketext_factory, 1]
MC/DC0%0/1
$config->{"FILTERS"}{"maketext"} ||= [\&CatalystX::I18N::TraitFor::ViewTT::_i18n_maketext_factory, 1]
$config->{"FILTERS"}{"maketext"}
Truth table50%1/2
A
A= $config->{"FILTERS"}{"maketext"}
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 3 | ▶ | if ($app->can('localize')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 3 | ▶ | $config->{FILTERS}{localize} ||= [ \&_i18n_localize_factory, 1 ]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"FILTERS"}{"localize"} ||= [\&CatalystX::I18N::TraitFor::ViewTT::_i18n_localize_factory, 1]
MC/DC0%0/1
$config->{"FILTERS"}{"localize"} ||= [\&CatalystX::I18N::TraitFor::ViewTT::_i18n_localize_factory, 1]
$config->{"FILTERS"}{"localize"}
Truth table50%1/2
A
A= $config->{"FILTERS"}{"localize"}
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Call original BUILDARGS | ||||||||||||||||||||||||||||
| 58 | 3 | return $self->$orig($app,$config); | |||||||||||||||||||||||||||
| 59 | 4 | }; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 4 | ▶ | sub _i18n_numberformat_factory { | ||||||||||||||||||||||||||
|
Subroutine
_i18n_numberformat_factory: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 62 | 4 | my ( $context, $format, @options ) = @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 4 | my $stash = $context->stash; | |||||||||||||||||||||||||||
| 65 | 4 | my $catalyst_var = $context->{CONFIG}{CATALYST_VAR}; | |||||||||||||||||||||||||||
| 66 | 4 | my $c = $stash->{$catalyst_var}; | |||||||||||||||||||||||||||
| 67 | 4 | weaken $c; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 4 | my $number_format = $c->i18n_numberformat; | |||||||||||||||||||||||||||
| 70 | 4 | ▶ | if (defined $format) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | undef $format | ||||||||||||||||||||||||||||
| 72 | 4 | ▶ | unless (grep { $format eq $_ } qw(number negative bytes price picture)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 4 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 76 | 4 | my $value = shift; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 4 | ▶ | my $local_format = 'format_'.($format || 'number'); | ||||||||||||||||||||||||||
|
Condition50%1/2
$format || 'number'
MC/DC0%0/1
$format || 'number'
$format
Truth table50%1/2
A
A= $format
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 4 | ▶ | return $c->maketext('n/a') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | unless defined $value; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | return $number_format->$local_format($value,@options); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 4 | } | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub _i18n_maketext_factory { | ||||||||||||||||||||||||||
|
Subroutine
_i18n_maketext_factory: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 89 | 0 | my ( $context,@params ) = @_; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | return _i18n_factory_helper('maketext', $context,@params) | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub _i18n_localize_factory { | ||||||||||||||||||||||||||
|
Subroutine
_i18n_localize_factory: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 95 | 0 | my ( $context,@params ) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | return _i18n_factory_helper('localize', $context,@params) | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub _i18n_factory_helper { | ||||||||||||||||||||||||||
|
Subroutine
_i18n_factory_helper: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 101 | 0 | my ( $method, $context, @params ) = @_; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | my $stash = $context->stash; | |||||||||||||||||||||||||||
| 104 | 0 | my $catalyst_var = $context->{CONFIG}{CATALYST_VAR}; | |||||||||||||||||||||||||||
| 105 | 0 | my $c = $stash->{$catalyst_var}; | |||||||||||||||||||||||||||
| 106 | 0 | weaken $c; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 109 | 0 | my ($msgid) = @_; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | if (scalar @params == 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
scalar @params == 1 and ref $params[0] eq "ARRAY"
MC/DC0%0/2
scalar @params == 1 and ref $params[0] eq "ARRAY"
scalar @params == 1
ref $params[0] eq "ARRAY"
Truth table0%0/3
A and B
A= scalar @params == 1B= ref $params[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 111 | && ref($params[0]) eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 112 | 0 | @params = @{$params[0]}; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | return $c->$method($msgid,@params); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 0 | } | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 4 | ▶ | no Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 120 | 4 | 1; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =encoding utf8 | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head1 NAME | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | CatalystX::I18N::TraitFor::ViewTT - Adds I18N filters and VMethods to a TT view | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # In your view | ||||||||||||||||||||||||||||
| 131 | package MyApp::View::TT; | ||||||||||||||||||||||||||||
| 132 | use Moose; | ||||||||||||||||||||||||||||
| 133 | extends qw(Catalyst::View::TT); | ||||||||||||||||||||||||||||
| 134 | with qw(CatalystX::I18N::TraitFor::ViewTT); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # In your TT template | ||||||||||||||||||||||||||||
| 137 | # Localised number format | ||||||||||||||||||||||||||||
| 138 | [% 22 | number('number') %] | ||||||||||||||||||||||||||||
| 139 | [% 22 | number %] | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Localised collation | ||||||||||||||||||||||||||||
| 142 | [% mylist.lsort().join(', ') %] | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # Maketext | ||||||||||||||||||||||||||||
| 145 | [% 'Hello %1!' | maketext(name) %] | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head2 Filters | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head3 number | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Formats a number with the current locale settings. You need to have | ||||||||||||||||||||||||||||
| 154 | the L<CatalystX::I18N::Role::NumberFormat> role loaded in Catalyst. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | The following formats are available | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =over | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =item * price | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =item * number (default) | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =item * bytes | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =item * negative | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =item * picture | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =back | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head3 maketext | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Returns the translation for the given string. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | You need to have the L<CatalystX::I18N::Role::Maketext> role loaded in | ||||||||||||||||||||||||||||
| 177 | Catalyst. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head3 localize | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Returns the translation for the given string. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | You need to have the L<CatalystX::I18N::Role::DataLocalize> role loaded in | ||||||||||||||||||||||||||||
| 184 | Catalyst. | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head2 VMethods | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head3 lsort | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Locale aware collation. You need to have | ||||||||||||||||||||||||||||
| 191 | the L<CatalystX::I18N::Role::Collate> role loaded in Catalyst. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | L<CatalystX::I18N::Role::NumberFormat>, L<CatalystX::I18N::Role::Collate>, | ||||||||||||||||||||||||||||
| 196 | L<CatalystX::I18N::Role::Maketext>, L<CatalystX::I18N::Role::DataLocalize> | ||||||||||||||||||||||||||||
| 197 | and L<Catalyst::View::TT> | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Maroš Kollár | ||||||||||||||||||||||||||||
| 202 | CPAN ID: MAROS | ||||||||||||||||||||||||||||
| 203 | maros [at] k-1.com | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | L<http://www.k-1.com> | ||||||||||||||||||||||||||||