翻译 laravel 中输入字段的名称

Translating the name of an input field in laravel

我正在 Laravel 中创建一个应用程序,但我卡在了翻译部分
我有一个如下所示的表单域:

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

当用户将该字段留空时,我收到错误消息:

The password field is required

这对于英文版来说是正确的。

但是当我将应用程序更改为荷兰语时,我想查看

Het wachtwoord veld is verplicht

我已经将它添加到翻译文件中,但是因为这句话将用于每个字段 例如用户名和电子邮件,它也使用字段的名称

现在我收到消息了 'Het password veld is verplicht' 而不是 'Het wachtwoord veld is verplicht'

我知道我不应该更改输入字段的名称,因为我的控制器希望密码字段的名称为 password 这是创建用户的代码,您可以看到它使用 $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)
    ]);
}

我的问题是如何获得请求语言的字段名称? 所以我仍然希望能够使用 $data->password 但我也希望以正确的语言查看字段名称。

有没有人知道如何做到这一点?

注意 我知道如何使用 laravel 中的翻译选项 我不想要任何类似的答案:
要在 blade 中翻译,请使用 @lang('translatefile.name')

当我查找 haakym 在评论中发布的 setAttributeNames 方法时,我发现了一些解决问题的方法。

在 validation.php 底部的 laravel 5 中有一个名为属性的数组。
在数组中您可以提供名称属性的翻译。

这实际上是一个非常简单的解决方案,但我花了好几个小时才找到答案

感谢 haakym 的建议,它帮助我找到了答案!

使用验证器

如果您直接使用验证器进行验证,您可以这样做:

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

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

或者,我相信您可以在创建验证器实例时将其添加为最终参数。请参阅文档:http://laravel.com/api/5.0/Illuminate/Validation/Validator.html#method___construct

使用请求

如果您使用请求进行验证,您可以在请求中覆盖 getValidatorInstance

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

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

  return $validator;
}