Is it possible to use Laravel Language Translator (Localization) in controller as constant? Below attempt, example that does not work:
class SearchApproval extends Controller
{
private $request;
const stage_1 = Lang::get('message.failed');
The __ helper function can be used to retrieve lines of text from language files. You can retrieve lines of text from either key-based translation files (represented as PHP arrays) or from literal, string-based translation files (represented as a JSON object).
Laravel-lang is a collection of over 68 language translations for Laravel by developer Fred Delrieu (caouecs), including authentication, pagination, passwords, and validation rules. This package also includes JSON files for many languages.
trans it's a global function, so you can use it directly from your controller
trans('messages.failed');
but this won't work as constant, so you can use it like this:
class SearchApproval extends Controller
{
private $request;
const stage_1 = 'message.failed';
public function xxx(){
$whatever = trans(self::stage_1);
}
}
UPDATED:
You can use this __('Your Text') inside the controller.
Use trans('message.failed')
instead of Lang::get('message.failed')
https://laravel.com/docs/5.5/helpers#method-trans
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With