Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Translate messages only if I18n package is available.
  • Loading branch information
ADmad committed Jan 30, 2015
1 parent 6f7237c commit 30c834c
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/ORM/RulesChecker.php
Expand Up @@ -99,6 +99,13 @@ class RulesChecker
*/
protected $_options = [];

/**
* Whether or not to use I18n functions for translating default error messages
*
* @var bool
*/
protected $_useI18n = false;

/**
* Constructor. Takes the options to be passed to all rules.
*
Expand All @@ -107,6 +114,7 @@ class RulesChecker
public function __construct(array $options)
{
$this->_options = $options;
$this->_useI18n = function_exists('__d');
}

/**
Expand Down Expand Up @@ -300,7 +308,13 @@ public function checkDelete(EntityInterface $entity, array $options = [])
*/
public function isUnique(array $fields, $message = null)
{
$message = $message?: __d('cake', 'This value is already in use');
if (!$message) {
if ($this->_useI18n) {
$message = __d('cake', 'This value is already in use');
} else {
$message = 'This value is already in use';
}
}

$errorField = current($fields);
return $this->_addError(new IsUnique($fields), compact('errorField', 'message'));
Expand Down Expand Up @@ -328,7 +342,13 @@ public function isUnique(array $fields, $message = null)
*/
public function existsIn($field, $table, $message = null)
{
$message = $message?: __d('cake', 'This value does not exist');
if (!$message) {
if ($this->_useI18n) {
$message = __d('cake', 'This value does not exist');
} else {
$message = 'This value does not exist';
}
}

$errorField = $field;
return $this->_addError(new ExistsIn($field, $table), compact('errorField', 'message'));
Expand Down

0 comments on commit 30c834c

Please sign in to comment.