Plack::I18N - I18N for Plack
use Plack::I18N; use Plack::Builder; my $i18n = Plack::I18N->new(lexicon => 'gettext', locale_dir => 'locale/'); builder { enable 'I18N', i18n => $i18n; sub { my $env = shift; my $handle = $env->{'plack.i18n.handle'}; [200, [], [$handle->maketext('Hello')]]; }; };
Plack::I18N is an easy way to add i18n to your application. Plack::I18N supports both Locale::Maketext *.pm files and gettext *.po files. Use whatevers suits better.
*.pm
gettext
*.po
See https://github.com/vti/plack-i18n/tree/master/examples directory for both examples.
Language detection is done via HTTP headers, session cookies, URL path prefix and so on. See Plack::Middleware::I18N for details.
$env
Plack::Middleware::I18N registers the following $env parameters:
plack.i18n
Holds Plack::I18N instance.
plack.i18n.language
Current detected language. A shortcut for $env->{'plack.i18n'}->language.
$env->{'plack.i18n'}->language
plack.i18n.handle
A shortcut for $env->{'plack.i18n'}->handle($env->{'plack.i18n.language'}).
$env->{'plack.i18n'}->handle($env->{'plack.i18n.language'})
new
Creates new object.
Options:
One of gettext or maketext.
maketext
This is usually MyApp::I18N. This class is automatically generated if does not exist. In case of gettext i18n_class it even doesn't have to be specified.
MyApp::I18N
i18n_class
Directory where translations are stored.
Default language. en by default.
en
Available languages. Automatically detected unless specified.
default_language
Returns default language.
handle($language)
Returns handle of appropriate language.
languages
Returns available languages.
Viacheslav Tykhanovskyi, <viacheslav.t@gmail.com>
Copyright (C) 2015, Viacheslav Tykhanovskyi
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.
To install Plack::I18N, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Plack::I18N
CPAN shell
perl -MCPAN -e shell install Plack::I18N
For more information on module installation, please visit the detailed CPAN module installation guide.