ability to provide custom strings for timeAgoInWords()

This commit is contained in:
euromark 2013-07-16 15:16:30 +02:00
parent 532b2099e5
commit 13ee5b7338
2 changed files with 36 additions and 3 deletions

View file

@ -186,6 +186,27 @@ class CakeTimeTest extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/**
* test the custom string options for timeAgoInWords
*
* @return void
*/
public function testTimeAgoInWordsCustomStrings() {
$result = $this->Time->timeAgoInWords(
strtotime('-8 years -4 months -2 weeks -3 days'),
array('relativeString' => 'at least %s ago', 'accuracy' => array('year' => 'year'), 'end' => '+10 years')
);
$expected = 'at least 8 years ago';
$this->assertEquals($expected, $result);
$result = $this->Time->timeAgoInWords(
strtotime('+4 months +2 weeks +3 days'),
array('absoluteString' => 'exactly on %s', 'accuracy' => array('year' => 'year'), 'end' => '+2 months')
);
$expected = 'exactly on 3/12/13';
$this->assertEquals($expected, $result);
}
/** /**
* Test the accuracy option for timeAgoInWords() * Test the accuracy option for timeAgoInWords()
* *

View file

@ -692,7 +692,7 @@ class CakeTime {
} }
/** /**
* Returns either a relative date or a formatted date depending * Returns either a relative or an absolute formatted date depending
* on the difference between the current time and given datetime. * on the difference between the current time and given datetime.
* $datetime should be in a *strtotime* - parsable format, like MySQL's datetime datatype. * $datetime should be in a *strtotime* - parsable format, like MySQL's datetime datatype.
* *
@ -708,6 +708,8 @@ class CakeTime {
* - minute => The format if minutes > 0 (default "minute") * - minute => The format if minutes > 0 (default "minute")
* - second => The format if seconds > 0 (default "second") * - second => The format if seconds > 0 (default "second")
* - `end` => The end of relative time telling * - `end` => The end of relative time telling
* - `relativeString` => The printf compatible string when outputting relative time
* - `absoluteString` => The printf compatible string when outputting absolute time
* - `userOffset` => Users offset from GMT (in hours) *Deprecated* use timezone intead. * - `userOffset` => Users offset from GMT (in hours) *Deprecated* use timezone intead.
* - `timezone` => The user timezone the timestamp should be formatted in. * - `timezone` => The user timezone the timestamp should be formatted in.
* *
@ -732,6 +734,8 @@ class CakeTime {
$timezone = null; $timezone = null;
$format = self::$wordFormat; $format = self::$wordFormat;
$end = self::$wordEnd; $end = self::$wordEnd;
$relativeString = __d('cake', '%s ago');
$absoluteString = __d('cake', 'on %s');
$accuracy = self::$wordAccuracy; $accuracy = self::$wordAccuracy;
if (is_array($options)) { if (is_array($options)) {
@ -757,6 +761,14 @@ class CakeTime {
if (isset($options['end'])) { if (isset($options['end'])) {
$end = $options['end']; $end = $options['end'];
} }
if (isset($options['relativeString'])) {
$relativeString = $options['relativeString'];
unset($options['relativeString']);
}
if (isset($options['absoluteString'])) {
$absoluteString = $options['absoluteString'];
unset($options['absoluteString']);
}
unset($options['end'], $options['format']); unset($options['end'], $options['format']);
} else { } else {
$format = $options; $format = $options;
@ -843,7 +855,7 @@ class CakeTime {
} }
if ($diff > abs($now - self::fromString($end))) { if ($diff > abs($now - self::fromString($end))) {
return __d('cake', 'on %s', date($format, $inSeconds)); return sprintf($absoluteString, date($format, $inSeconds));
} }
$f = $accuracy['second']; $f = $accuracy['second'];
@ -887,7 +899,7 @@ class CakeTime {
} }
if (!$backwards) { if (!$backwards) {
return __d('cake', '%s ago', $relativeDate); return sprintf($relativeString, $relativeDate);
} }
return $relativeDate; return $relativeDate;