【问题标题】:Laravel language on Auth validation errorLaravel 语言关于 Auth 验证错误
【发布时间】:2017-06-13 07:59:36
【问题描述】:

我想使用 laravel 默认功能来显示错误,但使用另一种语言。 我不需要“好名字”,而是 lang 文件中 **:attribute 值的翻译。 现在,如果我只是使用:

<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

它工作得很好,因为 :attribute 采用 phone 字段,并且在我拥有的验证语言中:

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

但是如何用语言文件管理电话字段呢?

我必须为每个字段编写自定义错误吗?请告诉我我不需要。

下面的函数只是一个错误的例子让你明白什么 我正在努力做

@if ($errors->has('phone'))
    <strong>{{ $errors->first(trans('generic.phone')) }}</strong>
@endif

【问题讨论】:

  • 现在您只有在en 目录中的auth.php 文件,创建其他语言环境目录,例如:esfr。并将所有文件粘贴到那里:auth.phppasswords.php... 你可以从这里获取翻译文件:https://github.com/caouecs/Laravel-lang
  • @zgabievi 我知道,但我也需要翻译 :attribute 并且这是在控制器/视图中设置的,而不是在 lang 中。
  • validation.php 文件中有**自定义验证属性**数组。我想你正在寻找那个。
  • @Ken 你是对的,它是重复的,我看到了你说的问题。问题在于 Laravel 版本,并且他发布了一个误导性问题。

标签: php laravel validation laravel-5.3


【解决方案1】:

真的很简单! 只需在语言文件中添加属性! 就我而言,对于lang/it/validation.php

我刚刚设置:

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

虽然所有句子都保持不变

'required' => 'Il campo :attribute &grave; obbligatorio.',

【讨论】:

    猜你喜欢
    • 2021-02-11
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    相关资源
    最近更新 更多