Laravel change validation language - php

How can I change the validator language messages. For example if I want to change the 'required' text 'The kilometerszakelijk field is required.' to something else?

All language specific content is stored in your resources/lang// folder. One of the files you'll find in this folder is validation.php that holds validation messages for given language, so if you want to change the translations, you'll need to edit that file.
Depending on the language that is used by the application, messages will be read from different folders in resources/lang/. You can set language/locale using
App::setLocale('en');
You can also set fallback locale (that is used when there are no translations for current language) by setting it in your config/app.php file, e.g.:
'fallback_locale' => 'en',
See more details in the docs: http://laravel.com/docs/5.0/localization

Create a folder named like your language code in
resources/lang
then create a file in this folder called validation.php and in there, write something like
return [
'required' => 'Das Feld :attribute ist ein Pflichtfeld.',
]

For the default messages, you can adjust the translations in resources/lang/<language>/validation.php.
You can also pass entirely custom messages to the validator.

Laravel-lang
In this repository, you can find the lang files for the framework PHP, Laravel 4&5.
you can see here for use that.

Related

Custom language not added properly in Laravel 8

I'm using Laravel 8 for my project. However, I wanted to add my own language for validation, so I used laravel-lang and placed the fa folder inside the resources/lang/ directory.
Then I added these to config/app.php:
'locale' => 'fa',
'fallback_locale' => 'fa',
'faker_locale' => 'fa_IR',
And I also re-run the php artisan serve to test if it works or not, but the problem is, it does not work and still shows the English messages for Login/Register Controller validation.
So what is going wrong here? How can I add my language properly?
In order to use translation you should use proper syntax to translate your variables or your sentences:
{{ _('your_variable') }}
If you use this syntax and the problem exist yet, check your html language meta tag or lang attribute:
It should be <html lang="fa">.

Yii2: How to change a system message translation file

The problem
Yii is providing its own translations for system messages, e.g. 'File upload failed' is translated to 'Das Hochladen der Datei ist gescheitert.' when the language on my website is changed to German. This would be fine but some of the translations are grammatically incorrect and I would like to change them.
I've found the messages file that yii is using to handle the translation: yiisoft>yii2>messages>de>yii.php but I can't make changes to it as its part of the vendor directory and any changes I'd make would be overwritten during the next yii update.
What I've tried
I've tried following the instructions laid out by the users here: https://forum.yiiframework.com/t/translating-system-messages/29733. Which involves making a copy of yii.php, putting it in a new directory, making the desired translation changes and then pointing coreMessages towards it in the config. I've followed all these steps but it doesn't seem to actually do anything for me.
As it's stated in the Guide:
Yii comes with the default translation messages for validation errors and some other strings. These messages are all in the category yii. Sometimes you want to correct the default framework message translation for your application. In order to do so, configure the i18n application component like the following:
'i18n' => [
'translations' => [
'yii' => [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => '#app/messages'
],
],
],
Now you can place your adjusted translations to #app/messages/<language>/yii.php.
BTW - you mentioned that
[...] translations are grammatically incorrect [...]
It would be great for Yii 2 and its community if you could fix the problem - please fork this file and send a PR.

Do forms automatically use translation in Symfony 4?

I'm working on upgrading an app from Symfony 3 to Symfony 4.
I noticed some errors in the profiler in the new version on one of my routes that has a form. The errors were in Translation -> Translation Messages -> Missing:
These messages are not available for the given locale and cannot be found in the fallback locales. Add them to the translation catalogue to avoid Symfony outputting untranslated contents.
Every form field was listed under missing messages.
I hadn't done anything with translation in the old version, so I was wondering why the new version was expecting translations. Is that something that's automatically associated with forms now? If so, is there a way to turn that off? It really isn't necessary for this app.
When you enable the translations in the framework bundle Forms will try to use them via the TranslationExtension that is automatically registered. If you don't need any translations you can disable them. Be aware that validation errors on the form are returned as their translation keys and not the message then.
In Symfony 3 the setting should be in app/config/config.yml and in Symfony 4 directory structure they should be in either config/packages/framework.yaml or config/packages/translation.yaml under:
framework:
translator: ~ # just set this to false if you don't want any translations to be used
Your other option is to prevent the TranslationExtension from being registered or write a custom form extension that sets the translation_domain on the abstract FormType to false.
Another option is to just ignore the notices for missing translations. Since translations are cached and will always fall back to the key (in your case the actual label) it will not have any performance impact or other negative effects.
edit: Regarding your final question, I don't think this behavior changed much from Symfony 3 to 4 and you should have seen a similar behavior before. I'm guessing you just didn't notice the warning before, but it was there. That's just a guess though.
You should disable Translation in Form like follow:
$builder
......
......
->add('budget', MoneyType::class, array(
'label_attr' => array('class' => 'control-label'),
'attr' => array('class' => 'span11'),
...
'translation_domain' => false
))
......
......
;

Declaring filters in application config

I'm trying to add a filter into an application config, but there is little documentation about this issue. I have managed to call a filter through the behaviour definition in the web.php like:
'as derp' => [
'class' => 'RouteToMyFilter',
'only' => ['url/defined'],
],
I am using, as suggested in the note in this url: http://www.yiiframework.com/doc-2.0/guide-structure-filters.html
Note: When declaring filters in modules or applications, you should use routes instead of action IDs in the only and except properties. This is because action IDs alone cannot fully specify actions within the scope of a module or application.
But, as you see (the url it's stupid, I know, but, it's for testing purpouses only), I have defined a route but the library it's using the id of the active controller, ignoring any route. Is there any way to define a filter to make it use the routes or the note it's wrong?

In Symfony2, how do I get labels from the FormBuilder

In Symfony2, I'm using formbuilder. I'm setting the labels in the form, as per the documentation.
However, when I'm on the 'show' and 'index' pages, I have to copy the labels into Twig.
Is there a way to have the same labels used everywhere? The options I have thought of:
Access the formbuilder configuration, but without actually building the form
Have a central config file, and lookup from the formbuilder and the twig files into that file
However, either way requires me to 'do' something, which I'm not used to in Symfony. It seems like this is something that would already have been solved, but I'm not sure how.
You can utilize translation system to overcome this problem. Make sure that you have enabled translation in config.yml.
If you have added field in your formtype like this
$builder->add('title', 'text', array(
'label'=> 'model.title'
));
//.....
Create a file named messages.en.yml in your bundles Resources/translations directory (replace en with your default locale and create multiple files based on the locales. Check translation chapter of the book.) and put following
#src/YourBundle/Resources/translation/messages.en.yml
model:
title: "Title"
field: "Field"
#....
Add and edit forms label will show Title. In index and show pages you can do
{{ "model.title" | trans([], 'messages') }}
Though this process is a bit lengthy but it is one time and you can change the value of the labels by changing the translation files.
I just want to say something about #Mun Mun Das's answer.
In Symfony 6, it looks like we don't even need to use trans() anymore in the template in the form. I defined the label in the Type file (php) and it translates directly. I don't know why though.
Example
FormatType
#src/Form/UserType.php
$builder->add('uEmail', TextType::class, array(
'label'=> 'uEmail' ,
'attr' => [ 'maxlength' => 50]
))
Translation file
#translations/message.en.yaml
uEmail: Email
It automatically translates it. I guess Symfony try to translate it by default ? If label is not set in UserType.php or in the template with form_label() function, it will display "u Email" by default

Categories