diff --git a/core/lib/Drupal/Component/Datetime/DateTimePlus.php b/core/lib/Drupal/Component/Datetime/DateTimePlus.php index c706f9e..2134ffb 100644 --- a/core/lib/Drupal/Component/Datetime/DateTimePlus.php +++ b/core/lib/Drupal/Component/Datetime/DateTimePlus.php @@ -36,10 +36,6 @@ * to abort processing. The calling script can decide what to do about * errors by checking the hasErrors() method and using the messages in * the errors array. - * - * Translation of error messages is not handled in this class. Error - * messages are all in English. - * */ class DateTimePlus extends \DateTime { @@ -48,6 +44,10 @@ class DateTimePlus extends \DateTime { const PHP = 'php'; const INTL = 'intl'; + // The warning number of the message when an invalid date is converted + // to a valid date. The English message is 'The parsed date is invalid'. + const INVALID_DATE_WARNING = 11; + /** * An array of possible date parts. */ @@ -160,7 +160,7 @@ public function __construct($time = 'now', $timezone = NULL, $format = NULL, $se $this->validate_format = !empty($settings['validate_format']) ? $settings['validate_format'] : TRUE; $this->langcode = !empty($settings['langcode']) ? $settings['langcode'] : NULL; $this->country = !empty($settings['country']) ? $settings['country'] : NULL; - $this->calendar = !empty($settings['calendar']) ? $settings['calendar'] : self::CALENDAR; + $this->calendar = !empty($settings['calendar']) ? $settings['calendar'] : static::CALENDAR; // Store the original input so it is available for validation. $this->input_time_raw = $time; @@ -224,7 +224,7 @@ public function __construct($time = 'now', $timezone = NULL, $format = NULL, $se * http://www.serverphorums.com/read.php?7,555645 */ public function __toString() { - $format = self::FORMAT; + $format = static::FORMAT; return $this->format($format) . ' ' . $this->getTimeZone()->getName(); } @@ -303,7 +303,7 @@ public function inputIsObject() { */ public function constructFromObject() { try { - $this->input_time_adjusted = $this->input_time_adjusted->format(self::FORMAT); + $this->input_time_adjusted = $this->input_time_adjusted->format(static::FORMAT); parent::__construct($this->input_time_adjusted, $this->input_timezone_adjusted); } catch (\Exception $e) { @@ -362,13 +362,13 @@ public function inputIsArray() { public function constructFromArray() { try { parent::__construct('', $this->input_timezone_adjusted); - $this->input_time_adjusted = self::prepareArray($this->input_time_adjusted, TRUE); - if (self::checkArray($this->input_time_adjusted)) { + $this->input_time_adjusted = static::prepareArray($this->input_time_adjusted, TRUE); + if (static::checkArray($this->input_time_adjusted)) { // Even with validation, we can end up with a value that the // parent class won't handle, like a year outside the range // of -9999 to 9999, which will pass checkdate() but // fail to construct a date object. - $this->input_time_adjusted = self::arrayToISO($this->input_time_adjusted); + $this->input_time_adjusted = static::arrayToISO($this->input_time_adjusted); parent::__construct($this->input_time_adjusted, $this->input_timezone_adjusted); } else { @@ -461,7 +461,7 @@ public function getErrors() { if (!empty($errors['errors'])) { $this->errors += $errors['errors']; } - if (!empty($errors['warnings']) && in_array('The parsed date was invalid', $errors['warnings'])) { + if (!empty($errors['warnings']) && array_key_exists(static::INVALID_DATE_WARNING, $errors['warnings'])) { $this->errors[] = 'The date is invalid.'; } } @@ -490,24 +490,24 @@ function hasErrors() { * The date as an ISO string. */ public static function arrayToISO($array, $force_valid_date = FALSE) { - $array = self::prepareArray($array, $force_valid_date); + $array = static::prepareArray($array, $force_valid_date); $input_time_adjusted = ''; if ($array['year'] !== '') { - $input_time_adjusted = self::datePad(intval($array['year']), 4); + $input_time_adjusted = static::datePad(intval($array['year']), 4); if ($force_valid_date || $array['month'] !== '') { - $input_time_adjusted .= '-' . self::datePad(intval($array['month'])); + $input_time_adjusted .= '-' . static::datePad(intval($array['month'])); if ($force_valid_date || $array['day'] !== '') { - $input_time_adjusted .= '-' . self::datePad(intval($array['day'])); + $input_time_adjusted .= '-' . static::datePad(intval($array['day'])); } } } if ($array['hour'] !== '') { $input_time_adjusted .= $input_time_adjusted ? 'T' : ''; - $input_time_adjusted .= self::datePad(intval($array['hour'])); + $input_time_adjusted .= static::datePad(intval($array['hour'])); if ($force_valid_date || $array['minute'] !== '') { - $input_time_adjusted .= ':' . self::datePad(intval($array['minute'])); + $input_time_adjusted .= ':' . static::datePad(intval($array['minute'])); if ($force_valid_date || $array['second'] !== '') { - $input_time_adjusted .= ':' . self::datePad(intval($array['second'])); + $input_time_adjusted .= ':' . static::datePad(intval($array['second'])); } } } @@ -672,10 +672,10 @@ function canUseIntl() { */ function format($format, $settings = array()) { - $format_string_type = isset($settings['format_string_type']) ? $settings['format_string_type'] : self::PHP; + $format_string_type = isset($settings['format_string_type']) ? $settings['format_string_type'] : static::PHP; $langcode = !empty($settings['langcode']) ? $settings['langcode'] : $this->langcode; $country = !empty($settings['country']) ? $settings['country'] : $this->country; - $calendar= !empty($settings['calendar']) ? $settings['calendar'] : $this->calendar; + $calendar = !empty($settings['calendar']) ? $settings['calendar'] : $this->calendar; $time_zone = !empty($settings['timezone']) ? $settings['timezone'] : $this->getTimezone()->getName(); $lenient = !empty($settings['lenient']) ? $settings['lenient'] : FALSE; @@ -683,13 +683,13 @@ function format($format, $settings = array()) { try { // If we have what we need to use the IntlDateFormatter, do so. - if ($this->canUseIntl() && $format_string_type == self::INTL) { + if ($this->canUseIntl() && $format_string_type == static::INTL) { // Construct the $locale variable needed by the IntlDateFormatter. $locale = $langcode . '_' . $country; // If we have information about a calendar, add it. - if (!empty($calendar) && $calendar != self::CALENDAR) { + if (!empty($calendar) && $calendar != static::CALENDAR) { $locale .= '@calendar=' . $calendar; } diff --git a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php index eccafa5..9378406 100644 --- a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php +++ b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php @@ -72,10 +72,6 @@ public function __construct($time = 'now', $timezone = NULL, $format = NULL, $se // Instantiate the parent class. parent::__construct($time, $timezone, $format, $settings); - // Attempt to translate the error messages. - foreach ($this->errors as &$error) { - $error = t($error); - } } /** @@ -128,7 +124,7 @@ public function __construct($time = 'now', $timezone = NULL, $format = NULL, $se */ function format($format, $settings = array()) { - $format_string_type = isset($settings['format_string_type']) ? $settings['format_string_type'] : self::PHP; + $format_string_type = isset($settings['format_string_type']) ? $settings['format_string_type'] : static::PHP; $settings['langcode'] = !empty($settings['langcode']) ? $settings['langcode'] : $this->langcode; $settings['country'] = !empty($settings['country']) ? $settings['country'] : $this->country; @@ -163,7 +159,7 @@ function format($format, $settings = array()) { } } catch (\Exception $e) { - $this->errors[] = t($e->getMessage()); + $this->errors[] = $e->getMessage(); } return $value; }