Amon2::Plugin::L10N - L10N support for Amon2
Amon2::Plugin::L10N is L10N support plugin for Amon2.
__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 });
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) ); }
[% l('Hello! %1', 'username') %]
package YourProj::M::Foo; sub bar { YourProj->context->loc('hello! %1', $username); }
__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 }, });
__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; }, });
__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; }, });
__PACKAGE__->load_plugins('L10N' => { default_lang => 'ja', accept_langs => [qw/ en ja /], before_detection_hook => sub { my $c = shift; return unless $NEV{CLI_MODE}; # CLI_MODE is example key return 'ja' if $ENV{LANG} =~ /ja/i; return 'en' if $ENV{LANG} =~ /en/i; return; # use default lang }, });
# in your MyApp.pm __PACKAGE__->load_plugins('L10N' => { accept_langs => [qw/ ja /], lexicon_options => { _auto => 0, }, });
package L10N; use strict; use warnings; use parent 'Locale::Maketext'; use File::Spec; use Locale::Maketext::Lexicon +{ 'ja' => [ Gettext => File::Spec->catfile('t', 'po', 'ja.po') ], _preload => 1, _style => 'gettext', _decode => 1, }; # in your MyApp.pm __PACKAGE__->load_plugins('L10N' => { accept_langs => [qw/ ja /], l10n_class => 'L10N', });
$ cpanm --with-suggests Amon2::Plugin::L10N
dependnt module list in the cpanfile file.
$ cd your_amon2_proj_base_dir $ perl amon2-xgettext.pl en ja fr zh-tw
$ vim po/ja.po $ vim po/zh-tw.po
Language that is detected will return.
It will return the text in the appropriate language.
Kazuhiro Osawa <yappo {at} shibuya {dot} pl>
Copyright 2013- Kazuhiro Osawa
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Amon2, Locale::Maketext::Lexicon, HTTP::AcceptLanguage, amon2-xgettext.pl
To install Amon2::Plugin::L10N, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Amon2::Plugin::L10N
CPAN shell
perl -MCPAN -e shell install Amon2::Plugin::L10N
For more information on module installation, please visit the detailed CPAN module installation guide.