Skip to content

allows to specify the output format of CakeTime::timeAgoInWords #951

Closed
wants to merge 5 commits into from

5 participants

@challet
challet commented Nov 12, 2012

There's already a format option, but this one was used with the end option,
when there is a cutoff point to no longer will use words.

Default value remain the same as before : "%s ago"

@ADmad ADmad commented on an outdated diff Nov 12, 2012
lib/Cake/Utility/CakeTime.php
@@ -690,6 +699,7 @@ public static function timeAgoInWords($dateTime, $options = array()) {
$format = self::$wordFormat;
$end = self::$wordEnd;
$accuracy = self::$wordAccuracy;
+ $time_ago_format = self::$timeAgoFormat;
@ADmad
CakePHP member
ADmad added a note Nov 12, 2012

As per Cake's coding standards the variable $time_ago_format needs to be in camel backed form $timeAgoFormat and so does the options key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ADmad ADmad commented on an outdated diff Nov 12, 2012
lib/Cake/Utility/CakeTime.php
@@ -715,6 +725,9 @@ public static function timeAgoInWords($dateTime, $options = array()) {
$end = $options['end'];
}
unset($options['end'], $options['format']);
+
+ $time_ago_format = isset($options['time_ago_format']) ? $options['time_ago_format'] : self::$timeAgoFormat;
+
@ADmad
CakePHP member
ADmad added a note Nov 12, 2012

Extra blank line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@markstory markstory commented on an outdated diff Nov 13, 2012
lib/Cake/Utility/CakeTime.php
@@ -844,7 +856,7 @@ public static function timeAgoInWords($dateTime, $options = array()) {
}
if (!$backwards) {
- return __d('cake', '%s ago', $relativeDate);
+ return __d('cake', $timeAgoFormat, $relativeDate);
@markstory
CakePHP member
markstory added a note Nov 13, 2012

This means that '%s ago' will not longer be caught by the i18n extract task and any timeAgoFormat will not show up in po files for sites using i18n files. Why not make the formatting conditional, and remove the default property that has been added. Either that or set the default property in the constructor so it can properly be translated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@challet
challet commented Dec 14, 2012

I think the last commit corrects the i18n usage for '%s ago'.
I would be glad to have an other opinion on that.

@dereuromark dereuromark commented on an outdated diff Dec 14, 2012
lib/Cake/Utility/CakeTime.php
@@ -844,7 +857,13 @@ public static function timeAgoInWords($dateTime, $options = array()) {
}
if (!$backwards) {
- return __d('cake', '%s ago', $relativeDate);
+ if($timeAgoFormat) {
@dereuromark
CakePHP member
dereuromark added a note Dec 14, 2012

missing space after if

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@markstory markstory and 2 others commented on an outdated diff Dec 14, 2012
lib/Cake/Utility/CakeTime.php
@@ -844,7 +857,13 @@ public static function timeAgoInWords($dateTime, $options = array()) {
}
if (!$backwards) {
- return __d('cake', '%s ago', $relativeDate);
+ if ($timeAgoFormat) {
+ // user format, has already been translated (logically, when setting the 'timeAgoFormat' option)
+ return vsprintf($timeAgoFormat, $relativeDate);
+ } else {
+ // default format, needs to be translated
+ return __d('cake', '%s ago', $relativeDate);
+ }
@markstory
CakePHP member
markstory added a note Dec 14, 2012

You don't need the else here. You can just return when the if closes.

@challet
challet added a note Dec 14, 2012

or maybe a ternary operator ? that's waht I used first before I added the comments line

@markstory
CakePHP member
markstory added a note Dec 15, 2012

I don't think a ternary will make the code easier to read. The else however, is not doing anything right now.

@dereuromark
CakePHP member
dereuromark added a note Dec 15, 2012

ternary might be too long here regarding the line length.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@challet
challet commented Dec 26, 2012

anything else to review ?

@lorenzo lorenzo closed this Jan 28, 2013
@lorenzo
CakePHP member
lorenzo commented Jan 28, 2013

Please reopen targeting the 2.4 branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.