| 1 | package Plack::Middleware::I18N; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use parent 'Plack::Middleware'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use List::Util qw(first); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use I18N::AcceptLanguage; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Plack::Util::Accessor qw(i18n use_path use_session use_header custom_cb); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 13 | ▶ | sub prepare_app { | ||||||||||||||||||||||||||
|
Subroutine
prepare_app: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 13 | ▶ | croak 'i18n required' unless $self->{i18n}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 12 | ▶ | $self->{use_path} = 1 unless defined $self->{use_path}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 12 | ▶ | $self->{use_session} = 1 unless defined $self->{use_session}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 12 | ▶ | $self->{use_header} = 1 unless defined $self->{use_header}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 12 | return $self; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 12 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 28 | 12 | my ($env) = @_; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 12 | $self->_detect_language($env); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 12 | return $self->app->($env); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 12 | ▶ | sub _detect_language { | ||||||||||||||||||||||||||
|
Subroutine
_detect_language: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 36 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | 12 | my ($env) = @_; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 12 | my $lang; | |||||||||||||||||||||||||||
| 40 | 12 | ▶ | $lang = $self->_detect_from_path($env) if $self->use_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 12 | ▶ | $lang ||= $self->_detect_from_session($env) if $self->use_session; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$lang ||= $self->_detect_from_session($env)
MC/DC100%2/2
$lang ||= $self->_detect_from_session($env)
$lang
$self->_detect_from_session($env)
Truth table100%3/3
A ||= B
A= $langB= $self->_detect_from_session($env)
| |||||||||||||||||||||||||||||
| 42 | 12 | ▶ | $lang ||= $self->_detect_from_header($env) if $self->use_header; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$lang ||= $self->_detect_from_header($env)
MC/DC100%2/2
$lang ||= $self->_detect_from_header($env)
$lang
$self->_detect_from_header($env)
Truth table100%3/3
A ||= B
A= $langB= $self->_detect_from_header($env)
| |||||||||||||||||||||||||||||
| 43 | 12 | ▶ | $lang = $self->_detect_from_custom_cb($env, $lang) if $self->custom_cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 12 | ▶ | $lang ||= $self->i18n->default_language; | ||||||||||||||||||||||||||
|
Condition66%2/3
$lang ||= $self->i18n->default_language
MC/DC0%0/2
$lang ||= $self->i18n->default_language
$lang
$self->i18n->default_language
Truth table66%2/3
A ||= B
A= $langB= $self->i18n->default_language
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 12 | $env->{'plack.i18n'} = $self->i18n; | |||||||||||||||||||||||||||
| 48 | 12 | $env->{'plack.i18n.language'} = $lang; | |||||||||||||||||||||||||||
| 49 | 12 | $env->{'plack.i18n.handle'} = $self->i18n->handle($lang); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 12 | ▶ | if ($self->{use_session}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 11 | $env->{'psgix.session'}->{'plack.i18n.language'} = $lang; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 7 | ▶ | sub _detect_from_session { | ||||||||||||||||||||||||||
|
Subroutine
_detect_from_session: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 57 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 58 | 7 | my ($env) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 7 | ▶ | return unless my $session = $env->{'psgix.session'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | ▶ | return unless my $lang = $session->{'plack.i18n.language'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | ▶ | return unless $self->_is_allowed($lang); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | return $lang; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 11 | ▶ | sub _detect_from_path { | ||||||||||||||||||||||||||
|
Subroutine
_detect_from_path: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 70 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 71 | 11 | my ($env) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 11 | my $path = $env->{PATH_INFO}; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 11 | my $languages_re = join '|', $self->i18n->languages; | |||||||||||||||||||||||||||
| 76 | 11 | ▶ | if ($path =~ s{^/($languages_re)(?=/|$)}{}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 4 | $env->{PATH_INFO} = $path; | |||||||||||||||||||||||||||
| 78 | 4 | ▶ | return $1 if $self->_is_allowed($1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 7 | return; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 6 | ▶ | sub _detect_from_header { | ||||||||||||||||||||||||||
|
Subroutine
_detect_from_header: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 85 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 86 | 6 | my ($env) = @_; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 6 | ▶ | return unless my $accept_header = $env->{HTTP_ACCEPT_LANGUAGE}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | return | ||||||||||||||||||||||||||||
| 91 | 1 | ▶ | unless my $lang = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | $self->_build_acceptor->accepts($accept_header, [$self->i18n->languages]); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | return unless $self->_is_allowed($lang); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | return $lang; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2 | ▶ | sub _detect_from_custom_cb { | ||||||||||||||||||||||||||
|
Subroutine
_detect_from_custom_cb: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 100 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 101 | 2 | my ($env, $detected_lang) = @_; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2 | my $lang = $self->custom_cb->($env, $detected_lang); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 2 | ▶ | return unless $lang; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | ▶ | return unless $self->_is_allowed($lang); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1 | return $lang; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | ▶ | sub _build_acceptor { | ||||||||||||||||||||||||||
|
Subroutine
_build_acceptor: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 113 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 1 | return I18N::AcceptLanguage->new(); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 8 | ▶ | sub _is_allowed { | ||||||||||||||||||||||||||
|
Subroutine
_is_allowed: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 119 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 120 | 8 | my ($lang) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 8 | ▶ | return !!first { $lang eq $_ } $self->i18n->default_language, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 123 | $self->i18n->languages; | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1 | 1; | |||||||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||||||||