| 1 | # ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
| 2 | package CatalystX::I18N::TraitFor::Request; | ||||||||||||||||||||||||||||||||||||||||||
| 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(headers user_agent address); | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use HTTP::BrowserDetect; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use IP::Country::Fast; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use CatalystX::I18N::TypeConstraints; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | has 'accept_language' => ( | |||||||||||||||||||||||||||||||||||||||||
| 15 | isa => 'Maybe[CatalystX::I18N::Type::Locales]', | ||||||||||||||||||||||||||||||||||||||||||
| 16 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 17 | lazy_build => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 18 | builder => '_build_accept_language', | ||||||||||||||||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 4 | has 'browser_language' => ( | |||||||||||||||||||||||||||||||||||||||||
| 22 | isa => 'Maybe[CatalystX::I18N::Type::Language]', | ||||||||||||||||||||||||||||||||||||||||||
| 23 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 24 | lazy_build => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | builder => '_build_browser_language', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 4 | has 'browser_territory' => ( | |||||||||||||||||||||||||||||||||||||||||
| 29 | isa => 'Maybe[CatalystX::I18N::Type::Territory]', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | lazy_build => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | builder => '_build_browser_territory', | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | has 'client_country' => ( | |||||||||||||||||||||||||||||||||||||||||
| 36 | isa => 'Maybe[CatalystX::I18N::Type::Territory]', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 38 | lazy_build => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 39 | builder => '_build_client_country', | ||||||||||||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 4 | has 'browser_detect' => ( | |||||||||||||||||||||||||||||||||||||||||
| 43 | isa => 'HTTP::BrowserDetect', | ||||||||||||||||||||||||||||||||||||||||||
| 44 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 45 | lazy_build => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | builder => '_build_browser_detect', | ||||||||||||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 13 | ▶ | sub _build_accept_language { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_accept_language: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 13 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 13 | my $accept_language = $self->headers->header('Accept-Language'); | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | return | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 13 | ▶ | unless $accept_language; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # Extract priority | ||||||||||||||||||||||||||||||||||||||||||
| 58 | my @accepted_languages = | ||||||||||||||||||||||||||||||||||||||||||
| 59 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 9 | my @tmp = split( /;\s*q=/, $_ ); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 26 | ▶ | $tmp[1] ||= 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$tmp[1] ||= 1
MC/DC100%1/1
$tmp[1] ||= 1
$tmp[1]
Truth table100%2/2
A
A= $tmp[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 26 | \@tmp; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } split( /\s*,\s*/, $accept_language ); | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 9 | my @sorted_locales; | |||||||||||||||||||||||||||||||||||||||||
| 66 | my @super_languages; | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | # Convert language tags to locales | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 9 | foreach my $element (sort { $b->[1] <=> $a->[1] } @accepted_languages) { | |||||||||||||||||||||||||||||||||||||||||
| 70 | 26 | my ($language,$dialect) = split /[_-]/,$element->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 26 | my $locale = lc($language); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 26 | ▶ | if (defined $dialect) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 8 | $locale .= '_'.uc($dialect); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 8 | push(@super_languages,$language); | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | next | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 26 | ▶ | unless $locale =~ $CatalystX::I18N::TypeConstraints::LOCALE_RE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 24 | push(@sorted_locales,$locale); | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | # Add super languages to locales | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 9 | foreach my $lanuage (@super_languages) { | |||||||||||||||||||||||||||||||||||||||||
| 83 | next | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 8 | ▶ | if grep { $lanuage eq $_ } @sorted_locales; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | next | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | ▶ | unless $lanuage =~ $CatalystX::I18N::TypeConstraints::LANGUAGE_RE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | push(@sorted_locales,$lanuage); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 9 | return \@sorted_locales; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 5 | ▶ | sub _build_browser_language { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_browser_language: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # browser_detect->language warns unless user_agent | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 5 | ▶ | return unless $self->user_agent; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 5 | my $language = $self->browser_detect()->language(); | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | return | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 5 | ▶ | unless defined $language; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 4 | $language = lc($language); | |||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 4 | my $constraint = Moose::Util::TypeConstraints::find_type_constraint('CatalystX::I18N::Type::Language'); | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | return | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 4 | ▶ | unless $constraint->check($language); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | return $language; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 7 | ▶ | sub _build_browser_territory { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_browser_territory: called CC=6 SCAR=24.2
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | # browser_detect->country warns unless user_agent | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 7 | ▶ | return unless $self->user_agent; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 4 | my $territory = $self->browser_detect()->country(); | |||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | return | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 4 | ▶ | if ! defined $territory || ! $territory || $territory eq '**'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not defined $territory && $territory or $territory eq "**"
defined $territory && $territory
MC/DC0%0/3
not defined $territory && $territory or $territory eq "**"
defined $territory
$territory
$territory eq "**"
Truth table25%1/4
not(A && B) or C
A= defined $territoryB= $territoryC= $territory eq "**"
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | my $constraint = Moose::Util::TypeConstraints::find_type_constraint('CatalystX::I18N::Type::Territory'); | |||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | return | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | unless $constraint->check($territory); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | return uc($territory); | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 5 | ▶ | sub _build_browser_detect { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_browser_detect: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 5 | return HTTP::BrowserDetect->new($self->user_agent); | |||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 7 | ▶ | sub _build_client_country { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_client_country: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 7 | my $ip_address = $self->address; | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | return | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 7 | ▶ | unless $ip_address; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 7 | my $ip_country = IP::Country::Fast->new(); | |||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 7 | my $country = $ip_country->inet_atocc($ip_address); | |||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | return | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 7 | ▶ | if ! $country || $country eq '**'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $country or $country eq "**"
MC/DC50%1/2
not $country or $country eq "**"
not $country
$country eq "**"
Truth table66%2/3
not(A) or B
A= not $countryB= $country eq "**"
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 2 | return $country; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 4 | ▶ | no Moose::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | CatalystX::I18N::TraitFor::Request - Adds various I18N methods to a Catalyst::Request object | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | package MyApp::Catalyst; | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | use CatalystX::RoleApplicator; | ||||||||||||||||||||||||||||||||||||||||||
| 172 | use Catalyst qw/MyPlugins | ||||||||||||||||||||||||||||||||||||||||||
| 173 | CatalystX::I18N::Role::Base/; | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | __PACKAGE__->apply_request_class_roles(qw/CatalystX::I18N::TraitFor::Request/); | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | Adds several attributes to a L<Catalyst::Request> object that help you | ||||||||||||||||||||||||||||||||||||||||||
| 180 | determine a users language and locale. | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | All attributes are lazy. This means that the values will be only calculated | ||||||||||||||||||||||||||||||||||||||||||
| 183 | when the attributes is read/called the first time. | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | =head3 accept_language | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | my @languages = $c->request->accept_language(); | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | Returns an ordered list of accepted languages (from the 'Accept-Language' | ||||||||||||||||||||||||||||||||||||||||||
| 192 | header). Inavlid entries in the language headers are filtered. | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | =head3 browser_language | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | my $browser_language = $c->request->browser_language(); | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | Returns the language of the browser (form the 'User-Agent' header) | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | =head3 browser_territory | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | my $browser_territory = $c->request->browser_territory(); | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | Returns the territory of the browser (form the 'User-Agent' header) | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | =head3 client_country | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | my $browser_territory = $c->request->client_country(); | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | Looks up the client IP-address via L<IP::Country::Fast>. | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | =head3 browser_detect | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | my $browser_detect = $c->request->browser_detect(); | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | Returns a L<HTTP::BrowserDetect> object. | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | L<Catalyst::Request>, L<IP::Country::Fast>, L<HTTP::BrowserDetect> | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | Maroš Kollár | ||||||||||||||||||||||||||||||||||||||||||
| 225 | CPAN ID: MAROS | ||||||||||||||||||||||||||||||||||||||||||
| 226 | maros [at] k-1.com | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | L<http://www.k-1.com> | ||||||||||||||||||||||||||||||||||||||||||