Exemplo n.º 1
0
 /**
  * Returns the models errors with proper messages.
  *
  * @return array
  */
 public function errorMessages()
 {
     $messages = array();
     // Loop over each field
     foreach ($this->errors as $field => $errors) {
         $messages[$field] = array();
         // Loop over the fields errors
         foreach ($errors as $validation => $error) {
             $vars = array_merge(array('field' => Language::translate($field)), $error);
             unset($vars['message']);
             $messages[$field][] = Language::translate($error['message'], $vars);
         }
     }
     return $messages;
 }
Exemplo n.º 2
0
 /**
  * Returns time ago in words of the given date.
  *
  * @param string  $original
  * @param boolean $detailed
  *
  * @return string
  */
 public static function agoInWords($original, $detailed = false)
 {
     // Check what kind of format we're dealing with, timestamp or datetime
     // and convert it to a timestamp if it is in datetime form.
     if (!is_numeric($original)) {
         $original = static::toUnix($original);
     }
     $now = time();
     // Get the time right now...
     // Time chunks...
     $chunks = array(array(60 * 60 * 24 * 365, 'year', 'years'), array(60 * 60 * 24 * 30, 'month', 'months'), array(60 * 60 * 24 * 7, 'week', 'weeks'), array(60 * 60 * 24, 'day', 'days'), array(60 * 60, 'hour', 'hours'), array(60, 'minute', 'minutes'), array(1, 'second', 'seconds'));
     // Get the difference
     $difference = $now > $original ? $now - $original : $original - $now;
     // Loop around, get the time from
     for ($i = 0, $c = count($chunks); $i < $c; $i++) {
         $seconds = $chunks[$i][0];
         $name = $chunks[$i][1];
         $names = $chunks[$i][2];
         if (0 != ($count = floor($difference / $seconds))) {
             break;
         }
     }
     // Format the time from
     $from = Language::current()->translate("time.x_{$name}", array($count));
     // Get the detailed time from if the detail variable is true
     if ($detailed && $i + 1 < $c) {
         $seconds2 = $chunks[$i + 1][0];
         $name2 = $chunks[$i + 1][1];
         $names2 = $chunks[$i + 1][2];
         if (0 != ($count2 = floor(($difference - $seconds * $count) / $seconds2))) {
             $from = Language::current()->translate('time.x_and_x', $from, Language::current()->translate("time.x_{$name2}", array($count2)));
         }
     }
     // Return the time from
     return $from;
 }
Exemplo n.º 3
0
 /**
  * Translates the passed string.
  *
  * @param string $string       String to translate.
  * @param array  $replacements Replacements to be inserted into the string.
  */
 public function translate($string, array $replacements = [])
 {
     return Language::translate($string, $replacements);
 }
Exemplo n.º 4
0
 /**
  * @param string $template
  * @param array  $locals
  *
  * @return string
  */
 public function render($template, array $locals = [])
 {
     $templatePath = $this->find($template);
     if (!$templatePath) {
         throw new Exception("Unable to find template [{$template}]");
     }
     // View variables
     $variables = $locals + $this->globals;
     foreach ($variables as $_name => $_value) {
         ${$_name} = $_value;
     }
     // Shortcut for escaping HTML
     $e = function ($string) {
         return htmlspecialchars($string);
     };
     // Shortcut for Language::translate()
     $t = function ($string, array $vars = array()) {
         return Language::translate($string, $vars);
     };
     // Shortcut for Language::date()
     $l = function ($format, $timestamp = null) {
         return Language::date($format, $timestamp);
     };
     ob_start();
     include $templatePath;
     return ob_get_clean();
 }