Context, DetectLang filter.
[blog.pm-common-perl-mods.git] / MojoX-Dispatcher-FilterChain / lib / MojoX / FilterChain / DetectLang.pm
bloba5beaa5f7ba73feecae3ee9fc8abba0126a58f3b
1 package MojoX::FilterChain::DetectLang;
3 use strict;
4 use warnings;
6 use base 'MojoX::FilterChain::Base';
8 use MojoX::FilterChain::Constants;
10 __PACKAGE__->attr('languages', default => sub {[]});
11 __PACKAGE__->attr('stash_key', default => 'language');
13 sub run {
14 my $self = shift;
15 my ( $c ) = @_;
17 if (my $path = $c->tx->req->url->path) {
18 my $part = $path->parts->[ 0 ];
20 if ( $part && grep { $part eq $_ } @{ $self->languages } ) {
22 shift @{ $path->parts };
24 $c->stash->{ $self->stash_key } = $part;
28 return NEXT;
32 __END__
34 =head1 NAME
36 MojoX::FilterChain::DetectLang - Detect client language filter
38 =head1 SYNOPSIS
40 $chain
41 ->add(MojoX::FilterChain::DetectLang->new(languages => [qw/ en de /]));
43 ...
45 # /
46 $chain->process($c);
47 $c->stash->{language}; # undef
49 # /en
50 $chain->process($c);
51 $c->stash->{language}; # en
53 # /de/path
54 $chain->process($c);
55 $c->stash->{language}; # de
57 =head1 DESCRIPTION
59 L<MojoX::FilterChain::DetectLang> is a filter for detecting client language
61 =head1 METHODS
63 L<MojoX::FilterChain::DetectLang> inherits all methods from
64 L<MojoX::FilterChain::Base> and implements the following ones.
66 =head2 C<languages>
68 Languages that your website can handle.
70 =head2 C<stash_key>
72 Stash variable name where language will be stored.
74 =cut