2

I am creating an application in Laravel and I am stuck on the translate part
I have a form field that looks like this:

{!! Form::password('password') !!}

When a user leaves the field blank I get the error:

The password field is required

This would be correct for the english version.

But when I change my application to dutch I want to see

Het wachtwoord veld is verplicht

I already added this to the translation file but as this sentece will be used for every field like for example username and email aswell it uses the name of the field

So now I get the message 'Het password veld is verplicht' instead of 'Het wachtwoord veld is verplicht'

I know that I should not change the name of the input field as my controller expects the password field to have the name password This is the code that creates the user as you can see it uses $data->password

public function createUser($data)
{
    /* store the users email in variable */
    $email = $data->email;

    /* Creates a new user in the database with the filled in email and password */
    $this->create([
        'email' => $email,
        'password' => \Hash::make($data->password)
    ]);
}

My question is how can I get the name of the field in the requested language? So I still want to be able to user $data->password but I also want to see the field name in the correct language.

Does anyone have an idea of how this can be done?

Note I know how to use the translate options in laravel I dont want any answers like:
To translate in blade use @lang('translatefile.name')

Szenis
  • 4,020
  • 2
  • 21
  • 34
  • Just so I'm clear - you're not talking about adding `validation.php` to `lang/nl` or whatever language you're doing it for – haakym Jun 06 '15 at 22:23
  • I already have the validation.php in lang/nl what i need is to be able to translate the name="password" after validation went wrong. So that the compleet message will be dutch. Now it is part dutch with the name of the field in english. – Szenis Jun 06 '15 at 23:04
  • 1
    Is the `setAttributeNames` method for the validator something you've explored? – haakym Jun 06 '15 at 23:35

2 Answers2

6

As I looked up the setAttributeNames method as haakym has posted in the comment I found out something that solved the issue.

In laravel 5 at the bottom of the validation.php there is an array called attributes.
In the array you can provide the translation of the name attributes.

It was actually a quite simple solution but it took me several hours to find out

Thank you haakym for your suggestion as it helped me find my answer!

Szenis
  • 4,020
  • 2
  • 21
  • 34
3

Using a validator

If you're doing validation using a validator directly you can do this:

// set up the validator
$validator = Validator::make(Input::all(), $rules);

// set the attribute names
$validator->setAttributeNames(['password' => 'wachtwoord']);

Alternatively I believe you can add it as the final argument when creating a validator instance. See documentation: http://laravel.com/api/5.0/Illuminate/Validation/Validator.html#method___construct

Using a request

If you're doing validation using a request you can override getValidatorInstance in the request:

protected function getValidatorInstance()
{
  $validator = parent::getValidatorInstance();

  $validator->setAttributeNames([
    'password' => 'wachtwoord'
  ]);

  return $validator;
}
haakym
  • 12,050
  • 12
  • 70
  • 98