Suppose there are still desperate comrades studying and proving the advantages of Perl over other web development tools. More rarely, among them are those who rated the Catalyst framework without fearing the lack of documentation in Russian. I believe that those who are interested in this post are already familiar with how to create a new project, add missing parts to View TT and install the CPAN .gettext , but not everything is so simple. Let's go to practice.MyApp . Take the update_po.sh script from the MojoMojo project and put it in the ./script project ./script . In order for this script to become suitable, it is necessary to do the following manipulation on it, I hope none of the authors will not mind.MOJOMOJO_DIR with PROJECT_DIR in the whole file. # Default setting PERL_DEFAULT=`which perl` PROJECT_DIR="." PROJECT_NAME="MyApp" PROJECT_NAME with the name of your project and make a replacement in the entire MojoMojo file with $PROJECT_NAME ../script . Both files are executable.po2json script is part of the module Locale::Simpleupdate_po.sh code, you can see that the collection of strings for localization occurs in the following files and directories:./lib/MyApp/I18N , it will be our .po files. If we use javascript, then we need to create a directory ./root/static/json .  $ ./script/myapp_create.pl view HTML TT ./lib/MyApp/View/HTML.pm . In its configuration section, add a path that will indicate where the templates will be located: __PACKAGE__->config( TEMPLATE_EXTENSION => '.tt', INCLUDE_PATH => [ MyApp->path_to('root', 'base'), ], render_die => 1, ); ./lib/MyApp.pm we do this default view: __PACKAGE__->config( name => 'MyApp', default_view => 'HTML', 'View::HTML' => { ENCODING => 'UTF-8', }, ... I18N and Unicode : use Catalyst qw/ -Debug ConfigLoader Static::Simple I18N Unicode ... I18N plugin adds loc() or localize() to the project. About other features you can read here Catalyst :: Plugin :: I18N .stash tool, we will display it in the page header. The second reflects the line in the body of the page by means of the function call in the template engine../lib/MyApp/Controller/Root.pm : sub index :Path :Args(0) { my ( $self, $c ) = @_; $c->stash->{title} = $c->loc("Home Page"); } ./root/base/index.tt <!doctype html> <html> <head> <meta charset="utf-8" /> <title>[% title %]</title> </head> <body> [% c.loc("Hello World!") %] </body> <html>   $ touch ./lib/MyApp/I18N/ru.po ./lib/MyApp/I18N/de.po   $ ./script/update_po.sh ru de If any errors occur, add the missing modules to the CPAN. "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Language: ru\n" #: root/base/index.tt:8 msgid "Hello World!" msgstr " !" #: lib/MyApp/Controller/Root.pm:32 msgid "Home Page" msgstr " " update_po.sh restart update_po.sh with the update_po.sh languages, the old data in the .po files is saved. <Model::DB> <connect_info> dsn dbi:mysql:database user username password password mysql_enable_utf8 1 </connect_info> </Model::DB> See the documentation for the DBI driver for details. sub enc { my ( $self, $str ) = @_; utf8::decode($str); return $str; } ./lib/MyApp.pm , respectively, we can call this function as from perl :  $c->stash->{field} = $c->enc($data);   [% c.enc(list.field) %] FILTERS in the configuration of the view ./lib/MyApp/View/HTML.pm __PACKAGE__->config( TEMPLATE_EXTENSION => '.tt', INCLUDE_PATH => [ MyApp->path_to('root', 'base'), ], render_die => 1, FILTERS => { enc => sub { utf8::decode($_[0]); $_[0]; }, } );   [% list.field | enc %]  function loc(str) { if ( (typeof(locale) === 'undefined') || (typeof locale[str] === 'undefined') ) return str; return locale[str] }  ... <script src="[% c.uri_for_static('json/') _ c.language _ '.po.json' %]" type="text/javascript"></script> </head> update_po.sh and edit the localization file in ./lib/MyApp/I18N ; call update_po.sh again to update the ./root/static/json/[lang].po.json file.Source: https://habr.com/ru/post/146893/
All Articles