Vikramraj
Vikramraj

Reputation: 198

How to disable translation of Model validation messages only in cakePHP?

CakePHP will automatically assume that all model validation error messages in your $validate array are intended to be localized. But, I don't want to translate model validation messages. How to achieve this, any suggestion?

Upvotes: 1

Views: 261

Answers (1)

Nunser
Nunser

Reputation: 4522

The simplest most easiest way would be just not translating those strings. So, if in your .po file

Mistake here   ->   Error aqui   //don't do that
Mistake here   ->   Mistake here

And your validation errors are "translated" to the same language.

If you just don't want to filter yourself which strings are from validation and which are "normal" strings, change the validation domain of the models (do it in the AppModel so you'll only have to do it once).

class User extends AppModel {

    public $validationDomain = 'validation_errors';
}

and now your validation messages will be in the new validation domain and not in default.pot, so you could just not translate the whole "validation_errors.pot" file and you'd be fine.

This part is only really valid for cake 2.5, I can't be sure if it applies to other versions
Now, if you want the really "difficult" way and just erase that functionality from the face of the earth, you'll have to overwrite some functionalities in Cake lib. I'm not recommending changing the code directly in the the lib Folder, just extending the classes and replacing the in app/lib, otherwise upgrading versions will be a pain.

The class and functions you'll have to modify should be CakeValidationSet in lib/Cake/Model/Validator and the function is _processValidationResponse

All parts that have something like this

__d($this->_validationDomain, $result, $args);

should be replace with a vsprintf($result, $args) or similar (depending on the name of the parameters. This __d function is called 4 times inside that function, so replace all of them.

Personally, I'd just change the validation domain, wouldn't translate the file, and be done with it. Searching the code that translated this messages was really not worth the effort (except, you know, just to know how it's done).

Upvotes: 3

Related Questions