summaryrefslogblamecommitdiff
path: root/lib/Plack/Middleware/i18n.pm
blob: 6a6e9272f763f05649f069427eb2723292bc9697 (plain) (tree)
1
2
3
4
5
6
7
8



                                
 

                                           
                   







                               

























                                                                        

 
 


       
package Plack::Middleware::i18n;

use strict;
use warnings;

use Plack::Util;
use Plack::Util::Accessor qw/default_lang/;
use Plack::Request;
use I18N::LangTags;
use I18N::LangTags::Detect;

our $VERSION = '0.01';

use parent 'Plack::Middleware';

sub call {
    my ($self, $env) = @_;

    my $locale;

    if (my $lang = $env->{'HTTP_ACCEPT_LANGUAGE'}) {
        $locale = (
            split(
                '-',
                (   I18N::LangTags::implicate_supers(
                        I18N::LangTags::Detect->http_accept_langs($lang)
                    )
                  )[0]
            )
        )[0];
    }
    else {
        $locale = $self->default_lang // 'en';
    }

    $env->{'psgix.locale'} = $locale;

    if (my $session = $env->{'psgix.session'}) {
        $session->{locale} = $locale;
    }

    $self->app->($env);
}


1;

__END__