summaryrefslogtreecommitdiff
path: root/lib/Plack/Middleware/i18n.pm
blob: aabb0a96e140ea9a3751d71569ccccaacebd839d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package Plack::Middleware::i18n;

use strict;
use warnings;

use Plack::Util;
use Plack::Util::Accessor qw/default_locale/;
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_locale // 'en';
    }

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

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

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


1;

__END__