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
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.
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.
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.
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.
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'
],
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With