Skip to content

Commit

Permalink
Version 1.1, adds translation array
Browse files Browse the repository at this point in the history
  • Loading branch information
mildlygeeky committed Mar 14, 2016
1 parent 144ef94 commit b16252f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 21 deletions.
21 changes: 20 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,29 @@ The plugin adds the Twig time_diff filter (http://twig.sensiolabs.org/doc/extens

### Time Diff

Renders the difference between a date and now.
Renders the difference between a date and now:

{{ post.published_at|time_diff }}

Outputs "1 day ago" or "in 5 months"

### Translations

Need to translate into other languages? Version 1.1 adds a simplistic delimited string that can be
split into an array, and uses Craft's built-in translation string where available:

{{ post.published_at|time_diff_array }}

Outputs "past|1|day|singular" or "future|5|month|plural"

This could then be turned into an array with Twig's split filter:

{% set diffArray = post.published_at|time_diff_array|split('|') %}

The array's first element will be either the word "past" or "future" (not translated), the second
element is the number of units, the third is the unit name (translated if available), and the last
is the word "plural" or "singular".

## Credits

* SensioLabs for writing the Date Extension
4 changes: 2 additions & 2 deletions timediff/TimediffPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function getName()

function getVersion()
{
return '1.0';
return '1.1';
}

function getDeveloper()
Expand All @@ -20,7 +20,7 @@ function getDeveloper()

function getDeveloperUrl()
{
return 'http://mildlygeeky.com';
return 'https://www.mildlygeeky.com';
}

public function addTwigExtension()
Expand Down
68 changes: 50 additions & 18 deletions timediff/twigextensions/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
*
*/

use Symfony\Component\Translation\TranslatorInterface;

/**
* @author Robin van der Vleuten <robinvdvleuten@gmail.com>
*/
class TwigExtensionDate extends \Twig_Extension
{
public $units_translated = array();

static $units = array(
'y' => 'year',
'm' => 'month',
Expand All @@ -27,19 +27,16 @@ class TwigExtensionDate extends \Twig_Extension
's' => 'second',
);

/**
* @var TranslatorInterface
*/
private $translator;

/**
* Constructor.
*
* @param TranslatorInterface $translator A TranslatorInterface instance.
*/
public function __construct(TranslatorInterface $translator = null)
function __construct()
{
$this->translator = $translator;
$this->units_translated = array(
'y' => Craft::t('year'),
'm' => Craft::t('month'),
'd' => Craft::t('day'),
'h' => Craft::t('hour'),
'i' => Craft::t('minute'),
's' => Craft::t('second'),
);
}

/**
Expand All @@ -53,6 +50,9 @@ public function getFilters()
new \Twig_SimpleFilter('time_diff', array($this, 'diff'), array(
'needs_environment' => true
)),
new \Twig_SimpleFilter('time_diff_array', array($this, 'diff_array'), array(
'needs_environment' => true
)),
);
}

Expand Down Expand Up @@ -96,18 +96,50 @@ public function diff(\Twig_Environment $env, $date, $now = null)
return '';
}

protected function getPluralizedInterval($count, $invert, $unit)
/**
* Filter for converting dates to a time ago string like Facebook and Twitter has, in a translatable array
*
* @param Twig_Environment $env A Twig_Environment instance.
* @param string|DateTime $date A string or DateTime object to convert.
* @param string|DateTime $now A string or DateTime object to compare with. If none given, the current time will be used.
*
* @return string The converted time.
*/
public function diff_array(\Twig_Environment $env, $date, $now = null)
{
if ($this->translator) {
$id = sprintf('diff.%s.%s', $invert ? 'in' : 'ago', $unit);
// Convert both dates to DateTime instances.
$date = twig_date_converter($env, $date);
$now = twig_date_converter($env, $now);

// Get the difference between the two DateTime objects.
$diff = $date->diff($now);

return $this->translator->transChoice($id, $count, array('%count%' => $count), 'date');
// Check for each interval if it appears in the $diff object.
foreach ($this->units_translated as $attribute => $unit) {
$count = $diff->$attribute;

if (0 !== $count) {
return $this->getPluralizedIntervalArray($count, $diff->invert, $unit);
}
}

return '';
}

protected function getPluralizedInterval($count, $invert, $unit)
{

if ($count > 1) {
$unit .= 's';
}

return $invert ? "in $count $unit" : "$count $unit ago";
}

protected function getPluralizedIntervalArray($count, $invert, $unit)
{
$translateable_array = array($invert ? 'future' : 'past', $count, $unit, $count > 1 ? 'plural' : 'single');
return implode('|', $translateable_array);
}

}

0 comments on commit b16252f

Please sign in to comment.