Amon2-Plugin-L10N

 view release on metacpan or  search on metacpan

lib/Amon2/Plugin/L10N.pm  view on Meta::CPAN

    my($class, $klass, $po_file_langs, $default_lang, $po_dir, $lexicon_options) = @_;

    # make package variable
    {
        my %opt = (
            _preload => 1,
            _style   => 'gettext',
            _decode  => 1,
            _auto    => 1,
        );

        for my $lang (@{ $po_file_langs }) {
            if ($lang eq $default_lang) {
                $opt{$lang} = [ 'Auto' ];
            } else {
                $opt{$lang} = [ Gettext => File::Spec->catfile($po_dir, "$lang.po") ];
            }
        }

        no strict 'refs';
        ${"$klass\::L10N::LEXICON_OPTIONS"} = +{ %opt, %{ $lexicon_options } };
    };

    my $code = qq!
package $klass\::L10N;
use parent 'Locale::Maketext';
use Locale::Maketext::Lexicon \$$klass\::L10N::LEXICON_OPTIONS;
1;
!;
    eval $code or die $@;
}

1;
__END__

=encoding utf-8

=head1 NAME

Amon2::Plugin::L10N - L10N support for Amon2

=head1 DESCRIPTION

Amon2::Plugin::L10N is L10N support plugin for Amon2.

=head1 Implementation L10N for your App

=head2 in YourProj.pm

  __PACKAGE__->load_plugins('L10N' => {
      default_lang => 'en',                                  # default is en
      accept_langs => [qw/ en ja zh-tw zh-cn fr /],          # default is ['en']
      po_dir       => 'po',                                  # default is po
  });

=head2 in your YourProj::Web::ViewFunction

  use Text::Xslate ();
  sub l {
      my $string = shift;
      my @args = map { Text::Xslate::html_escape($_) } @_; # escape arguments
      Text::Xslate::mark_raw( YourProj->context->loc($string, @args) );
  }

=head2 in your tmpl/foo.tt

  [% l('Hello! %1', 'username') %]

=head2 in your some class

  package YourProj::M::Foo;
  
  sub bar {
      YourProj->context->loc('hello! %1', $username);
  }

=head2 hook of before language detection

  __PACKAGE__->load_plugins('L10N' => {
      accept_langs          => [qw/ en ja zh-tw zh-cn fr /],
      before_detection_hook => sub {
          my $c = shift;
          return unless ref($c);
  
          my $accept_re = qr/\A(?:en|ja|zh-tw)\z/;
          my $lang = $c->req->param('lang');
          if ($lang && $lang =~ $accept_re) {
              $c->session->set( lang => $lang );
              return $lang;
          } elsif (! defined $lang) {
              $lang = $c->session->get('lang');
              if ($lang && $lang =~ $accept_re) {
                  return $lang;
              }
          }
          $c->session->set( lang => '' );
          return; # through
      },
  });

=head2 hook of after language detection

  __PACKAGE__->load_plugins('L10N' => {
      accept_langs         => [qw/ en ja zh zh-tw zh-cn fr /],
      after_detection_hook => sub {
          my($c, $lang) = shift;
          return 'zh' if $lang =~ /\Azh(?:-.+)\z/;
          return $lang;
      },
  });

=head2 you can customize the po files name

  __PACKAGE__->load_plugins('L10N' => {
      accept_langs         => [qw/ zh-tw zh-cn zh /],
      po_file_langs        => [qw/ zh-tw zh-cn /],    # zh.po is not exists file
      after_detection_hook => sub {
          my($c, $lang) = shift;
          return 'zh-cn' if $lang eq 'zh'; # use zh-cn.po file
          return $lang;
      },



( run in 1.854 second using v1.01-cache-2.11-cpan-9581c071862 )