如何在 Laravel 中自定义验证消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18543309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to customize validation messages in Laravel?
提问by Felix4
I searched a lot before asking this question but found nothing to help me.
I started using Laravel as a PHP framework. As I started to do form validation, I used the Validator
class to manage all my validation and error messages. But I'd like to know how I can customize the error messages (I'd like to put them in another language for example).
在问这个问题之前,我搜索了很多,但没有找到任何帮助。我开始使用 Laravel 作为 PHP 框架。当我开始进行表单验证时,我使用Validator
该类来管理我所有的验证和错误消息。但我想知道如何自定义错误消息(例如,我想将它们放在另一种语言中)。
回答by Rubens Mariuzzo
The documentation says:
该文件说:
Language strings are stored in files within the
resources/lang
directory. Within this directory there should be a subdirectory for each language supported by the application.
语言字符串存储在目录内的
resources/lang
文件中。在这个目录中,应用程序支持的每种语言都应该有一个子目录。
Just create a directory with the supported language code, and add a validation.php
file (it is wise to just copy the one provided in English then start translating).
只需使用支持的语言代码创建一个目录,并添加一个validation.php
文件(只需复制英文提供的文件然后开始翻译是明智的)。
An example for Spanish translation would be created at resources/lang/es/validation.php
. And it will look as follow:
将在 上创建西班牙语翻译示例resources/lang/es/validation.php
。它将如下所示:
<?php
return array(
"required" => "El campo :attribute es requerido.",
);
After that, you will need to change the default language.
之后,您将需要更改默认语言。
The default language for your application is stored in the
app/config/app.php
configuration file. You may change the active language at any time using theApp::setLocale
method.
应用程序的默认语言存储在
app/config/app.php
配置文件中。您可以随时使用该App::setLocale
方法更改活动语言。
Example of changing the default language at runtime:
在运行时更改默认语言的示例:
App::setLocale('es');
回答by aiswarya
in laravel 5 the validation.php file comes under the folder resources.
在 Laravel 5 中,validation.php 文件位于文件夹资源下。
resources/lang/en/validation.php