如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:21:32  来源:igfitidea点击:

How to customize validation messages in Laravel?

validationlaravel

提问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 Validatorclass 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/langdirectory. 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.phpfile (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.phpconfiguration file. You may change the active language at any time using the App::setLocalemethod.

应用程序的默认语言存储在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