Skip to content

Commit

Permalink
Add ISO8601 override for English date formatting
Browse files Browse the repository at this point in the history
Also add US English as an option, so that the old default can
still be selected if desired.
  • Loading branch information
samwilson committed Sep 6, 2018
1 parent 8a16193 commit 19d8187
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions i18n/en-us.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 4 additions & 0 deletions src/AppBundle/Service/Intuition.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public static function serviceFactory(RequestStack $requestStack, SessionInterfa
$intuition = new static('grantmetrics');
$intuition->registerDomain('grantmetrics', $rootDir.'/../i18n');
$intuition->setLang(strtolower($useLang));

// Also add American English, so we can access the locale information (e.g. for date formatting).
$intuition->addAvailableLang('en-us', 'US English');

return $intuition;
}
}
7 changes: 7 additions & 0 deletions src/AppBundle/Twig/FormatExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public function numberFormat($number, $decimals = 0)
*/
public function dateFormat($datetime)
{
// If the language is 'en' with no country code,
// override the US English format that's provided by ICU.
if ($this->getIntuition()->getLang() === 'en') {
return $this->dateFormatStd($datetime);
}

// Otherwise, format it according to the current locale.
if (!isset($this->dateFormatter)) {
$this->dateFormatter = new IntlDateFormatter(
$this->getIntuition()->getLang(),
Expand Down
29 changes: 23 additions & 6 deletions tests/AppBundle/Twig/FormatExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class FormatExtensionTest extends GrantMetricsTestCase
/** @var \AppBundle\Twig\FormatExtension Instance of class */
protected $formatExtension;

/** @var Intuition */
protected $intuition;

/**
* Set class instance.
*/
Expand All @@ -31,8 +34,8 @@ public function setUp()
static::bootKernel();
$stack = new RequestStack();
$session = new Session();
$intuition = new Intuition();
$this->formatExtension = new FormatExtension(static::$container, $stack, $session, $intuition);
$this->intuition = new Intuition();
$this->formatExtension = new FormatExtension(static::$container, $stack, $session, $this->intuition);
}

/**
Expand Down Expand Up @@ -107,17 +110,31 @@ public function testNumberFormat()
*/
public function testDateFormat()
{
// Localized.
// Default of English uses ISO8601 format.
$this->assertEquals('en', $this->intuition->getLang());
static::assertEquals(
'2017-02-01 23:45',
$this->formatExtension->dateFormat(new DateTime('2017-02-01 23:45:34'))
);
// Change to another locale and check format.
$this->intuition->setLang('pl');
// As a Datetime object.
static::assertEquals(
'2/1/17, 11:45 PM',
'01.02.2017, 23:45',
$this->formatExtension->dateFormat(new DateTime('2017-02-01 23:45:34'))
);
// As a string.
static::assertEquals(
'8/12/15, 11:45 AM',
'12.08.2015, 11:45',
$this->formatExtension->dateFormat('2015-08-12 11:45:50')
);
}

// ISO 8601.
/**
* Format a date according to ISO8601.
*/
public function testDateFormatStd()
{
static::assertEquals(
'2017-02-01 23:45',
$this->formatExtension->dateFormatStd(new DateTime('2017-02-01 23:45:34'))
Expand Down

0 comments on commit 19d8187

Please sign in to comment.