# How to Configure Laravel-Validate for Different Languages

> Easily configure laravel-validate for different languages by publishing and customizing translation files. Learn how to manage validation messages in your Laravel app.

- Repository: [Milwad Khosravi/laravel-validate](https://github.com/milwad-dev/laravel-validate)
- Tags: how-to-guide
- Published: 2026-03-07

---

**Laravel-Validate automatically loads validation messages from `lang/{locale}/validate.php` based on your application's locale setting, and you can publish these files to your project's `resources/lang/` directory using vendor tags like `"validate-lang-en"` to customize translations.**

The `milwad-dev/laravel-validate` package provides extensive validation rules for Laravel applications with built-in support for multiple languages. Configuring laravel-validate for different languages relies on Laravel's standard localization system, requiring no additional settings in the package configuration file.

## How Language Files Are Organized

The package ships with translation files organized under the `lang` directory by locale code. Each file contains default validation messages for a specific language.

- **English**: [`lang/en/validate.php`](https://github.com/milwad-dev/laravel-validate/blob/main/lang/en/validate.php)
- **Chinese**: [`lang/zh_CN/validate.php`](https://github.com/milwad-dev/laravel-validate/blob/main/lang/zh_CN/validate.php)

These files contain the message strings returned as PHP arrays, keyed by validation rule names.

## Automatic Locale Detection

In [`src/LaravelValidateServiceProvider.php`](https://github.com/milwad-dev/laravel-validate/blob/main/src/LaravelValidateServiceProvider.php) at line 24, the provider registers translation loading via Laravel's `loadTranslationsFrom` method. This hooks the package's language directory into Laravel's translator.

The package determines which language file to use based solely on your application's current locale. Laravel reads this value from [`config/app.php`](https://github.com/milwad-dev/laravel-validate/blob/main/config/app.php) under the `'locale'` key. When you set the locale to `'zh_CN'`, the package automatically loads [`lang/zh_CN/validate.php`](https://github.com/milwad-dev/laravel-validate/blob/main/lang/zh_CN/validate.php) without requiring manual configuration.

## Publishing Language Files for Customization

To edit validation messages without modifying the vendor directory, the service provider's `publishLangFiles` method scans the `lang` directory and registers publishable tags for each supported language.

### Publishing Specific Language Files

Run the artisan command with the specific language tag to copy files to your project's `resources/lang/` directory:

```bash
php artisan vendor:publish --tag="validate-lang-en"

```

This copies the English validation file to [`resources/lang/en/validation.php`](https://github.com/milwad-dev/laravel-validate/blob/main/resources/lang/en/validation.php), where you can safely modify messages. The tag naming convention follows the pattern `"validate-lang-{locale}"` for each supported language.

### Editing Published Messages

After publishing, modify the returned array in your local copy:

```php
// resources/lang/en/validation.php
return [
    'phone-number' => 'The phone number format is invalid for the selected country.',
    'iban' => 'The IBAN entered is not valid for the specified country.',
    // ... other rules
];

```

## Adding Support for New Languages

To add a language not included in the package or create a regional variant:

1. **Publish a similar language file** (e.g., Spanish) using `php artisan vendor:publish --tag="validate-lang-es"`.
2. **Rename the copied file** to your custom locale code, such as [`resources/lang/es_MX/validation.php`](https://github.com/milwad-dev/laravel-validate/blob/main/resources/lang/es_MX/validation.php) for Mexican Spanish.
3. **Edit the messages** in the renamed file to match your regional requirements.
4. **Set the application locale** in [`config/app.php`](https://github.com/milwad-dev/laravel-validate/blob/main/config/app.php):
   ```php
   'locale' => 'es_MX',
   ```

## Switching Languages at Runtime

You can change the validation language dynamically using Laravel's `App` facade:

```php
use Illuminate\Support\Facades\App;

// Switch to French validation messages
App::setLocale('fr');

```

This immediately loads the corresponding [`lang/fr/validate.php`](https://github.com/milwad-dev/laravel-validate/blob/main/lang/fr/validate.php) file from the package for subsequent validation errors.

## Package Configuration Limitations

The [`config/laravel-validate.php`](https://github.com/milwad-dev/laravel-validate/blob/main/config/laravel-validate.php) file controls only the `using_container` setting for validator registration. It contains no language-specific settings, meaning all locale handling delegates to Laravel's native localization system as implemented in [`src/LaravelValidateServiceProvider.php`](https://github.com/milwad-dev/laravel-validate/blob/main/src/LaravelValidateServiceProvider.php).

## Summary

- **Language files** are stored in `lang/{locale}/validate.php` within the package source, with support for locales like `en` and `zh_CN`.
- **Automatic loading** occurs via `loadTranslationsFrom` in the service provider based on your [`config/app.php`](https://github.com/milwad-dev/laravel-validate/blob/main/config/app.php) locale setting.
- **Publishing** uses `php artisan vendor:publish --tag="validate-lang-{locale}"` to copy files to `resources/lang/{locale}/validation.php` for customization.
- **Runtime switching** is supported through `App::setLocale('{locale}')`.
- **No language settings** exist in [`config/laravel-validate.php`](https://github.com/milwad-dev/laravel-validate/blob/main/config/laravel-validate.php); configuration relies entirely on Laravel's standard localization.

## Frequently Asked Questions

### Where are the default validation messages stored in the package?

The default messages are located in the `lang/{locale}/validate.php` files within the package source, such as [`lang/en/validate.php`](https://github.com/milwad-dev/laravel-validate/blob/main/lang/en/validate.php) for English and [`lang/zh_CN/validate.php`](https://github.com/milwad-dev/laravel-validate/blob/main/lang/zh_CN/validate.php) for Chinese.

### How do I override a specific validation message for my application?

Run `php artisan vendor:publish --tag="validate-lang-{locale}"` to copy the language file to `resources/lang/{locale}/validation.php`, then edit the specific key in the returned array. Laravel will automatically load your custom version instead of the package default.

### Can I switch validation languages dynamically at runtime?

Yes. Call `App::setLocale('{locale}')` using the `Illuminate\Support\Facades\App` facade. The package will immediately begin serving validation messages from the corresponding `lang/{locale}/validate.php` file.

### What should I do if my language is not included in the package?

Publish the closest available language using the vendor publish command, rename the copied file in `resources/lang/` to match your locale code (e.g., `es_MX`), translate the messages, and set your application locale to that code in [`config/app.php`](https://github.com/milwad-dev/laravel-validate/blob/main/config/app.php) or via `App::setLocale()`.