Understanding Laravel 5.3 Multilingualism

Asked 2 years ago, Updated 2 years ago, 137 views

If you want to multilingualize validation messages (this time in Japanese),

/resources/lang/ja/validation.php

In the attributes array of the above files field name =>Japanese name
The field name is translated into Japanese, but
How can I tell if different models use the same field name?

For example,
The User model has a name field meaning username and
The Shop model has a name field meaning store name.

laravel laravel-5 localization i18n

2022-09-30 21:19

2 Answers

ValidatesRequests::validate attribute naming is

class UsersController extensions Controller
{
    public function post(Request$request)
    {
        $this->validate($request,[
            'name' = > 'required',
        ], null, [
            'name' = > 'username',
        ]);
    }
}

You can also specify it as shown in .


2022-09-30 21:19

You can do it by creating validators for each.

class UsersController extensions Controller {

    public function post(){

        $validator=Validator::make($request->all(),[
            'name' = > 'required',
        ]);

        $validator->setAttributeNames([]
            'name' = > 'username',
        ]);

        if($validator->fails()){
            return redirect ('users/')
                        - > with Errors ($validator)
                        ->withInput();
        }
    }
}
class ShopsController extensions Controller {

    public function post(){

        $validator=Validator::make($request->all(),[
            'name' = > 'required',
        ]);

        $validator->setAttributeNames([]
            'name' = > 'Store name',
        ]);

        if($validator->fails()){
            return redirect ('shops/')
                        - > with Errors ($validator)
                        ->withInput();
        }
    }
}

Also, if you have created a request and are validating it,
The attributes() method is available.

class UsersRequest extensions FormRequest {

    //...

    public function attributes() {
        return [
            'name' = > 'username',
        ];
    }
}


2022-09-30 21:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.