sharifulin / mojolicious-plugin-i18n

Internationalization Plugin for Mojolicious 5.x and higher
11 stars 11 forks source link

Load language files outside of http server (e.g. Minion) #20

Open Htbaa opened 8 years ago

Htbaa commented 8 years ago

Hi,

when using Minion (job manager for Mojolicious, which ties nicely into the framework) to process background jobs the language files aren't being loaded by the I18N plugin. One use case where this would be nice to have is when you send localized emails in the background.

It think the problem lies within the way this plugin initializes. It uses the before_dispatch hook, which as far as I can see isn't being executed by a Minion worker.

Would there be a way to be able to load the language files even when the before_dispatch hook isn't being fired? Manually setting the desired language for such use cases would be fine, no need for auto detection I think.

sharifulin commented 8 years ago

Hi Cris,

Sorry, I don't understand your problem. Could you make a fork or patch with test suite?

Anatoly

On Fri, Apr 8, 2016 at 4:38 PM, Christiaan Kras notifications@github.com wrote:

Hi,

when using Minion (job manager for Mojolicious, which ties nicely into the framework) to process background jobs the language files aren't being loaded by the I18N plugin. One use case where this would be nice to have is when you send localized emails in the background.

It think the problem lies within the way this plugin initializes. It uses the before_dispatch hook, which as far as I can see isn't being executed by a Minion worker.

Would there be a way to be able to load the language files even when the before_dispatch hook isn't being fired? Manually setting the desired language for such use cases would be fine, no need for auto detection I think.

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/sharifulin/mojolicious-plugin-i18n/issues/20

С уважением, Анатолий Шарифулин.

Htbaa commented 8 years ago

Hi Anatoly,

Sorry, I seem to have forgotten the actual issue at hand :-).

But basically if I have the string foobar in my language file to contain test (so foobar => 'test') and I call $self->app->l('foobar') then the result will be foobar and not test.

I'll try to ready some test code next Monday.

Htbaa commented 8 years ago

Add these lines to i18n_app.t:

$t->app->languages('en_us');
is($t->app->l('hello'), 'Hello two US');
$t->app->languages('es');
is($t->app->l('hello'), 'hola');

Both tests fail. The content returned is the string hello instead of the translations.

csroli commented 7 years ago

Hi Christiaan!

I encountered the same problem with I18N and Minion.

The main reason for the missing translations is because when you use the "l" helper on a Mojolicious app, it uses the $app->stash helper internally. In Mojolicious (according to latest docs) when you call stash on an app, it creates a new empty Mojolicious::Controller object, and populates its stash. That stash will not be the same that you are using for render, etc.

I solved the problem with a helper controller, then called the $app methods on that. Eg.:

  $app->minion->add_task(send_email => sub {
    my $job = shift; 
    my $app = $job->app;
    my $controller = $app->build_controller(Mojolicious::Controller->new); ##
    $controller->languages("hu"); ##
    ...
    my $email_html_body = $controller->render_to_string(template=>"...", items=>...); ##
    ... 
    $app->generate_email(
      $email_html_body, 
      {subject => $controller->l($direction) }, ##
    );
  };

This way I've got the translations in the mail body and the subject too. I think it's not the perfect answer, because it loads the translations in the beginning of every run, but it works.

Htbaa commented 7 years ago

Hi csroli,

Well, at least it's a workaround. Great find.