Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How use Laravel Language Translation in controller as constant? [duplicate]

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');
like image 717
Adam Kozlowski Avatar asked Dec 06 '17 12:12

Adam Kozlowski


People also ask

What is {{ __ }} In laravel?

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).

What is @lang in laravel?

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.


2 Answers

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.

like image 189
timod Avatar answered Nov 15 '22 10:11

timod


Use trans('message.failed') instead of Lang::get('message.failed') https://laravel.com/docs/5.5/helpers#method-trans

like image 26
Amando Vledder Avatar answered Nov 15 '22 10:11

Amando Vledder