Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate field names in Laravel validations?

It's easy to translate messages of validations in Laravel. But I couldn't find out how to translate field names.

There is a ":attribute" placeholder in validation.php that will be replaced by the real field name which is usually English.

English: The "price" field is required.

Italian:

Il campo "price" è richiesto. <== WRONG

Il campo "prezzo" è richiesto. <== RIGHT

French:

Le champ "price" est obligatoire. <== WRONG

Le champ "prix" est obligatoire. <== RIGHT

Persian:

.مورد نیاز است "price" <== WRONG

قیمت" مورد نیاز است" <== RIGHT

like image 562
Milad Rahimi Avatar asked Aug 04 '15 03:08

Milad Rahimi


People also ask

How to use validation rules in Laravel?

In Laravel, there are Validation Rules which are predefined rules, which when used in Laravel application. There is a list of rules that can be used to validate the data being submitted by the user. Syntax 2: You can also specify the rules in the form of array as shown below. Syntax 3: You can specify Multiple Validating Fields.

How to create a student registration form with Laravel validation?

In this example, we are going to create a student registration form with basic Laravel validation. Step 1: Install a fresh Laravel project. Step 2: Connect your project to the database. Step 3: Create xxxx_xx_xx_xxxxxx_create_students_table migration and the Student model.

What is the translation ID in Laravel?

The translation ID is a string that can be freely defined in your project. Dots separate different parts of the ID — allowing you to structure the translations. The substring before the first dot is the filename in which the translation is located. Laravel looks for translations in the resources/lang/<languagecode>/<filename>.php file.

How to add multiple dots to a translation in Laravel?

Using multiple dots allows you to structure the translations within a file. Create the resources/lang/en/welcome.php file and paste the following content in it: Refresh the app to see the english translations. The loader for Laravel php files is quite restrictive. It only allows one single return statement with an array of translations.


1 Answers

In the file:

{project}/resources/lang/{your-locale}/validation.php

At the bottom you have this:

'attributes' => [],

You can add your translations like this:

'attributes' => [
    'price' => 'your translation'
],
like image 155
M0rtiis Avatar answered Oct 06 '22 20:10

M0rtiis