Mojolicious::Plugin::I18N usage

124 views
Skip to first unread message

Pavel Serikov

unread,
Jun 17, 2016, 4:48:04 AM6/17/16
to Mojolicious
Hi all,

I made one-file Mojolicious app (test.pl) to test Mojolicious::Plugin::I18N, but test fails.

package main;
use Mojo::Base -strict;
use Test::More;
use Test::Mojo;


{
   
package MyApp::Controller::Test;
   
use Mojo::Base 'Mojolicious::Controller';
   
sub info {
     
my $self = shift;
      $self
->render(json => { msg => $self->l('hello')} );
   
}

   
package MyApp::I18N::ru;
   
use Mojo::Base 'MyApp::I18N';
   
our %Lexicon = (hello => 'Privet');
   
1;

   
package MyApp;
   
use Mojo::Base 'Mojolicious';
   
sub startup {
     
my $self = shift;
     $self
->plugin('I18N');
     $self
->routes->get('/info')->to('test#info');
   
}

}

my $app = MyApp::->new();
my $t = Test::Mojo->new($app);
$t
->get_ok('/info/ru')->status_is(200)->json_is({msg => 'hallo'});
done_testing
;

error that I got:

perl test.pl 
Can't locate MyApp/I18N.pm in @INC (you may need to install the MyApp::I18N module)

What can be wrong? 

Stefan Adams

unread,
Jun 20, 2016, 2:43:25 PM6/20/16
to mojolicious

On Fri, Jun 17, 2016 at 3:48 AM, Pavel Serikov <pavel.p...@gmail.com> wrote:
   package MyApp::I18N::ru;
   
use Mojo::Base 'MyApp::I18N';

Can't locate MyApp/I18N.pm in @INC (you may need to install the MyApp::I18N module)

You are subclassing MyApp::I18N into MyApp::I18N::ru, but I do not see a package MyApp::I18N defined in your code.

Pavel Serikov

unread,
Jun 21, 2016, 4:05:36 AM6/21/16
to Mojolicious
Hi Stefan,

Thank you for answer. According source code MyApp::I18N must be defined when I register the plugin:

$self->plugin('I18N');

I (and not only me) think that the plugin doesn't work in current Mojolicious version - tested it at 6.55 and 6.66. I'm trying to understand the reason and it will be great if you will help me.
 
For example, when I have
%=l 'about_service'
at my template and localization module looks like

package MyApp::I18N::en;

use Mojo::Base 'MyApp::I18N';
require Exporter;
our @ISA = qw(Exporter);
our %Lexicon = (
    about_service
=> 'About app',
);
1;

at rendered template I still see 'about_service' text instead of 'About app'

I also tried to specify the url, e.g. /en. At log I see debug message

[Tue Jun 21 11:02:41 2016] [debug] Found language en in URL /en
[Tue Jun 21 11:02:41 2016] [debug] GET "/"
[Tue Jun 21 11:02:41 2016] [debug] Routing to a callback
[Tue Jun 21 11:02:41 2016] [debug] Rendering cached template "index.html.ep"
[Tue Jun 21 11:02:41 2016] [debug] Rendering cached template "layouts/default.html.ep"
[Tue Jun 21 11:02:41 2016] [debug] 200 OK (0.007188s, 139.121/s)

But localize subroutine still doesn't work.

What could be the problem?


понедельник, 20 июня 2016 г., 21:43:25 UTC+3 пользователь Stefan Adams написал:

Pavel Serikov

unread,
Jun 22, 2016, 8:15:40 AM6/22/16
to Mojolicious
Problem solved :)
I just forgot to put I18N folder at MyApp folder at root of project.

вторник, 21 июня 2016 г., 11:05:36 UTC+3 пользователь Pavel Serikov написал:
Reply all
Reply to author
Forward
0 new messages