diff options
author | franck cuny <franck@lumberjaph.net> | 2010-04-25 22:08:33 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-04-25 22:08:33 +0200 |
commit | 9a8304acdf7e12a23488072b974d9194a731ef0d (patch) | |
tree | 9584df83689095f621a73184a5225dccf5f8296a | |
parent | initial commit (diff) | |
download | dancer-plugin-i18n-9a8304acdf7e12a23488072b974d9194a731ef0d.tar.gz |
languages method to set various languages; l method to localize some text; POD
-rw-r--r-- | lib/Dancer/Plugin/I18n.pm | 80 |
1 files changed, 77 insertions, 3 deletions
diff --git a/lib/Dancer/Plugin/I18n.pm b/lib/Dancer/Plugin/I18n.pm index 0bcc402..67c57ec 100644 --- a/lib/Dancer/Plugin/I18n.pm +++ b/lib/Dancer/Plugin/I18n.pm @@ -2,22 +2,96 @@ package Dancer::Plugin::I18n; use strict; use warnings; +use Dancer::Plugin; + +use I18N::LangTags; +use I18N::LangTags::Detect; + our $VERSION = '0.01'; +my @languages; +my $i18n_package; + +add_hook( + before_dispatch => sub { + my $request = shift; + @languages = ('en'); + push @languages, I18N::LangTags::implicate_supers( + I18N::LangTags::Detect->http_accept_langs( + scalar $request->header('Accept-Language') + ) + ); + my $app = "TestApp"; + $i18n_package = $app."::I18N"; + eval "package $i18n_package; use base 'Locale::Maketext'; 1;"; + die "Impossible to load I18N plugin : $@" if $@; + } +); + +add_hook( + before_template => sub { + my ( $view, $tokens ) = @_; + $tokens->{l} = sub { localize(@_) }; + $tokens->{languages} = sub { languages(@_) }; + }, +); + +register languages => sub { languages(@_); }; +register l => sub { localize(@_); }; + +sub languages { + my @lang = shift; + return $languages[0] unless @lang; + unshift @languages, @lang; + return; +} + +sub localize { + if ( $i18n_package && ( my $h = $i18n_package->get_handle(@languages) ) ) + { + + return $h->maketext(@_); + } + else { + return join '', @_; + } +} + +register_plugin; + 1; __END__ =head1 NAME -Dancer::Plugin::I18n - +Dancer::Plugin::I18n - Intenationalization for Dancer =head1 SYNOPSIS - use Dancer::Plugin::I18n; + package myapp::I18N::fr; + use base 'myapp::I18N'; + our %Lexicon = ( hello => 'bonjour' ); + 1; + + package myapp; + use Dancer; + use Dancer::Plugin::I18n; + get '/' => sub { template 'index' }; + + # index.tt + hello in <% languages %> => <% l('hello') %> + # or + <% languages('fr') %>This is an <% l('hello') %> =head1 DESCRIPTION -Dancer::Plugin::I18n is +Dancer::Plugin::I18n add L<Locale::Maketext> to your L<Dancer> application + +=head1 METHODS + +=head2 languages + +=head2 l =head1 AUTHOR |