From 94dc1d13f640aeeaffc5b744fdb46514ff292e15 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Tue, 3 May 2016 14:12:01 -0600 Subject: [PATCH] Added an `adminNicetime` twig filter to translate into admin's language --- CHANGELOG.md | 4 +- .../partials/dashboard-pages.html.twig | 2 +- twig/AdminTwigExtension.php | 80 +++++++++++++++++++ 3 files changed, 83 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b860cd97..0e342da3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,12 +2,12 @@ ## 05/xx/2016 1. [](#new) - * + * Added a `|adminNicetime` Twig filter to show nicetime in admin user's language 1. [](#improved) * Fixed UI issue with Backup / Update buttons positioning * Tweaked placeholders color in login/new user panels [#542](https://github.com/getgrav/grav-plugin-admin/issues/542) 1. [](#bugfix) - * Fix some untranslated strings + * Fixed several untranslated strings * Fix the version information after updating Grav from Admin * Fix a Twig autoescape issue on Plugins descriptions * Fix for showing empty drop-down with only one supported language [#522](https://github.com/getgrav/grav-plugin-admin/issues/522) diff --git a/themes/grav/templates/partials/dashboard-pages.html.twig b/themes/grav/templates/partials/dashboard-pages.html.twig index 3109d2d0..ab5ad180 100644 --- a/themes/grav/templates/partials/dashboard-pages.html.twig +++ b/themes/grav/templates/partials/dashboard-pages.html.twig @@ -9,7 +9,7 @@ {{ latest.title|e }} - {{ latest.route }}{{ latest.modified|nicetime }} + {{ latest.route }}{{ latest.modified|adminNicetime }} {% endfor %} diff --git a/twig/AdminTwigExtension.php b/twig/AdminTwigExtension.php index 94ed0439..38e65e56 100644 --- a/twig/AdminTwigExtension.php +++ b/twig/AdminTwigExtension.php @@ -40,6 +40,7 @@ class AdminTwigExtension extends \Twig_Extension new \Twig_SimpleFilter('tu', [$this, 'tuFilter']), new \Twig_SimpleFilter('toYaml', [$this, 'toYamlFilter']), new \Twig_SimpleFilter('fromYaml', [$this, 'fromYamlFilter']), + new \Twig_SimpleFilter('adminNicetime', [$this, 'adminNicetimeFilter']), ]; } @@ -60,4 +61,83 @@ class AdminTwigExtension extends \Twig_Extension $yaml = new Parser(); return $yaml->parse($value); } + + public function adminNicetimeFilter($date, $long_strings = true) + { + if (empty($date)) { + return $this->grav['admin']->translate('NICETIME.NO_DATE_PROVIDED', null, true); + } + + if ($long_strings) { + $periods = [ + "NICETIME.SECOND", + "NICETIME.MINUTE", + "NICETIME.HOUR", + "NICETIME.DAY", + "NICETIME.WEEK", + "NICETIME.MONTH", + "NICETIME.YEAR", + "NICETIME.DECADE" + ]; + } else { + $periods = [ + "NICETIME.SEC", + "NICETIME.MIN", + "NICETIME.HR", + "NICETIME.DAY", + "NICETIME.WK", + "NICETIME.MO", + "NICETIME.YR", + "NICETIME.DEC" + ]; + } + + $lengths = ["60", "60", "24", "7", "4.35", "12", "10"]; + + $now = time(); + + // check if unix timestamp + if ((string)(int)$date == $date) { + $unix_date = $date; + } else { + $unix_date = strtotime($date); + } + + // check validity of date + if (empty($unix_date)) { + return $this->grav['admin']->translate('NICETIME.BAD_DATE', null, true); + } + + // is it future date or past date + if ($now > $unix_date) { + $difference = $now - $unix_date; + $tense = $this->grav['admin']->translate('NICETIME.AGO', null, true); + + } else { + $difference = $unix_date - $now; + $tense = $this->grav['admin']->translate('NICETIME.FROM_NOW', null, true); + } + + for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths) - 1; $j++) { + $difference /= $lengths[$j]; + } + + $difference = round($difference); + + if ($difference != 1) { + $periods[$j] .= '_PLURAL'; + } + + if ($this->grav['language']->getTranslation($this->grav['user']->language, + $periods[$j] . '_MORE_THAN_TWO') + ) { + if ($difference > 2) { + $periods[$j] .= '_MORE_THAN_TWO'; + } + } + + $periods[$j] = $this->grav['admin']->translate($periods[$j], null, true); + + return "$difference $periods[$j] {$tense}"; + } }