Implemented translation of validation rule arguments in validation error messages. Closes #2816

This commit is contained in:
ADmad 2012-04-22 23:25:07 +05:30
parent a233460c7f
commit b1e7164d1a
3 changed files with 48 additions and 0 deletions

View file

@ -3189,6 +3189,11 @@ class Model extends Object implements CakeEventListener {
if (is_array($validator['rule']) && $args === null) {
$args = array_slice($ruleSet[$index]['rule'], 1);
}
if (!empty($args)) {
foreach ($args as $k => $arg) {
$args[$k] = __d($validationDomain, $arg);
}
}
$message = __d($validationDomain, $message, $args);
} elseif (is_string($index)) {
if (is_array($validator['rule'])) {

View file

@ -761,6 +761,44 @@ class ModelValidationTest extends BaseModelTest {
$this->assertEquals($expected, $TestModel->validationErrors);
}
/**
* Test validation message translation
*
* @return void
*/
public function testValidationMessageTranslation() {
$lang = Configure::read('Config.language');
Configure::write('Config.language', 'en');
App::build(array(
'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS),
), App::RESET);
$TestModel = new ValidationTest1();
$TestModel->validationDomain = 'validation_messages';
$TestModel->validate = array(
'title' => array(
array(
'rule' => array('customValidationMethod', 'arg1'),
'required' => true,
'message' => 'Validation failed: %s'
)
)
);
$TestModel->create();
$TestModel->invalidFields();
$expected = array(
'title' => array(
'Translated validation failed: Translated arg1',
)
);
$this->assertEquals($expected, $TestModel->validationErrors);
$TestModel->validationDomain = 'default';
Configure::write('Config.language', $lang);
App::build();
}
/**
* Test for 'on' => [create|update] in validation rules.
*

View file

@ -0,0 +1,5 @@
msgid "Validation failed: %s"
msgstr "Translated validation failed: %s"
msgid "arg1"
msgstr "Translated arg1"