Home > OS >  Laravel using word replacement in abort() error message with __() isn't working
Laravel using word replacement in abort() error message with __() isn't working

Time:10-06

I have an an abort(404) with a custom error message with translation and word replacement. But the word isn't replaced, instead it's showing the keyword that's supposed to be replaced.

abort(404, __('errors.no-category-of-type'), ['foo' => 'bar']);

And in errors.php

    'no-category-of-type' => 'There is no :foo category.',

This returns There is no :foo category. instead of replacing the keyword.

I'm a bit surprised it's not working as it seems pretty straightforward and I just follow along what normally works fine of separately used language and abort functions.

CodePudding user response:

You need to pass ['foo' => 'bar'] on trans function as a second argument:

abort(404, __('errors.no-category-of-type', ['foo' => 'bar']));
  • Related