Model validation messages and i18n

Hi there!

I believe that everyone who already used i18n within CakePHP noticed that when you define model validation messages it simply doesn’t get translated, so.. what to do to bring i18n to work with these messages?

It’s simple and easy, there’s a method invalidate() in the Model class, it is used for marking fields as invalid, it receives a field to invalidate and a message, everything we need to do is overriding this method in our AppModel and calling the Model invalidate() method with our message wrapped by the translation function as shown below:

class AppModel extends Model {

	function invalidate($field, $value = true) {
		return parent::invalidate($field, __($value, true));
	}

}

.. and voilá! You won’t need to bother with it anymore :-)

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: