diff options
-rw-r--r-- | lib/Dancer/Plugin/I18n.pm | 28 | ||||
-rw-r--r-- | t/01_basic.t | 17 |
2 files changed, 24 insertions, 21 deletions
diff --git a/lib/Dancer/Plugin/I18n.pm b/lib/Dancer/Plugin/I18n.pm index 1796cc3..2f5b871 100644 --- a/lib/Dancer/Plugin/I18n.pm +++ b/lib/Dancer/Plugin/I18n.pm @@ -2,7 +2,9 @@ package Dancer::Plugin::I18n; use strict; use warnings; + use Dancer::Plugin; +use Dancer ':syntax'; use I18N::LangTags; use I18N::LangTags::Detect; @@ -13,17 +15,21 @@ my @languages; my $i18n_package; add_hook( - before_dispatch => sub { - my $request = shift; + before => sub { + my $request = request; @languages = ('en'); - push @languages, I18N::LangTags::implicate_supers( + push @languages, + I18N::LangTags::implicate_supers( I18N::LangTags::Detect->http_accept_langs( - scalar $request->header('Accept-Language') + scalar $request->accept_language ) - ); - # FIXME what's the best method to get the application name ?? - my $app = "TestApp"; - $i18n_package = $app."::I18N"; + ); + + my $app = setting('appname'); + if (!$app) { + die "Impossible to find your application name; please set the appname in your configuration"; + } + $i18n_package = $app . "::I18N"; eval "package $i18n_package; use base 'Locale::Maketext'; 1;"; die "Impossible to load I18N plugin : $@" if $@; } @@ -31,7 +37,7 @@ add_hook( add_hook( before_template => sub { - my ( $view, $tokens ) = @_; + my $tokens = shift; $tokens->{l} = sub { localize(@_) }; $tokens->{languages} = sub { languages(@_) }; }, @@ -48,9 +54,7 @@ sub languages { } sub localize { - if ( $i18n_package && ( my $h = $i18n_package->get_handle(@languages) ) ) - { - + if ($i18n_package && (my $h = $i18n_package->get_handle(@languages))) { return $h->maketext(@_); } else { diff --git a/t/01_basic.t b/t/01_basic.t index 72bfcc6..dbdb70d 100644 --- a/t/01_basic.t +++ b/t/01_basic.t @@ -1,19 +1,17 @@ use strict; use warnings; -#use lib ('t/lib'); use Test::More tests => 2; use HTTP::Request; use LWP::UserAgent; use Plack::Loader; use Dancer::Config 'setting'; -#use TestApp; use Test::TCP; my $app = sub { - my $env = shift; + my $env = shift; my $request = Dancer::Request->new($env); Dancer->dance($request); }; @@ -21,18 +19,19 @@ my $app = sub { Test::TCP::test_tcp( client => sub { my $port = shift; - my $req = HTTP::Request->new(GET => "http://127.0.0.1:$port/"); - $req->header( 'Accept-Language' => 'fr' ); - my $ua = LWP::UserAgent->new; + my $req = HTTP::Request->new( GET => "http://127.0.0.1:$port/" ); + $req->header( 'Accept-Language' => 'fr' ); + my $ua = LWP::UserAgent->new; my $res = $ua->request($req); - like $res->content, qr/first we got bonjour/, 'french content'; - like $res->content, qr/then we have hallo/, 'german content'; + like $res->content, qr/first we got bonjour/, 'french content'; + like $res->content, qr/then we have hallo/, 'german content'; }, server => sub { use t::lib::TestApp; my $port = shift; setting apphandler => 'PSGI'; - setting template => 'template_toolkit'; + setting appname => 'TestApp'; + setting template => 'template_toolkit'; Dancer::Config->load; Plack::Loader->auto( port => $port )->run($app); } |