Permalink
Browse files

Implemented translation of validation rule arguments in validation er…

…ror messages. Closes #2816
  • Loading branch information...
ADmad committed Apr 22, 2012
1 parent a233460 commit b1e7164d1a4f041762b551b4f90e2aa705d351f0
View
@@ -3189,6 +3189,11 @@ public function invalidFields($options = array()) {
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 @@ public function testValidationMessageAsArray() {
$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"

0 comments on commit b1e7164

Please sign in to comment.