Backend Development 6 min read

Installing and Using Custom Chinese Validation Messages in Laravel

This guide explains how to install the Laravel validation package, create a Chinese language file for validation messages, wrap the validator in a custom handler class, and demonstrate direct validation, custom messages, attribute names, and error printing with practical code examples.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Installing and Using Custom Chinese Validation Messages in Laravel

First, install the validation component via Composer:

composer require illuminate/validation

Create a Chinese language file lang/zh_cn/validation.php that returns an array of translation strings for each validation rule, following the structure used by Laravel.

<?php
return [
    // Validation Language Lines
    'accepted' => ':attribute必须接受',
    'active_url' => ':attribute必须是一个合法的 URL',
    // ... (other rules) ...
    'attributes' => [
        // 'name' => '名字',
    ],
];

Wrap the validator in a custom handler to load the Chinese translations:

namespace App\handlers;

class Validator extends \Illuminate\Validation\Factory
{
    /**
     * Create an instance of the validator factory with Chinese locale.
     * @return \Illuminate\Validation\Factory
     */
    public static function getInstance()
    {
        static $validator = null;
        if ($validator === null) {
            $translation_path = __DIR__ . '/../lang';
            $translation_locale = 'zh_cn';
            $loader = new \Illuminate\Translation\FileLoader(new \Illuminate\Filesystem\Filesystem, $translation_path);
            $translator = new \Illuminate\Translation\Translator($loader, $translation_locale);
            $validator = new \Illuminate\Validation\Factory($translator);
        }
        return $validator;
    }
}

Usage examples:

Direct validation

$data['title']   = '123';
$data['content'] = '123';
$validator = Validator::getInstance()->make($data, [
    'title'   => 'required|min:10',
    'content' => 'required|min:10',
]);

Custom messages and attribute names

$rules = [
    'title'   => 'required|min:10',
    'content' => 'required|min:10',
];
$messages = [
    'title.required' => ':title字段必须',
];
$attributes = [
    'title'   => '标题',
    'content' => '内容',
];
$validator = Validator::getInstance()->make($data, $rules, $messages, $attributes);

Print validation errors when they occur:

if ($validator->fails()) {
    print_r($validator->errors()->all());
}

For a full list of validation rules, refer to the official Laravel Validator documentation.

BackendValidationPHPlocalization
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.