From 310a259c5fdf3f77ced6415741ede1c31e64e1a2 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Thu, 14 Jun 2018 17:28:16 +1200 Subject: [PATCH] Add locale to Format Fix up some regressions --- src/ORM/FieldType/DBDate.php | 18 ++++++++---------- src/ORM/FieldType/DBDatetime.php | 9 +++++---- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/ORM/FieldType/DBDate.php b/src/ORM/FieldType/DBDate.php index 9b8489b70..920eb905f 100644 --- a/src/ORM/FieldType/DBDate.php +++ b/src/ORM/FieldType/DBDate.php @@ -38,7 +38,7 @@ class DBDate extends DBField * Fixed locale to use for ISO date formatting. This is necessary to prevent * locale-specific numeric localisation breaking internal date strings. */ - const ISO_LOCALE = 'en_NZ'; + const ISO_LOCALE = 'en_US'; public function setValue($value, $record = null, $markChanged = true) { @@ -208,7 +208,7 @@ class DBDate extends DBField */ public function getFormatter($dateLength = IntlDateFormatter::MEDIUM, $timeLength = IntlDateFormatter::NONE) { - return $this->getCustomFormatter(null, $dateLength, $timeLength); + return $this->getCustomFormatter(null, null, $dateLength, $timeLength); } /** @@ -262,9 +262,10 @@ class DBDate extends DBField * for the day of the month ("1st", "2nd", "3rd" etc) * * @param string $format Format code string. See http://userguide.icu-project.org/formatparse/datetime + * @param string $locale Custom locale to use * @return string The date in the requested format */ - public function Format($format) + public function Format($format, $locale = null) { if (!$this->value) { return null; @@ -275,9 +276,8 @@ class DBDate extends DBField $format = str_replace('{o}', "'{$this->DayOfMonth(true)}'", $format); } - $formatter = $this->getFormatter(); - $formatter->setPattern($format); - return $formatter->format($this->getTimestamp()); + $formatter = $this->getCustomFormatter($locale, $format); + return $formatter->Format($this->getTimestamp()); } /** @@ -311,9 +311,7 @@ class DBDate extends DBField } // Get user format - $format = $member->getDateFormat(); - $formatter = $this->getCustomFormatter($format, $member->getLocale()); - return $formatter->format($this->getTimestamp()); + return $this->Format($member->getDateFormat(), $member->getLocale()); } /** @@ -549,7 +547,7 @@ class DBDate extends DBField */ public function URLDate() { - return rawurlencode($this->Format(self::ISO_DATE)); + return rawurlencode($this->Format(self::ISO_DATE, self::ISO_LOCALE)); } public function scaffoldFormField($title = null, $params = null) diff --git a/src/ORM/FieldType/DBDatetime.php b/src/ORM/FieldType/DBDatetime.php index 7a058bf6f..e56d99265 100644 --- a/src/ORM/FieldType/DBDatetime.php +++ b/src/ORM/FieldType/DBDatetime.php @@ -111,7 +111,7 @@ class DBDatetime extends DBDate implements TemplateGlobalProvider $timeFormat = $member->getTimeFormat(); // Get user format - return $this->Format($dateFormat . ' ' . $timeFormat); + return $this->Format($dateFormat . ' ' . $timeFormat, $member->getLocale()); } public function requireField() @@ -135,16 +135,17 @@ class DBDatetime extends DBDate implements TemplateGlobalProvider */ public function URLDatetime() { - return rawurlencode($this->Format(self::ISO_DATETIME)); + return rawurlencode($this->Format(self::ISO_DATETIME, self::ISO_LOCALE)); } public function scaffoldFormField($title = null, $params = null) { $field = DatetimeField::create($this->name, $title); $dateTimeFormat = $field->getDatetimeFormat(); + $locale = $field->getLocale(); // Set date formatting hints and example - $date = static::now()->Format($dateTimeFormat); + $date = static::now()->Format($dateTimeFormat, $locale); $field ->setDescription(_t( 'SilverStripe\\Forms\\FormField.EXAMPLE', @@ -225,7 +226,7 @@ class DBDatetime extends DBDate implements TemplateGlobalProvider */ public function getFormatter($dateLength = IntlDateFormatter::MEDIUM, $timeLength = IntlDateFormatter::MEDIUM) { - return new IntlDateFormatter(i18n::get_locale(), $dateLength, $timeLength); + return parent::getFormatter($dateLength, $timeLength); }