Browse Source

Added locale support for Time::nice()

Jose Lorenzo Rodriguez 12 years ago
parent
commit
bf857e24bb
2 changed files with 13 additions and 2 deletions
  1. 10 2
      src/Utility/Time.php
  2. 3 0
      tests/TestCase/Utility/TimeTest.php

+ 10 - 2
src/Utility/Time.php

@@ -46,6 +46,13 @@ class Time extends Carbon {
 	public static $niceFormat = [IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT];
 
 /**
+ * The default locale to be used for displaying formatted date strings.
+ *
+ * @var string
+ */
+	public static $defaultLocale;
+
+/**
  * The format to use when formatting a time using `Cake\Utility\Time::timeAgoInWords()`
  * and the difference is more than `Cake\Utility\Time::$wordEnd`
  *
@@ -101,7 +108,7 @@ class Time extends Carbon {
  * be changed.
  * @return string Formatted date string
  */
-	public function nice($timezone = null) {
+	public function nice($timezone = null, $locale = null) {
 		$time = $this;
 
 		if ($timezone) {
@@ -109,7 +116,8 @@ class Time extends Carbon {
 			$time->timezone($timezone);
 		}
 
-		return IntlDateFormatter::formatObject($time, static::$niceFormat);
+		$locale = $locale ?: static::$defaultLocale;
+		return IntlDateFormatter::formatObject($time, static::$niceFormat, $locale);
 	}
 
 /**

+ 3 - 0
tests/TestCase/Utility/TimeTest.php

@@ -363,6 +363,9 @@ class TimeTest extends TestCase {
 		$result = $time->nice('America/New_York');
 		$this->assertEquals('Apr 20, 2014, 4:00 PM', $result);
 		$this->assertEquals('UTC', $time->getTimezone()->getName());
+
+		$this->assertEquals('20 avr. 2014 20:00', $time->nice(null, 'fr-FR'));
+		$this->assertEquals('20 avr. 2014 16:00', $time->nice('America/New_York', 'fr-FR'));
 	}
 
 /**