summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-05-25 08:29:34 +0200
committerfranck cuny <franck@lumberjaph.net>2010-05-25 08:29:34 +0200
commit34a15ef1645a5e392e8fd9b753d96279714d0058 (patch)
treecd80b4935ec7d77e067b950f8c90540fa8d545e0
downloadplack-middleware-i18n-34a15ef1645a5e392e8fd9b753d96279714d0058.tar.gz
initial commit
-rw-r--r--lib/Plack/Middleware/i18n.pm34
-rw-r--r--t/01_basic.t27
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();