mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Implemented translation of validation rule arguments in validation error messages. Closes #2816
This commit is contained in:
parent
a233460c7f
commit
b1e7164d1a
3 changed files with 48 additions and 0 deletions
|
@ -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'])) {
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
msgid "Validation failed: %s"
|
||||
msgstr "Translated validation failed: %s"
|
||||
|
||||
msgid "arg1"
|
||||
msgstr "Translated arg1"
|
Loading…
Reference in a new issue