Thursday, December 24, 2009

Re: Localization for Model validation messages

put this in your app_model.php


/**
* Overrides the Core invalidate function from the Model class
* with the addition to use internationalization (I18n and L10n)
* @param string $field Name of the table column
* @param mixed $value The message or value which should be returned
* 2009-07-27 ms
*/
function invalidate($field, $value = null) {
if (!is_array($this->validationErrors)) {
$this->validationErrors = array();
}
if(empty($value)) {
$value = true;
}
if (is_array($value)) {
if (count($value) > 2) { # string %s %s string, trans1, trans2
$translatedValue = sprintf(__($value[0], true), $value[1], $value
[2]);
} else { # string %s string, trans1
$translatedValue = sprintf(__($value[0], true), $value[1]);
}
$this->validationErrors[$field] = $translatedValue;
} else {
$this->validationErrors[$field] = __($value, true);
}
}

i even added an array functionality to use
message = array('at least % chars', 255)
etc


On 24 Dez., 18:39, si-mon <sijumo...@gmail.com> wrote:
> Hi All,
>
> Is there a way to use localization for validation messages in the
> Model $validate array?
>
> eg: $validate = array ( 'name' => array('rule'=>'notEmpty',
> 'message'=>'Name is mandatory' ));
>
> I need the message 'Name is mandatory' in five different languages.
>
> In other words: How can we use functions like __('message') like
> functions in model?
>
> Please help me. Thanks.

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: