0

I would like to use the laravel default functionality to show the error, but in another language. I don't need a "nice name", but a translation for the **:attribute value in lang files. Right now if I just use:

<input type="text" placeholder="{{ trans('generic.phone') }}" name="phone" value="{{ old('phone') }}">
@if ($errors->has('phone'))
<span class="help-block">
    <strong>{{ $errors->first('phone') }}</strong>
</span>
@endif

it works perfectly, because the :attribute takes the field phone, and in the validation language I have:

'required' => 'The :attribute field is required.',

But how to manage phone field with a language file?

Do I must have to write a custom error for each field? Please tell me I don't.

The following function is just a wrong example to let you understand what I'm trying to do

@if ($errors->has('phone'))
    <strong>{{ $errors->first(trans('generic.phone')) }}</strong>
@endif
Vixed
  • 3,429
  • 5
  • 37
  • 68
  • 2
    Possible duplicate of [Laravel validation attributes "nice names"](http://stackoverflow.com/questions/17047116/laravel-validation-attributes-nice-names) – Ken Jan 27 '17 at 15:30
  • Now you have `auth.php` file only in `en` directory, create other locale directories, Ex: `es`, `fr`. And paste all files there: `auth.php`, `passwords.php`... You can take translated files from here: [https://github.com/caouecs/Laravel-lang](https://github.com/caouecs/Laravel-lang) – zgabievi Jan 27 '17 at 15:30
  • @zgabievi I know that, but I need to translate the **:attribute** too and this is set in the controller/view not in lang. – Vixed Jan 27 '17 at 17:56
  • 1
    There is ** Custom Validation Attributes** array in `validation.php` file. I think you are looking for that one. – zgabievi Jan 27 '17 at 18:01
  • @Ken you're right, it's a duplicate, I saw the question you said. The problem it's the Laravel version, and that he posted a misleading question. – Vixed Jan 28 '17 at 11:26

1 Answers1

1

Really simple! Just add attributes in the language file! In my case, for lang/it/validation.php

I just set:

'attributes' => [
    'phone' => 'telefono',
],

while all remain the same for the sentences

'required' => 'Il campo :attribute &grave; obbligatorio.',
Vixed
  • 3,429
  • 5
  • 37
  • 68