diff options
author | franck cuny <franck@lumberjaph.net> | 2010-05-25 08:29:34 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-05-25 08:29:34 +0200 |
commit | 34a15ef1645a5e392e8fd9b753d96279714d0058 (patch) | |
tree | cd80b4935ec7d77e067b950f8c90540fa8d545e0 | |
download | plack-middleware-i18n-34a15ef1645a5e392e8fd9b753d96279714d0058.tar.gz |
initial commit
-rw-r--r-- | lib/Plack/Middleware/i18n.pm | 34 | ||||
-rw-r--r-- | t/01_basic.t | 27 |
2 files changed, 61 insertions, 0 deletions
diff --git a/lib/Plack/Middleware/i18n.pm b/lib/Plack/Middleware/i18n.pm new file mode 100644 index 0000000..28d4536 --- /dev/null +++ b/lib/Plack/Middleware/i18n.pm @@ -0,0 +1,34 @@ +package Plack::Middleware::i18n; + +use strict; +use warnings; +use Plack::Util; +use Plack::Util::Accessor qw/default_lang/; +use I18N::LangTags; +use I18N::LangTags::Detect; + +our $VERSION = '0.01'; + +use parent 'Plack::Middleware'; + +sub call { + my $self = shift; + my $res = $self->app->(@_); + + my $h = Plack::Util::headers($res->[1]); + + my @languages = ($self->default_lang) if $self->default_lang; + push @languages, + I18N::LangTags::implicate_supers( + I18N::LangTags::Detect->http_accept_langs($h->get('Accept-Language'))); + + # XXX store languages in psgix.languages + # XXX if session, store in session too + # XXX maybe redirect to appropriate location ? + + return $res; +} + +1; + +__END__ diff --git a/t/01_basic.t b/t/01_basic.t new file mode 100644 index 0000000..630bd55 --- /dev/null +++ b/t/01_basic.t @@ -0,0 +1,27 @@ +use strict; +use warnings; +use Test::More; +use Plack::Builder; +use Plack::Test; +use HTTP::Request::Common; + +my $handler = builder { + enable "i18n"; + sub { + [ '200', ['Content-Type' => 'text/html', 'Accept-Language' => 'fr'], + ['Hello world'] + ]; + }; +}; + +test_psgi + app => $handler, + client => sub { + my $cb = shift; + { + my $req = GET "http://localhost/"; + ok my $res = $cb->($req); + } +}; + +done_testing(); |